shoc-frontend-new/src/app/(protected)/settings/task-templates.tsx
npal 86e43b66f4 fix: reset add-item draft on template selection change (SH-378)
TaskTemplateItemsField owns newItemText locally but stays mounted
across selectTemplate/startNewTemplate (which only call form.reset()),
so a typed draft survived switching templates - the base page cleared
this draft explicitly in both handleSelect and handleNew.

Remount TaskTemplateItemsField on selection change via key={selectedId}
instead of lifting the draft into the editor hook: it's transient
input-only state, not form data, so this keeps the fix local and lets
React's own remount semantics reset it. Confirmed the two new
regression tests fail without the key and pass with it.
2026-09-21 20:04:33 -05:00

69 lines
2.2 KiB
TypeScript

import { Alert, Box, Stack, Typography } from "@mui/material";
import { SettingsNav } from "@/components/common/settings-nav";
import { TASK_TEMPLATE_COPY } from "@/domain/settings/task-templates/constants/task-template-constants";
import { useTaskTemplateEditor } from "@/domain/settings/task-templates/use-cases/use-task-template-editor";
import { DeleteTaskTemplateDialog } from "./_components/task-templates/delete-task-template-dialog";
import { TaskTemplateDetailForm } from "./_components/task-templates/task-template-detail-form";
import { TaskTemplateList } from "./_components/task-templates/task-template-list";
export default function TaskTemplatesPage() {
const {
templatesQuery,
templates,
selectedId,
isNew,
isSaving,
form,
deleteConfirmOpen,
templateName,
selectTemplate,
startNewTemplate,
submitForm,
requestDelete,
cancelDelete,
confirmDelete,
} = useTaskTemplateEditor();
const { isLoading, error } = templatesQuery;
return (
<Box className="p-4">
<SettingsNav />
<Stack spacing={1} className="mb-4">
<Typography variant="h5">{TASK_TEMPLATE_COPY.pageTitle}</Typography>
<Typography variant="body2" sx={{ color: "text.secondary" }}>
{TASK_TEMPLATE_COPY.pageSubtitle}
</Typography>
</Stack>
{Boolean(error) && (
<Alert severity="error" className="mb-4">
{error instanceof Error ? error.message : TASK_TEMPLATE_COPY.loadError}
</Alert>
)}
<Box className="grid grid-cols-1 gap-4 md:grid-cols-[280px_1fr]">
<TaskTemplateList
isLoading={isLoading}
selectedId={selectedId}
templates={templates}
onNew={startNewTemplate}
onSelect={selectTemplate}
/>
<TaskTemplateDetailForm
form={form}
isNew={isNew}
isSelected={selectedId !== null}
isSaving={isSaving}
selectedId={selectedId}
onDelete={requestDelete}
onSubmit={submitForm}
/>
</Box>
<DeleteTaskTemplateDialog
open={deleteConfirmOpen}
templateName={templateName}
onCancel={cancelDelete}
onConfirm={confirmDelete}
/>
</Box>
);
}