0.1.3 #4
@@ -1,5 +1,6 @@
|
||||
import * as Domain from "@/domain"
|
||||
import { TextAreaInput } from "@/lib/TextAreaInput"
|
||||
import { TextFieldInput } from "@/lib/TextFieldInput"
|
||||
import { Box, Button, Flex, IconButton } from "@radix-ui/themes"
|
||||
import { GetRandomValues, makeUuid4 } from "@typed/id"
|
||||
import { Chunk, Effect, Match, Option, Ref, Runtime, Schema, SubscriptionRef } from "effect"
|
||||
@@ -12,6 +13,7 @@ import { TodosState } from "./TodosState.service"
|
||||
|
||||
|
||||
const StringTextAreaInput = TextAreaInput(Schema.String)
|
||||
const OptionalDateInput = TextFieldInput({ optional: true, schema: Schema.DateTimeUtc })
|
||||
|
||||
const makeTodo = makeUuid4.pipe(
|
||||
Effect.map(id => Domain.Todo.Todo.make({
|
||||
@@ -32,7 +34,7 @@ export class Todo extends Component.makeUntraced(function* Todo(props: TodoProps
|
||||
const runtime = yield* Effect.runtime()
|
||||
const state = yield* TodosState
|
||||
|
||||
const [ref, contentRef] = yield* Hooks.useMemo(() => Match.value(props).pipe(
|
||||
const [ref, contentRef, completedAtRef] = yield* Hooks.useMemo(() => Match.value(props).pipe(
|
||||
Match.tag("new", () => Effect.andThen(makeTodo, SubscriptionRef.make)),
|
||||
Match.tag("edit", ({ index }) => Effect.succeed(SubscriptionSubRef.makeFromChunkRef(state.ref, index))),
|
||||
Match.exhaustive,
|
||||
@@ -40,18 +42,38 @@ export class Todo extends Component.makeUntraced(function* Todo(props: TodoProps
|
||||
Effect.map(ref => [
|
||||
ref,
|
||||
SubscriptionSubRef.makeFromPath(ref, ["content"]),
|
||||
SubscriptionSubRef.makeFromPath(ref, ["completedAt"]),
|
||||
] as const),
|
||||
), [props._tag, props.index])
|
||||
|
||||
const [size] = yield* Hooks.useSubscribeRefs(state.sizeRef)
|
||||
|
||||
const StringTextAreaInputFC = yield* StringTextAreaInput
|
||||
const OptionalDateInputFC = yield* OptionalDateInput
|
||||
|
||||
return (
|
||||
<Flex direction="column" align="stretch" gap="2">
|
||||
<Flex direction="row" align="center" gap="2">
|
||||
<Box flexGrow="1">
|
||||
<Flex direction="column" align="stretch" gap="2">
|
||||
<StringTextAreaInputFC ref={contentRef} />
|
||||
|
||||
<Flex direction="row" justify="center" align="center" gap="2">
|
||||
<OptionalDateInputFC ref={completedAtRef} />
|
||||
|
||||
{props._tag === "new" &&
|
||||
<Button
|
||||
onClick={() => ref.pipe(
|
||||
Effect.andThen(todo => Ref.update(state.ref, Chunk.prepend(todo))),
|
||||
Effect.andThen(makeTodo),
|
||||
Effect.andThen(todo => Ref.set(ref, todo)),
|
||||
Runtime.runSync(runtime),
|
||||
)}
|
||||
>
|
||||
Add
|
||||
</Button>
|
||||
}
|
||||
</Flex>
|
||||
</Flex>
|
||||
</Box>
|
||||
|
||||
{props._tag === "edit" &&
|
||||
@@ -96,22 +118,6 @@ export class Todo extends Component.makeUntraced(function* Todo(props: TodoProps
|
||||
</Flex>
|
||||
}
|
||||
</Flex>
|
||||
|
||||
{props._tag === "new" &&
|
||||
<Flex direction="row" justify="center">
|
||||
<Button
|
||||
onClick={() => ref.pipe(
|
||||
Effect.andThen(todo => Ref.update(state.ref, Chunk.prepend(todo))),
|
||||
Effect.andThen(makeTodo),
|
||||
Effect.andThen(todo => Ref.set(ref, todo)),
|
||||
Runtime.runSync(runtime),
|
||||
)}
|
||||
>
|
||||
Add
|
||||
</Button>
|
||||
</Flex>
|
||||
}
|
||||
</Flex>
|
||||
)
|
||||
}).pipe(
|
||||
Memo.memo
|
||||
|
||||
Reference in New Issue
Block a user