From 36fadc059a2cb49ec8ee68875899770d15579ad4 Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Mon, 4 Sep 2023 15:52:53 -0400 Subject: [PATCH] Feat(frontend): Model the todo class --- frontend/src/models.tsx | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 frontend/src/models.tsx diff --git a/frontend/src/models.tsx b/frontend/src/models.tsx new file mode 100644 index 0000000..2948319 --- /dev/null +++ b/frontend/src/models.tsx @@ -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, + }; + } +}