Skip to content

Commit 7ae29af

Browse files
committed
cleanup recipes
1 parent 248b6ab commit 7ae29af

File tree

2 files changed

+33
-67
lines changed

2 files changed

+33
-67
lines changed

recipes/ifelse_test.tsx

Lines changed: 32 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,48 @@
1+
/// <cts-enable />
2+
13
import {
2-
h,
4+
Cell,
5+
Default,
36
derive,
7+
h,
48
handler,
59
ifElse,
6-
JSONSchema,
710
lift,
811
NAME,
912
recipe,
10-
schema,
1113
str,
1214
UI,
1315
} from "commontools";
1416

15-
const inputSchema = schema({
16-
type: "object",
17-
properties: {
18-
counter: {
19-
type: "integer",
20-
default: 0,
21-
asCell: true,
22-
},
17+
const incCounter = handler<undefined, { counter: Cell<number> }>(
18+
(_, { counter }) => {
19+
counter.set(counter.get() + 1);
2320
},
24-
default: { counter: 0 },
25-
});
26-
27-
const outputSchema = schema({
28-
type: "object",
29-
properties: {},
30-
});
21+
);
3122

32-
const incCounter = handler({}, {
33-
type: "object",
34-
properties: {
35-
counter: {
36-
type: "integer",
37-
asCell: true,
38-
},
39-
},
40-
}, (_event, state) => {
41-
if (state.counter) {
42-
const current_count = state.counter.value;
43-
console.log("current count=", current_count);
44-
state.counter.set(current_count + 1);
45-
} else {
46-
console.log("counter is undefined, ignoring");
47-
}
23+
const isEven = lift<{ counter: number }, boolean>(({ counter }) => {
24+
console.log("isEven", counter);
25+
return counter % 2 === 0;
4826
});
4927

50-
const isEven = lift(({ counter }) => {
51-
return counter % 2;
28+
export default recipe<{
29+
counter: Default<number, 0>;
30+
}>("IfElseTest", ({ counter }) => {
31+
derive(counter, (c) => {
32+
console.log("derive counter: ", c);
33+
});
34+
return {
35+
[NAME]: str`counter: ${counter}`,
36+
[UI]: (
37+
<div>
38+
{isEven({ counter })
39+
? <p>counter is even : {counter}</p>
40+
: <p>counter is odd : {counter}</p>}
41+
<p />
42+
<button type="button" onClick={incCounter({ counter })}>
43+
|click me|
44+
</button>
45+
</div>
46+
),
47+
};
5248
});
53-
54-
export default recipe(
55-
inputSchema,
56-
outputSchema,
57-
({ counter }) => {
58-
const isEvenVal = isEven({ counter });
59-
derive(counter, (c) => {
60-
console.log("derive counter: ", c);
61-
});
62-
return {
63-
[NAME]: str`counter: ${counter}`,
64-
[UI]: (
65-
<div>
66-
{ifElse(
67-
isEvenVal,
68-
<p>counter is odd : {counter}</p>,
69-
<p>counter is even : {counter}</p>,
70-
)}
71-
<p />
72-
<button
73-
type="button"
74-
onClick={incCounter({ counter })}
75-
>
76-
|click me|
77-
</button>
78-
</div>
79-
),
80-
};
81-
},
82-
);

recipes/todo-list.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const addTask = handler<
3535
console.log("addTask", event, state);
3636
const items = state.items;
3737
const task = event.detail?.message?.trim();
38-
if (task) items.set([...items.get(), { title: task, done: false }]);
38+
if (task) items.push({ title: task, done: false });
3939
},
4040
);
4141

0 commit comments

Comments
 (0)