Skip to content

Commit b1a3c30

Browse files
committed
feat(dialog): add editing mode to CreateSectionDialog
1 parent 3da55bb commit b1a3c30

3 files changed

Lines changed: 60 additions & 6 deletions

File tree

apps/web/src/components/views/dialogs/CreateSectionDialog.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import DialogButtons from "../elements/DialogButtons";
1414
import { _t } from "../../../languageHandler";
1515

1616
interface CreateSectionDialogProps {
17+
/**
18+
* Whether this dialog is being used to edit an existing section or create a new one
19+
*/
20+
sectionToEdit?: string;
21+
1722
/**
1823
* Callback called when the dialog is closed.
1924
* @param shouldCreateSection Whether a section should be created or not. This will be false if the user cancels the dialog.
@@ -25,15 +30,16 @@ interface CreateSectionDialogProps {
2530
/**
2631
* Dialog shown to the user to create a new section in the room list.
2732
*/
28-
export function CreateSectionDialog({ onFinished }: CreateSectionDialogProps): JSX.Element {
29-
const [value, setValue] = useState("");
33+
export function CreateSectionDialog({ onFinished, sectionToEdit }: CreateSectionDialogProps): JSX.Element {
34+
const isEdition = Boolean(sectionToEdit);
35+
const [value, setValue] = useState(sectionToEdit ?? "");
3036
const isInvalid = Boolean(value.trim().length === 0);
3137

3238
return (
3339
<BaseDialog
3440
className="mx_CreateSectionDialog"
3541
onFinished={() => onFinished(false, value)}
36-
title={_t("create_section_dialog|title")}
42+
title={isEdition ? _t("create_section_dialog|title_edition") : _t("create_section_dialog|title")}
3743
hasCancel={true}
3844
>
3945
<Flex gap="var(--cpd-space-6x)" direction="column" className="mx_CreateSectionDialog_content">
@@ -49,12 +55,18 @@ export function CreateSectionDialog({ onFinished }: CreateSectionDialogProps): J
4955
>
5056
<Form.Field name="sectionName">
5157
<Form.Label> {_t("create_section_dialog|label")}</Form.Label>
52-
<Form.TextControl onChange={(evt) => setValue(evt.target.value)} required={true} />
58+
<Form.TextControl
59+
value={value}
60+
onChange={(evt) => setValue(evt.target.value)}
61+
required={true}
62+
/>
5363
</Form.Field>
5464
</Form.Root>
5565
</Flex>
5666
<DialogButtons
57-
primaryButton={_t("create_section_dialog|create_section")}
67+
primaryButton={
68+
isEdition ? _t("create_section_dialog|edit_section") : _t("create_section_dialog|create_section")
69+
}
5870
primaryDisabled={isInvalid}
5971
hasCancel={true}
6072
onCancel={() => onFinished(false, "")}

apps/web/src/i18n/strings/en_EN.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,10 @@
684684
"create_section_dialog": {
685685
"create_section": "Create section",
686686
"description": "Sections are only for you",
687+
"edit_section": "Edit section",
687688
"label": "Section name",
688-
"title": "Create a section"
689+
"title": "Create a section",
690+
"title_edition": "Edit a section"
689691
},
690692
"create_space": {
691693
"add_details_prompt": "Add some details to help people recognise it.",
@@ -1855,6 +1857,12 @@
18551857
"ongoing": "Removing…",
18561858
"reason_label": "Reason (optional)"
18571859
},
1860+
"remove_section_dialog": {
1861+
"confirmation": "Are you sure you want to remove this section? ",
1862+
"description": "The chats in this section will still be available in your chats list.",
1863+
"remove_section": "Remove section",
1864+
"title_edition": "Remove section?"
1865+
},
18581866
"report_content": {
18591867
"description": "Reporting this message will send its unique 'event ID' to the administrator of your homeserver. If messages in this room are encrypted, your homeserver administrator will not be able to read the message text or view any files or images.",
18601868
"disagree": "Disagree",

apps/web/test/unit-tests/components/views/dialogs/CreateSectionDialog-test.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,38 @@ describe("CreateSectionDialog", () => {
5656
await userEvent.keyboard("{Enter}");
5757
expect(onFinished).toHaveBeenCalledWith(true, "My section");
5858
});
59+
60+
describe("editing mode", () => {
61+
function renderEditComponent(): void {
62+
render(<CreateSectionDialog onFinished={onFinished} sectionToEdit="Existing Section" />);
63+
}
64+
65+
it("pre-fills the input with the existing section name", () => {
66+
renderEditComponent();
67+
const input = screen.getByRole("textbox");
68+
expect(input).toHaveValue("Existing Section");
69+
});
70+
71+
it("shows the edit section button instead of create section", () => {
72+
renderEditComponent();
73+
expect(screen.getByRole("button", { name: "Edit section" })).toBeInTheDocument();
74+
expect(screen.queryByRole("button", { name: "Create section" })).not.toBeInTheDocument();
75+
});
76+
77+
it("calls onFinished with the updated name when edit section is clicked", async () => {
78+
renderEditComponent();
79+
const input = screen.getByRole("textbox");
80+
await userEvent.clear(input);
81+
await userEvent.type(input, "Updated Section");
82+
await userEvent.click(screen.getByRole("button", { name: "Edit section" }));
83+
expect(onFinished).toHaveBeenCalledWith(true, "Updated Section");
84+
});
85+
86+
it("has the edit section button disabled when the input is empty", async () => {
87+
renderEditComponent();
88+
const input = screen.getByRole("textbox");
89+
await userEvent.clear(input);
90+
expect(screen.getByRole("button", { name: "Edit section" })).toBeDisabled();
91+
});
92+
});
5993
});

0 commit comments

Comments
 (0)