Compare commits
1 Commits
6db57cdb8f
...
ccd655e3c5
| Author | SHA1 | Date | |
|---|---|---|---|
| ccd655e3c5 |
@@ -18,7 +18,6 @@ extends Pipeable.Pipeable {
|
|||||||
|
|
||||||
readonly schema: Schema.Schema<A, I, R>
|
readonly schema: Schema.Schema<A, I, R>
|
||||||
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>
|
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>
|
||||||
readonly autosubmit: boolean
|
|
||||||
readonly debounce: Option.Option<Duration.DurationInput>
|
readonly debounce: Option.Option<Duration.DurationInput>
|
||||||
|
|
||||||
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>
|
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>
|
||||||
@@ -37,7 +36,6 @@ extends Pipeable.Class() implements Form<A, I, R, SA, SE, SR> {
|
|||||||
constructor(
|
constructor(
|
||||||
readonly schema: Schema.Schema<A, I, R>,
|
readonly schema: Schema.Schema<A, I, R>,
|
||||||
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>,
|
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>,
|
||||||
readonly autosubmit: boolean,
|
|
||||||
readonly debounce: Option.Option<Duration.DurationInput>,
|
readonly debounce: Option.Option<Duration.DurationInput>,
|
||||||
|
|
||||||
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>,
|
readonly valueRef: SubscriptionRef.SubscriptionRef<Option.Option<A>>,
|
||||||
@@ -55,15 +53,11 @@ extends Pipeable.Class() implements Form<A, I, R, SA, SE, SR> {
|
|||||||
export const isForm = (u: unknown): u is Form<unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, FormTypeId)
|
export const isForm = (u: unknown): u is Form<unknown, unknown, unknown, unknown, unknown, unknown> => Predicate.hasProperty(u, FormTypeId)
|
||||||
|
|
||||||
export namespace make {
|
export namespace make {
|
||||||
export interface Options<in out A, in out I, in out R, in out SA = void, in out SE = A, out SR = never> {
|
export interface Options<in out A, in out I, out R, in out SA = void, in out SE = A, out SR = never> {
|
||||||
readonly schema: Schema.Schema<A, I, R>
|
readonly schema: Schema.Schema<A, I, R>
|
||||||
readonly initialEncodedValue: NoInfer<I>
|
readonly initialEncodedValue: NoInfer<I>
|
||||||
readonly onSubmit: (
|
readonly onSubmit: (value: NoInfer<A>) => Effect.Effect<SA, SE, SR>,
|
||||||
this: Form<NoInfer<A>, NoInfer<I>, NoInfer<R>, unknown, unknown, unknown>,
|
readonly debounce?: Duration.DurationInput,
|
||||||
value: NoInfer<A>,
|
|
||||||
) => Effect.Effect<SA, SE, SR>
|
|
||||||
readonly autosubmit?: boolean
|
|
||||||
readonly debounce?: Duration.DurationInput
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +76,6 @@ export const make: {
|
|||||||
return new FormImpl(
|
return new FormImpl(
|
||||||
options.schema,
|
options.schema,
|
||||||
options.onSubmit,
|
options.onSubmit,
|
||||||
options.autosubmit ?? false,
|
|
||||||
Option.fromNullable(options.debounce),
|
Option.fromNullable(options.debounce),
|
||||||
|
|
||||||
valueRef,
|
valueRef,
|
||||||
@@ -121,24 +114,20 @@ export const run = <A, I, R, SA, SE, SR>(
|
|||||||
Effect.exit,
|
Effect.exit,
|
||||||
Effect.andThen(flow(
|
Effect.andThen(flow(
|
||||||
Exit.matchEffect({
|
Exit.matchEffect({
|
||||||
onSuccess: v => SubscriptionRef.set(self.valueRef, Option.some(v)).pipe(
|
onSuccess: v => Effect.andThen(
|
||||||
Effect.andThen(SubscriptionRef.set(self.errorRef, Option.none())),
|
SubscriptionRef.set(self.valueRef, Option.some(v)),
|
||||||
Effect.as(Option.some(v)),
|
SubscriptionRef.set(self.errorRef, Option.none()),
|
||||||
),
|
),
|
||||||
onFailure: c => Option.match(
|
onFailure: c => Option.match(
|
||||||
Chunk.findFirst(Cause.failures(c), e => e._tag === "ParseError"),
|
Chunk.findFirst(Cause.failures(c), e => e._tag === "ParseError"),
|
||||||
{
|
{
|
||||||
onSome: e => Effect.as(SubscriptionRef.set(self.errorRef, Option.some(e)), Option.none()),
|
onSome: e => SubscriptionRef.set(self.errorRef, Option.some(e)),
|
||||||
onNone: () => Effect.succeed(Option.none()),
|
onNone: () => Effect.void,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
}),
|
}),
|
||||||
Effect.uninterruptible,
|
Effect.uninterruptible,
|
||||||
)),
|
)),
|
||||||
Effect.andThen(value => Option.isSome(value) && self.autosubmit
|
|
||||||
?
|
|
||||||
: Effect.void
|
|
||||||
),
|
|
||||||
Effect.scoped,
|
Effect.scoped,
|
||||||
Effect.forkScoped,
|
Effect.forkScoped,
|
||||||
)
|
)
|
||||||
@@ -169,7 +158,7 @@ export const submit = <A, I, R, SA, SE, SR>(
|
|||||||
)
|
)
|
||||||
|
|
||||||
export namespace service {
|
export namespace service {
|
||||||
export interface Options<in out A, in out I, in out R, in out SA = void, in out SE = A, out SR = never>
|
export interface Options<in out A, in out I, out R, in out SA = void, in out SE = A, out SR = never>
|
||||||
extends make.Options<A, I, R, SA, SE, SR> {}
|
extends make.Options<A, I, R, SA, SE, SR> {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,16 +5,10 @@ import { Component, Form, Hooks, Memoized, Subscribable, SubscriptionSubRef } fr
|
|||||||
import { FaArrowDown, FaArrowUp } from "react-icons/fa"
|
import { FaArrowDown, FaArrowUp } from "react-icons/fa"
|
||||||
import { FaDeleteLeft } from "react-icons/fa6"
|
import { FaDeleteLeft } from "react-icons/fa6"
|
||||||
import * as Domain from "@/domain"
|
import * as Domain from "@/domain"
|
||||||
import { TextFieldFormInput } from "@/lib/form/TextFieldFormInput"
|
|
||||||
import { DateTimeUtcFromZonedInput } from "@/lib/schema"
|
import { DateTimeUtcFromZonedInput } from "@/lib/schema"
|
||||||
import { TodosState } from "./TodosState.service"
|
import { TodosState } from "./TodosState.service"
|
||||||
|
|
||||||
|
|
||||||
const TodoFormSchema = Schema.compose(Schema.Struct({
|
|
||||||
...Domain.Todo.Todo.fields,
|
|
||||||
completedAt: Schema.OptionFromSelf(DateTimeUtcFromZonedInput),
|
|
||||||
}), Domain.Todo.Todo)
|
|
||||||
|
|
||||||
const makeTodo = makeUuid4.pipe(
|
const makeTodo = makeUuid4.pipe(
|
||||||
Effect.map(id => Domain.Todo.Todo.make({
|
Effect.map(id => Domain.Todo.Todo.make({
|
||||||
id,
|
id,
|
||||||
@@ -34,62 +28,51 @@ export class Todo extends Component.makeUntraced("Todo")(function*(props: TodoPr
|
|||||||
const runtime = yield* Effect.runtime()
|
const runtime = yield* Effect.runtime()
|
||||||
const state = yield* TodosState
|
const state = yield* TodosState
|
||||||
|
|
||||||
const [indexRef, form, contentField, completedAtField] = yield* Component.useOnChange(() => Effect.gen(function*() {
|
const { ref, indexRef, contentRef, completedAtRef } = yield* Hooks.useMemo(() => Match.value(props).pipe(
|
||||||
const indexRef = Match.value(props).pipe(
|
Match.tag("new", () => Effect.Do.pipe(
|
||||||
Match.tag("new", () => Subscribable.make({ get: Effect.succeed(-1), changes: Stream.empty })),
|
Effect.bind("ref", () => Effect.andThen(makeTodo, SubscriptionRef.make)),
|
||||||
Match.tag("edit", ({ id }) => state.getIndexSubscribable(id)),
|
Effect.let("indexRef", () => Subscribable.make({ get: Effect.succeed(-1), changes: Stream.empty })),
|
||||||
Match.exhaustive,
|
)),
|
||||||
)
|
Match.tag("edit", ({ id }) => Effect.Do.pipe(
|
||||||
|
Effect.let("ref", () => state.getElementRef(id)),
|
||||||
|
Effect.let("indexRef", () => state.getIndexSubscribable(id)),
|
||||||
|
)),
|
||||||
|
Match.exhaustive,
|
||||||
|
|
||||||
|
Effect.let("contentRef", ({ ref }) => SubscriptionSubRef.makeFromPath(ref, ["content"])),
|
||||||
|
Effect.let("completedAtRef", ({ ref }) => SubscriptionSubRef.makeFromPath(ref, ["completedAt"])),
|
||||||
|
), [props._tag, props._tag === "edit" ? props.id : undefined])
|
||||||
|
|
||||||
|
const { form } = yield* Component.useOnChange(() => Effect.gen(function*() {
|
||||||
const form = yield* Form.service({
|
const form = yield* Form.service({
|
||||||
schema: TodoFormSchema,
|
schema: Domain.Todo.TodoFromJson,
|
||||||
initialEncodedValue: yield* Schema.encode(TodoFormSchema)(
|
initialEncodedValue: yield* Schema.encode(Domain.Todo.TodoFromJson)(
|
||||||
yield* Match.value(props).pipe(
|
yield* Match.value(props).pipe(
|
||||||
Match.tag("new", () => makeTodo),
|
Match.tag("new", () => makeTodo),
|
||||||
Match.tag("edit", ({ id }) => state.getElementRef(id)),
|
Match.tag("edit", ({ id }) => state.getElementRef(id)),
|
||||||
Match.exhaustive,
|
Match.exhaustive,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
onSubmit: function(todo) {
|
onSubmit: v => Effect.void,
|
||||||
return Match.value(props).pipe(
|
|
||||||
Match.tag("new", () => Ref.update(state.ref, Chunk.prepend(todo)).pipe(
|
|
||||||
Effect.andThen(makeTodo),
|
|
||||||
Effect.andThen(Schema.encode(TodoFormSchema)),
|
|
||||||
Effect.andThen(v => Ref.set(this.encodedValueRef, v)),
|
|
||||||
)),
|
|
||||||
Match.tag("edit", ({ id }) => Ref.set(state.getElementRef(id), todo)),
|
|
||||||
Match.exhaustive,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return [
|
return { form }
|
||||||
indexRef,
|
|
||||||
form,
|
|
||||||
Form.field(form, ["content"]),
|
|
||||||
Form.field(form, ["completedAt"]),
|
|
||||||
] as const
|
|
||||||
}), [props._tag, props._tag === "edit" ? props.id : undefined])
|
}), [props._tag, props._tag === "edit" ? props.id : undefined])
|
||||||
|
|
||||||
const [index, size] = yield* Hooks.useSubscribables(indexRef, state.sizeSubscribable)
|
const [index, size] = yield* Hooks.useSubscribables(indexRef, state.sizeSubscribable)
|
||||||
const submit = yield* Form.useSubmit(form)
|
|
||||||
const TextFieldFormInputFC = yield* TextFieldFormInput
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Flex direction="row" align="center" gap="2">
|
<Flex direction="row" align="center" gap="2">
|
||||||
<Box flexGrow="1">
|
<Box flexGrow="1">
|
||||||
<Flex direction="column" align="stretch" gap="2">
|
<Flex direction="column" align="stretch" gap="2">
|
||||||
<TextFieldFormInputFC
|
<StringTextAreaInputFC ref={contentRef} />
|
||||||
field={contentField}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<Flex direction="row" justify="center" align="center" gap="2">
|
<Flex direction="row" justify="center" align="center" gap="2">
|
||||||
<TextFieldFormInputFC
|
<OptionalDateTimeInputFC
|
||||||
optional
|
|
||||||
field={completedAtField}
|
|
||||||
type="datetime-local"
|
type="datetime-local"
|
||||||
defaultValue=""
|
ref={completedAtRef}
|
||||||
|
defaultValue={yield* Hooks.useOnce(() => DateTime.now)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{props._tag === "new" &&
|
{props._tag === "new" &&
|
||||||
|
|||||||
Reference in New Issue
Block a user