Skip to content

Commit 21fff22

Browse files
committed
done
1 parent 9ef31cc commit 21fff22

File tree

6 files changed

+72
-19
lines changed

6 files changed

+72
-19
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"@types/node": "^12.0.0",
1414
"@types/react": "^16.9.0",
1515
"@types/react-dom": "^16.9.0",
16+
"effector": "^22.1.2",
17+
"effector-react": "^22.0.5",
1618
"framer-motion": ">=3.0.0",
1719
"react": "^17.0.1",
1820
"react-dom": "^17.0.1",

src/components/TodoAdd.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import * as React from "react";
2-
import { Button, Input, Grid } from "@chakra-ui/react";
2+
import {Button, Grid, Input} from "@chakra-ui/react";
3+
import {useStore} from "effector-react";
4+
import {$store, addTodo, setNewTodo} from "../store";
35

46
function TodoAdd() {
7+
const store = useStore($store)
8+
59
return (
610
<Grid pt={2} templateColumns="5fr 1fr" columnGap="3">
7-
<Input placeholder="New todo" />
8-
<Button>Add Todo</Button>
11+
<Input placeholder="New todo" value={store.newTodo} onChange={(e) => setNewTodo(e.target.value)}/>
12+
<Button onClick={() => addTodo()}>Add Todo</Button>
913
</Grid>
1014
);
1115
}

src/components/TodoList.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import * as React from "react";
2-
import { Button, Input, Flex, Checkbox, Heading } from "@chakra-ui/react";
2+
import {Button, Checkbox, Flex, Heading, Input} from "@chakra-ui/react";
3+
import {useStore} from "effector-react";
4+
import {$store, remove, toggle, update} from "../store";
35

46
function TodoListItems() {
7+
const store = useStore($store);
8+
59
return (
610
<>
7-
{[].map((todo: { id: number; text: string }) => (
8-
<Flex pt={2} key={todo.id}>
9-
<Checkbox />
10-
<Input mx={2} value={todo.text} />
11-
<Button>Delete</Button>
11+
{store.todos.map(({id, done, text}) => (
12+
<Flex pt={2} key={id}>
13+
<Checkbox checked={done} onClick={() => toggle(id)}/>
14+
<Input mx={2} value={text} onChange={(e) => update({id, text: e.target.value})}/>
15+
<Button onClick={() => remove(id)}>Delete</Button>
1216
</Flex>
1317
))}
1418
</>

src/components/TopBar.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import * as React from "react";
2-
import { Button, Grid } from "@chakra-ui/react";
3-
import { ColorModeSwitcher } from "./ColorModeSwitcher";
2+
import {Button, Grid} from "@chakra-ui/react";
3+
import {ColorModeSwitcher} from "./ColorModeSwitcher";
4+
import {load} from "../store";
45

5-
/*
6-
JSON source: https://raw.githubusercontent.com/jherr/todos-four-ways/master/data/todos.json
7-
*/
6+
const url = "https://raw.githubusercontent.com/jherr/todos-four-ways/master/data/todos.json";
87

98
function TopBar() {
109
return (
1110
<Grid pt={2} templateColumns="1fr 1fr" columnGap="3">
12-
<ColorModeSwitcher />
13-
<Button>Load</Button>
11+
<ColorModeSwitcher/>
12+
<Button onClick={() => load(url)}>Load</Button>
1413
</Grid>
1514
);
1615
}

src/store.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// Standard interface and functions
2+
3+
import {createEffect, createEvent, createStore} from "effector";
4+
25
export interface Todo {
36
id: number;
47
text: string;
58
done: boolean;
69
}
710

8-
export const updateTodo = (todos: Todo[], id: number, text: string): Todo[] =>
11+
const updateTodo = (todos: Todo[], id: number, text: string): Todo[] =>
912
todos.map((todo) => ({
1013
...todo,
1114
text: todo.id === id ? text : todo.text,
@@ -20,11 +23,42 @@ export const toggleTodo = (todos: Todo[], id: number): Todo[] =>
2023
export const removeTodo = (todos: Todo[], id: number): Todo[] =>
2124
todos.filter((todo) => todo.id !== id);
2225

23-
export const addTodo = (todos: Todo[], text: string): Todo[] => [
26+
export const addTodoToList = (todos: Todo[], text: string): Todo[] => [
2427
...todos,
2528
{
26-
id: Math.max(0, Math.max(...todos.map(({ id }) => id))) + 1,
29+
id: Math.max(0, Math.max(...todos.map(({id}) => id))) + 1,
2730
text,
2831
done: false,
2932
},
3033
];
34+
35+
// Effector state implementation
36+
type Store = {
37+
todos: Todo[];
38+
newTodo: string;
39+
}
40+
41+
export const setNewTodo = createEvent<string>()
42+
export const addTodo = createEvent()
43+
export const update = createEvent<{ id: number, text: string }>()
44+
export const toggle = createEvent<number>()
45+
export const remove = createEvent<number>()
46+
47+
export const load = createEffect(async (url: string) => {
48+
const req = await fetch(url)
49+
return req.json()
50+
})
51+
52+
export const $store = createStore<Store>({todos: [], newTodo: ''})
53+
.on(load.doneData, (state, todos) => ({...state, todos}))
54+
.on(setNewTodo, (state, newTodo) => ({...state, newTodo}))
55+
.on(addTodo, (state) => ({...state, newTodo: "", todos: addTodoToList(state.todos, state.newTodo)}))
56+
.on(update, (state, {id, text}) => ({
57+
...state, newTodo: "", todos: updateTodo(state.todos, id, text)
58+
}))
59+
.on(toggle, (state, id) => ({
60+
...state, todos: toggleTodo(state.todos, id)
61+
}))
62+
.on(remove, (state, id) => ({
63+
...state, todos: removeTodo(state.todos, id)
64+
}))

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4926,6 +4926,16 @@ ee-first@1.1.1:
49264926
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
49274927
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=
49284928

4929+
effector-react@^22.0.5:
4930+
version "22.0.5"
4931+
resolved "https://registry.yarnpkg.com/effector-react/-/effector-react-22.0.5.tgz#3632d2858a32d9a0aede1d65d81be1cfdca23a93"
4932+
integrity sha512-JcqI3c557QRY2k+QsZjj+QdGXQprc8AwgUtUChzztzPC+PYwdvHkTlDJhd+rcO2CPtL2YWAClHDAsuO4NXFSBA==
4933+
4934+
effector@^22.1.2:
4935+
version "22.1.2"
4936+
resolved "https://registry.yarnpkg.com/effector/-/effector-22.1.2.tgz#5edf2aea4d6b5ae932a2a9c75312804c50e3fe55"
4937+
integrity sha512-myJlmwZbaoNV5YYaRmM4QhAQeUqpbnmmUXZkACzuhV/2z49qCByC4DT62hghHpGz6Knr/sbB4sAAZ8VJCUYy2g==
4938+
49294939
ejs@^2.6.1:
49304940
version "2.7.4"
49314941
resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.7.4.tgz#48661287573dcc53e366c7a1ae52c3a120eec9ba"

0 commit comments

Comments
 (0)