[FE] Send custom_label to backend

This commit is contained in:
Araozu 2023-09-02 19:42:57 -05:00
parent 77bd3ba00f
commit 9e38942a2e
3 changed files with 14 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import { XIcon } from "../../icons/XIcon";
import { allCourses } from "../../utils/allCourses"; import { allCourses } from "../../utils/allCourses";
import { RegisterBatchCreate } from "../../types/Register"; import { RegisterBatchCreate } from "../../types/Register";
import { RegistrationPreview } from "."; import { RegistrationPreview } from ".";
import { loadCustomLabels } from "../../utils/allCustomLabels";
function isoDateToLocalDate(date: string): string { function isoDateToLocalDate(date: string): string {
@ -13,16 +14,19 @@ function isoDateToLocalDate(date: string): string {
export function RegisterPreview(props: {selections: Array<RegistrationPreview>, personId: number | null, onDelete: (v: number) => void, onRegister: () => void}) { export function RegisterPreview(props: {selections: Array<RegistrationPreview>, personId: number | null, onDelete: (v: number) => void, onRegister: () => void}) {
const submit = async() => { const submit = async() => {
const registers: RegisterBatchCreate = props.selections.map(({courseId, date}) => ({ const registers: RegisterBatchCreate = props.selections.map(({courseId, date, customLabel}) => ({
person_id: props.personId!, person_id: props.personId!,
course_id: courseId, course_id: courseId,
date, date,
custom_label: customLabel,
})); }));
const result = await createRegisters(registers); const result = await createRegisters(registers);
if (result === null) { if (result === null) {
console.log("Create register: success"); console.log("Create register: success");
// Custom labels may have changed, reload them
loadCustomLabels();
} else { } else {
console.log(`error. ${result}`); console.log(`error. ${result}`);
} }

View File

@ -5,6 +5,10 @@ export type RegisterBatchCreate = Array<{
* YYYY-MM-DD * YYYY-MM-DD
*/ */
date: string, date: string,
/**
* Value of the custom label
*/
custom_label: string,
}> }>
export type Register = { export type Register = {

View File

@ -5,8 +5,7 @@ type CustomLabelsMap = {[k: number]: CustomLabel};
export const [customLabelsMap, setCustomLabelsMap] = createSignal<{[k: number]: CustomLabel}>({}); export const [customLabelsMap, setCustomLabelsMap] = createSignal<{[k: number]: CustomLabel}>({});
(() => { export function loadCustomLabels() {
// Get all labels from the API
fetch(`${import.meta.env.VITE_BACKEND_URL}/api/label`) fetch(`${import.meta.env.VITE_BACKEND_URL}/api/label`)
.then((res) => res.json()) .then((res) => res.json())
.then((data: Array<CustomLabel>) => { .then((data: Array<CustomLabel>) => {
@ -16,4 +15,7 @@ export const [customLabelsMap, setCustomLabelsMap] = createSignal<{[k: number]:
} }
setCustomLabelsMap(map); setCustomLabelsMap(map);
}); });
})(); }
loadCustomLabels();