-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,101 @@ | ||
import * as m from "mithril"; | ||
import { Either, Just, Maybe, Nothing } from "purify-ts"; | ||
import { ValidatorResult, ValidatorError } from "../data/internal/error"; | ||
import { Mutable, mutable } from "./internal/lens"; | ||
import { Mutable, mutable, Prism } from "./internal/lens"; | ||
import { _just } from "./lens"; | ||
|
||
export interface FormInit { | ||
_tag: "FormInit"; | ||
} | ||
|
||
const formInitValue: FormInit = { | ||
_tag: "FormInit" | ||
}; | ||
|
||
export interface FormLoading { | ||
_tag: "FormLoading"; | ||
}; | ||
|
||
const formLoadingValue: FormLoading = { | ||
_tag: "FormLoading" | ||
}; | ||
|
||
export interface FormResult { | ||
_tag: "FormResult"; | ||
_value: Maybe<ValidatorError>; | ||
} | ||
|
||
const mkFormResult = (value: Maybe<ValidatorError>): FormResult => ({ | ||
_tag: "FormResult", | ||
_value: value | ||
}); | ||
|
||
export type FormState = FormInit | FormLoading | FormResult; | ||
|
||
const _isloading = (): Prism<FormState, boolean> => ({ | ||
get: s => Just(s._tag === "FormLoading"), | ||
set: (s, _) => Just(s) | ||
}); | ||
|
||
const _result = (): Prism<FormState, Maybe<ValidatorError>> => ({ | ||
get: s => { | ||
if (s._tag === "FormResult") { | ||
return Just(s._value); | ||
} | ||
else { | ||
return Nothing; | ||
} | ||
}, | ||
set: (s, _) => Just(s) | ||
}); | ||
|
||
export interface FormMutable<T> extends Mutable<T> { | ||
validate: <R>(f: (data: T) => ValidatorResult<R>) => Promise<Either<ValidatorError, R>>; | ||
resetErr: () => void; | ||
resetTip: () => void; | ||
reset: () => void; | ||
getErr: () => Maybe<ValidatorError>; | ||
getResult: () => Maybe<Maybe<ValidatorError>>; | ||
isValidating: () => boolean; | ||
} | ||
|
||
export const formMut = <T>(state: T): FormMutable<T> => { | ||
const err = mutable<Maybe<ValidatorError>>(Nothing); | ||
const formState = mutable<FormState>(formInitValue); | ||
const mut = mutable(state); | ||
const isProcessing = mutable<boolean>(false); | ||
|
||
const validate: FormMutable<T>["validate"] = async f => { | ||
isProcessing.set(true); | ||
formState.set(formLoadingValue); | ||
m.redraw(); | ||
|
||
const s = mut.get(); | ||
const r = await f(s); | ||
|
||
r.ifLeft(e => err.set(Just(e))); | ||
isProcessing.set(false); | ||
const es = r.swap().toMaybe(); | ||
|
||
formState.set(mkFormResult(es)); | ||
m.redraw(); | ||
return r; | ||
}; | ||
|
||
const resetErr: FormMutable<T>["resetErr"] = () => { | ||
err.set(Nothing); | ||
const resetTip: FormMutable<T>["resetTip"] = () => { | ||
formState.set(formInitValue); | ||
}; | ||
|
||
const reset: FormMutable<T>["reset"] = () => { | ||
resetErr(); | ||
resetTip(); | ||
mut.set(state); | ||
}; | ||
|
||
const getErr: FormMutable<T>["getErr"] = err.get; | ||
const getResult: FormMutable<T>["getResult"] = () => | ||
formState.prism(_result()).get(); | ||
|
||
const isValidating: FormMutable<T>["isValidating"] = isProcessing.get; | ||
const isValidating: FormMutable<T>["isValidating"] = () => | ||
formState.prism(_isloading()).get().orDefault(false); | ||
|
||
return { | ||
...mut, | ||
validate, | ||
resetErr, | ||
resetTip, | ||
reset, | ||
getErr, | ||
getResult, | ||
isValidating | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters