refactor(editor): Task 2 round 2, locals and line limits
This commit is contained in:
1 parent
b703333b20
commit
20905adf72
10 files changed
+258
-168
No files matched your search
@@ -16,7 +16,7 @@ import {
|
||||
} from '@/editor/findings/tree'
|
||||
import type { IdPrefix } from '@/editor/ids'
|
||||
import { isBlank, useSheetDocument } from '@/editor/sheetDocument'
|
||||
import { useEditCommand } from '@/editor/useEditCommand'
|
||||
import { useEditCommand, type EditCommandApi } from '@/editor/useEditCommand'
|
||||
import type { Guards } from '@/editor/useEditorNavigation'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
@@ -60,26 +60,36 @@ function isComplete(draft: Draft, pending: PendingFinding, parent: unknown) {
|
||||
return pending.kind !== 'category' || (parent !== null && draft.kind !== '')
|
||||
}
|
||||
|
||||
/** The one `insert`, under a fresh id; the new id, or null when the core refused it. */
|
||||
function insertNew(
|
||||
edit: EditCommandApi,
|
||||
pending: PendingFinding,
|
||||
parent: FindingItem | null,
|
||||
draft: Draft,
|
||||
): string | null {
|
||||
let createdId = ''
|
||||
const name = { fr: draft.fr.trim(), en: draft.en.trim() }
|
||||
const added = edit.runNew('form', PREFIX[pending.kind], (id) => {
|
||||
createdId = id
|
||||
return insertCommand({ ...pending, parent }, id, name, draft.kind)
|
||||
})
|
||||
return added ? createdId : null
|
||||
}
|
||||
|
||||
/** The form's state and its one `insert` (« Créer »), refused on a missing field. */
|
||||
function useCreateForm(props: FindingCreateFormProps) {
|
||||
const { pending, guards, onCreated } = props
|
||||
const edit = useEditCommand()
|
||||
const zones = useZoneChoice()
|
||||
const form = useFormDraft(EMPTY, guards, sameDraft)
|
||||
const form = useFormDraft(EMPTY, props.guards, sameDraft)
|
||||
const create = () => {
|
||||
const { pending } = props
|
||||
const parent = pending.kind === 'category' ? zones.chosen : pending.parent
|
||||
if (!isComplete(form.draft, pending, parent)) {
|
||||
edit.reject('form', 'editor.findings.required')
|
||||
return
|
||||
}
|
||||
const { fr, en, kind } = form.draft
|
||||
let createdId = ''
|
||||
const added = edit.runNew('form', PREFIX[pending.kind], (id) => {
|
||||
createdId = id
|
||||
const name = { fr: fr.trim(), en: en.trim() }
|
||||
return insertCommand({ ...pending, parent }, id, name, kind)
|
||||
})
|
||||
if (added) onCreated(createdId, parent)
|
||||
const id = insertNew(edit, pending, parent, form.draft)
|
||||
if (id !== null) props.onCreated(id, parent)
|
||||
}
|
||||
return { ...form, zones, create, error: edit.fieldError('form') }
|
||||
}
|
||||
@@ -129,11 +139,9 @@ function CreateFields(props: {
|
||||
* is written before « Créer », a close or a move never creates (R2F-3).
|
||||
*/
|
||||
export default function FindingCreateForm(props: FindingCreateFormProps) {
|
||||
const { pending, readOnly, onClose } = props
|
||||
const t = useT()
|
||||
const form = useCreateForm(props)
|
||||
const { guard } = form
|
||||
const category = pending.kind === 'category'
|
||||
const category = props.pending.kind === 'category'
|
||||
return (
|
||||
<form
|
||||
data-draft-form
|
||||
@@ -144,21 +152,21 @@ export default function FindingCreateForm(props: FindingCreateFormProps) {
|
||||
form.create()
|
||||
}}
|
||||
>
|
||||
{readOnly && <ReadOnlyNotice />}
|
||||
{props.readOnly && <ReadOnlyNotice />}
|
||||
{category && form.zones.chosen && <ZoneTitle zone={form.zones.chosen} />}
|
||||
<CreateFields form={form} category={category} readOnly={readOnly} />
|
||||
<CreateFields form={form} category={category} readOnly={props.readOnly} />
|
||||
{form.error && (
|
||||
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
|
||||
{form.error}
|
||||
</p>
|
||||
)}
|
||||
<FormActions
|
||||
confirming={guard.confirming}
|
||||
readOnly={readOnly}
|
||||
confirming={form.guard.confirming}
|
||||
readOnly={props.readOnly}
|
||||
submitLabel={t('editor.findings.create')}
|
||||
onCancel={() => onClose(readOnly)}
|
||||
onKeepTyping={guard.keepTyping}
|
||||
onDiscard={() => onClose(true)}
|
||||
onCancel={() => props.onClose(props.readOnly)}
|
||||
onKeepTyping={form.guard.keepTyping}
|
||||
onDiscard={() => props.onClose(true)}
|
||||
/>
|
||||
</form>
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user