Skip to content

Commit

Permalink
FEAT/zod added in todo example (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
AugustinSorel authored Jan 6, 2024
1 parent 531fe33 commit d9c1225
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
3 changes: 2 additions & 1 deletion example/todo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"webpack-dev-server": "^4.15.1"
},
"dependencies": {
"SerendipJS": "workspace:^"
"SerendipJS": "workspace:^",
"zod": "^3.22.4"
}
}
21 changes: 17 additions & 4 deletions example/todo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import {
} from "SerendipJS";
import "./index.css";
import { getTodos, saveTodos } from "./localStorage";
import { z } from "zod";

export type Todo = { id: number; title: string; isDone: boolean };
export const todoSchema = z.object({
id: z.number(),
title: z.string().trim().min(1),
isDone: z.boolean(),
});
export const todosSchema = todoSchema.array();

export type Todo = z.infer<typeof todoSchema>;

const todos = getTodos();

Expand All @@ -28,12 +36,17 @@ const newTodoForm = (emit: Emit<typeof reducers>) => {
e.preventDefault();
const formElement = <HTMLFormElement>e.currentTarget;

//FIXME: add zod
const formData = new FormData(formElement);

const todoTitle = formData.get("todo-title") as string;
const todoTitle = todoSchema.shape.title.safeParse(
formData.get("todo-title"),
);

if (!todoTitle.success) {
return alert("todo title cannot be empty");
}

emit("addNewTodo", todoTitle);
emit("addNewTodo", todoTitle.data);
},
},
},
Expand Down
15 changes: 8 additions & 7 deletions example/todo/src/localStorage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//FIXME: add zod

import { Todo } from ".";
import { Todo, todosSchema } from ".";

const localStorageKey = "todo";

Expand All @@ -9,9 +7,12 @@ export const saveTodos = (todos: Todo[]) => {
};

export const getTodos = () => {
const todos = localStorage.getItem(localStorageKey) ?? "";

const parsedTodos = JSON.parse(todos) || [];
try {
const unsafeTodos = localStorage.getItem(localStorageKey) ?? "";
const todos = todosSchema.parse(JSON.parse(unsafeTodos));

return parsedTodos as Todo[];
return todos;
} catch (e) {
return [];
}
};
6 changes: 2 additions & 4 deletions packages/runtime/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const createApp = <TState, TReducers extends Reducers<TState>>({

const dispatcher = new Dispatcher();

const emit = (eventName: string, payload: any) => {
const emit: Emit<any> = (eventName, payload) => {
dispatcher.dispatch(eventName, payload);
};

Expand All @@ -51,7 +51,7 @@ export const createApp = <TState, TReducers extends Reducers<TState>>({
destroyDom(vdom);
}

vdom = view(state, emit as Emit<any>);
vdom = view(state, emit);

if (vdom && parentEl) {
mountDOM(vdom, parentEl);
Expand Down Expand Up @@ -87,5 +87,3 @@ export const createApp = <TState, TReducers extends Reducers<TState>>({
},
};
};

//TODO: find something cleaner instead of satifies
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d9c1225

Please sign in to comment.