Managing the app state
As with the backend, it helps to have models that represent the data used in the app. These models will validate the data, help the linters ensure that we are using the data correctly, and ensure that the correct types are used. We will also use the model to correctly convert to and from the JSON representation used to communicate with the backend API.
The to-do model needs to be constructed from JSON, based on what is received from the backend or from data that the user entered. Then, the model needs to output as JSON so that this output can be sent to the backend. In addition, the model should validate that the data it is constructed from is of the correct structure and convert types (i.e., from strings representing dates in JSON to Date
instances).
We only need a model for to-dos in the frontend, and hence we need the following in frontend/src/models.ts:
import { formatISO } from "date-fns"; import * as yup from "yup"; const todoSchema...