You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.5 KiB
59 lines
1.5 KiB
import { useCallback, useMemo, useState } from 'react'; |
|
|
|
import buildObjectStateSetterCallback from '../lib/buildObjectStateSetterCallback'; |
|
|
|
import FormSummary from '../components/FormSummary'; |
|
|
|
const useChecklist = (): { |
|
buildDeleteDialogProps: BuildDeleteDialogPropsFunction; |
|
checklist: Checklist; |
|
checks: ArrayChecklist; |
|
getCheck: GetCheckFunction; |
|
hasChecks: boolean; |
|
setCheck: SetCheckFunction; |
|
} => { |
|
const [checklist, setChecklist] = useState<Checklist>({}); |
|
|
|
const checks = useMemo(() => Object.keys(checklist), [checklist]); |
|
|
|
const hasChecks = useMemo(() => checks.length > 0, [checks.length]); |
|
|
|
const buildDeleteDialogProps = useCallback<BuildDeleteDialogPropsFunction>( |
|
({ |
|
confirmDialogProps = {}, |
|
formSummaryProps = {}, |
|
getConfirmDialogTitle, |
|
}) => ({ |
|
actionProceedText: 'Delete', |
|
content: ( |
|
<FormSummary entries={checklist} maxDepth={0} {...formSummaryProps} /> |
|
), |
|
proceedColour: 'red', |
|
titleText: getConfirmDialogTitle(checks.length), |
|
...confirmDialogProps, |
|
}), |
|
[checklist, checks.length], |
|
); |
|
|
|
const getCheck = useCallback<GetCheckFunction>( |
|
(key) => checklist[key], |
|
[checklist], |
|
); |
|
|
|
const setCheck = useCallback<SetCheckFunction>( |
|
(key, checked) => |
|
setChecklist(buildObjectStateSetterCallback(key, checked || undefined)), |
|
[], |
|
); |
|
|
|
return { |
|
buildDeleteDialogProps, |
|
checklist, |
|
checks, |
|
getCheck, |
|
hasChecks, |
|
setCheck, |
|
}; |
|
}; |
|
|
|
export default useChecklist;
|
|
|