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({}); const checks = useMemo(() => Object.keys(checklist), [checklist]); const hasChecks = useMemo(() => checks.length > 0, [checks.length]); const buildDeleteDialogProps = useCallback( ({ confirmDialogProps = {}, formSummaryProps = {}, getConfirmDialogTitle, }) => ({ actionProceedText: 'Delete', content: ( ), proceedColour: 'red', titleText: getConfirmDialogTitle(checks.length), ...confirmDialogProps, }), [checklist, checks.length], ); const getCheck = useCallback( (key) => checklist[key], [checklist], ); const setCheck = useCallback( (key, checked) => setChecklist(buildObjectStateSetterCallback(key, checked || undefined)), [], ); return { buildDeleteDialogProps, checklist, checks, getCheck, hasChecks, setCheck, }; }; export default useChecklist;