|
1 | | -import { |
2 | | - children, |
3 | | - createEffect, |
4 | | - createMemo, |
5 | | - createSignal, |
6 | | - For, |
7 | | - PropsWithChildren, |
8 | | - splitProps, |
9 | | -} from "solid-js"; |
10 | | -import { ResolvedJSXElement } from "solid-js/types/reactive/signal"; |
11 | | -import styles from "./App.module.css"; |
| 1 | +import { createResource, For } from "solid-js"; |
12 | 2 |
|
13 | | -type Todo = { |
14 | | - id: number; |
15 | | - title: string; |
16 | | - finished: boolean; |
17 | | -}; |
18 | | - |
19 | | -const formattedTodo = (todo: Todo) => { |
20 | | - return createMemo( |
21 | | - () => `${todo.title} - ${todo.finished ? "DONE" : "ACTIVE"}` |
22 | | - ); |
23 | | -}; |
24 | | - |
25 | | -type TodoProps = { |
26 | | - todo: Todo; |
27 | | - updateTodo: (todo: Todo) => void; |
28 | | - deleteTodo: (todoId: number) => void; |
29 | | -}; |
30 | | - |
31 | | -const Todo = (props: TodoProps) => { |
32 | | - const [editedTitle, setEditedTitle] = createSignal(props.todo.title); |
33 | | - const [editMode, setEditMode] = createSignal(false); |
34 | | - |
35 | | - const [data, actions] = splitProps(props, ["todo"]); |
36 | | - |
37 | | - return ( |
38 | | - <div> |
39 | | - {editMode() ? ( |
40 | | - <input |
41 | | - value={editedTitle()} |
42 | | - onInput={(e) => setEditedTitle(e.currentTarget.value)} |
43 | | - /> |
44 | | - ) : ( |
45 | | - <p>{formattedTodo(data.todo)()}</p> |
46 | | - )} |
47 | | - |
48 | | - {editMode() ? ( |
49 | | - <button |
50 | | - onClick={() => |
51 | | - actions.updateTodo({ ...data.todo, title: editedTitle() }) |
52 | | - }> |
53 | | - Save |
54 | | - </button> |
55 | | - ) : ( |
56 | | - <button onClick={() => setEditMode(true)}>Edit</button> |
57 | | - )} |
58 | | - |
59 | | - {!data.todo.finished && ( |
60 | | - <button |
61 | | - onClick={() => actions.updateTodo({ ...data.todo, finished: true })}> |
62 | | - Finish |
63 | | - </button> |
64 | | - )} |
65 | | - |
66 | | - <button onClick={() => actions.deleteTodo(data.todo.id)}>Delete</button> |
67 | | - </div> |
68 | | - ); |
69 | | -}; |
70 | | - |
71 | | -const ListTodos = (props: PropsWithChildren) => { |
72 | | - const memo = children(() => props.children); |
73 | | - const [counter, setCounter] = createSignal(0); |
74 | | - |
75 | | - createEffect(() => |
76 | | - setCounter(((memo() as ResolvedJSXElement[]) || []).length) |
77 | | - ); |
78 | | - // Below code won't work (because props.children.length is not "reactive"?) |
79 | | - // createEffect(() => setCounter(props.children.length)); |
80 | | - |
81 | | - return ( |
82 | | - <div class={styles.todo}> |
83 | | - <div>{memo()}</div> |
84 | | - {/* <div>{props.children}</div> */} |
85 | | - <p>Total todos: {counter()}</p> |
86 | | - </div> |
87 | | - ); |
88 | | -}; |
89 | | - |
90 | | -type AddTodosProps = { |
91 | | - addTodo: (todo: Todo) => void; |
92 | | - count: number; |
93 | | -}; |
94 | | - |
95 | | -const AddTodos = (props: AddTodosProps) => { |
96 | | - const [title, setTitle] = createSignal(""); |
| 3 | +interface Device { |
| 4 | + id: string; |
| 5 | + name: string; |
| 6 | + userId: string; |
| 7 | +} |
97 | 8 |
|
98 | | - const addTodo = () => { |
99 | | - props.addTodo({ id: props.count, title: title(), finished: false }); |
100 | | - setTitle(""); |
| 9 | +function App() { |
| 10 | + const [devices, { mutate, refetch }] = createResource<Device[]>(async () => { |
| 11 | + const response = await fetch("http://localhost:3001/api/devices", { |
| 12 | + method: "GET", |
| 13 | + body: JSON.stringify({ |
| 14 | + userId: 1, |
| 15 | + }), |
| 16 | + }); |
| 17 | + const connectedDevices = await response.json(); |
| 18 | + return connectedDevices; |
| 19 | + }); |
| 20 | + |
| 21 | + const onSend = () => { |
| 22 | + console.log("onSend"); |
| 23 | + vscodeApi.postMessage({ |
| 24 | + type: "send", |
| 25 | + }); |
101 | 26 | }; |
102 | 27 |
|
103 | | - return ( |
104 | | - <div> |
105 | | - <input |
106 | | - type="text" |
107 | | - onInput={(e) => setTitle(e.currentTarget.value)} |
108 | | - value={title()} |
109 | | - /> |
110 | | - {title() && <button onClick={addTodo}>Add</button>} |
111 | | - </div> |
112 | | - ); |
113 | | -}; |
114 | | - |
115 | | -function App() { |
116 | | - const [todos, setTodos] = createSignal<Todo[]>([]); |
| 28 | + const onReceive = () => { |
| 29 | + console.log("onReceive"); |
| 30 | + vscodeApi.postMessage({ |
| 31 | + type: "receive", |
| 32 | + }); |
| 33 | + }; |
117 | 34 |
|
118 | 35 | return ( |
119 | 36 | <div> |
120 | | - <button |
121 | | - onClick={() => |
122 | | - vscodeApi.postMessage({ |
123 | | - type: "fetch", |
124 | | - text: "hooli", |
125 | | - }) |
126 | | - }> |
127 | | - PUSH SOME MESSAGE TO VSCODE |
128 | | - </button> |
| 37 | + <p>Devices</p> |
129 | 38 |
|
130 | 39 | <div> |
131 | | - <h4>Simple Todo App</h4> |
132 | | - <AddTodos |
133 | | - addTodo={(todo) => setTodos([...todos(), todo])} |
134 | | - count={todos().length} |
135 | | - /> |
136 | | - |
137 | | - <ListTodos> |
138 | | - <For each={todos()}> |
139 | | - {(todo) => ( |
140 | | - <Todo |
141 | | - todo={todo} |
142 | | - updateTodo={(newTodo) => |
143 | | - setTodos( |
144 | | - todos().map((t) => (t.id === newTodo.id ? newTodo : t)) |
145 | | - ) |
146 | | - } |
147 | | - deleteTodo={(todoId) => |
148 | | - setTodos(todos().filter((t) => t.id !== todoId)) |
149 | | - } |
150 | | - /> |
151 | | - )} |
152 | | - </For> |
153 | | - </ListTodos> |
| 40 | + <button onClick={onSend}>SEND</button> |
| 41 | + <button onClick={onReceive}>RECEIVE</button> |
154 | 42 | </div> |
155 | 43 |
|
156 | 44 | <div> |
157 | | - <h4>(WIP)</h4> |
| 45 | + <For each={devices()} fallback={<span>Empty</span>}> |
| 46 | + {(device) => ( |
| 47 | + <div> |
| 48 | + <span>{`${device.name} - ${device.id}`}</span> |
| 49 | + <div> |
| 50 | + <button onClick={onSend}>SEND</button> |
| 51 | + <button onClick={onReceive}>RECEIVE</button> |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + )} |
| 55 | + </For> |
158 | 56 | </div> |
159 | 57 | </div> |
160 | 58 | ); |
|
0 commit comments