[FE] Send custom_label to backend

master
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 { RegisterBatchCreate } from "../../types/Register";
import { RegistrationPreview } from ".";
import { loadCustomLabels } from "../../utils/allCustomLabels";
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}) {
const submit = async() => {
const registers: RegisterBatchCreate = props.selections.map(({courseId, date}) => ({
const registers: RegisterBatchCreate = props.selections.map(({courseId, date, customLabel}) => ({
person_id: props.personId!,
course_id: courseId,
date,
custom_label: customLabel,
}));
const result = await createRegisters(registers);
if (result === null) {
console.log("Create register: success");
// Custom labels may have changed, reload them
loadCustomLabels();
} else {
console.log(`error. ${result}`);
}

View File

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

View File

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