Feat(frontend): Model the todo class

This commit is contained in:
minhtrannhat 2023-09-04 15:52:53 -04:00
parent 4aba3db888
commit 36fadc059a
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062

36
frontend/src/models.tsx Normal file
View File

@ -0,0 +1,36 @@
import { formatISO } from "date-fns";
import * as yup from "yup";
const todoSchema = yup.object({
complete: yup.boolean().required(),
due: yup.date().nullable(),
id: yup.number().required().positive().integer(),
task: yup.string().trim().min(1).defined().strict(true),
});
export class Todo {
complete: boolean;
due: Date | null;
id: number;
task: string;
constructor(data: any) {
const validatedData = todoSchema.validateSync(data);
this.complete = validatedData.complete;
this.due = validatedData.due ?? null;
this.id = validatedData.id;
this.task = validatedData.task;
}
toJSON(): any {
return {
complete: this.complete,
due:
this.due !== null
? formatISO(this.due, { representation: "date" })
: null,
id: this.id,
task: this.task,
};
}
}