Skip to content

Commit

Permalink
Fix field type not updating when enum name is changed (#193 part2)
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilit committed Jul 31, 2024
1 parent 34d9045 commit 844b241
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/components/EditorHeader/ControlPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ export default function ControlPanel({
}
} else if (a.element === ObjectType.ENUM) {
updateEnum(a.id, a.undo);
if (a.updatedFields) {
if (a.undo.name) {
a.updatedFields.forEach((x) =>
updateField(x.tid, x.fid, { type: a.undo.name.toUpperCase() }),
);
}
}
}
setRedoStack((prev) => [...prev, a]);
} else if (a.action === Action.PAN) {
Expand Down Expand Up @@ -477,6 +484,13 @@ export default function ControlPanel({
}
} else if (a.element === ObjectType.ENUM) {
updateEnum(a.id, a.redo);
if (a.updatedFields) {
if (a.redo.name) {
a.updatedFields.forEach((x) =>
updateField(x.tid, x.fid, { type: a.redo.name.toUpperCase() }),
);
}
}
}
setUndoStack((prev) => [...prev, a]);
} else if (a.action === Action.PAN) {
Expand Down
25 changes: 23 additions & 2 deletions src/components/EditorSidePanel/EnumsTab/EnumDetails.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useState } from "react";
import { Button, Input, TagInput } from "@douyinfe/semi-ui";
import { IconDeleteStroked } from "@douyinfe/semi-icons";
import { useEnums, useUndoRedo } from "../../../hooks";
import { useDiagram, useEnums, useUndoRedo } from "../../../hooks";
import { Action, ObjectType } from "../../../data/constants";
import { useTranslation } from "react-i18next";

export default function EnumDetails({ data, i }) {
const { t } = useTranslation();
const { deleteEnum, updateEnum } = useEnums();
const { tables, updateField } = useDiagram();
const { setUndoStack, setRedoStack } = useUndoRedo();
const [editField, setEditField] = useState({});

Expand All @@ -19,10 +20,29 @@ export default function EnumDetails({ data, i }) {
value={data.name}
placeholder={t("name")}
validateStatus={data.name.trim() === "" ? "error" : "default"}
onChange={(value) => updateEnum(i, { name: value })}
onChange={(value) => {
updateEnum(i, { name: value });
tables.forEach((table, i) => {
table.fields.forEach((field, j) => {
if (field.type.toLowerCase() === data.name.toLowerCase()) {
updateField(i, j, { type: value.toUpperCase() });
}
});
});
}}
onFocus={(e) => setEditField({ name: e.target.value })}
onBlur={(e) => {
if (e.target.value === editField.name) return;

const updatedFields = tables.reduce((acc, table) => {
table.fields.forEach((field, i) => {
if (field.type.toLowerCase() === data.name.toLowerCase()) {
acc.push({ tid: table.id, fid: i });
}
});
return acc;
}, []);

setUndoStack((prev) => [
...prev,
{
Expand All @@ -31,6 +51,7 @@ export default function EnumDetails({ data, i }) {
id: i,
undo: editField,
redo: { name: e.target.value },
updatedFields,
message: t("edit_enum", {
enumName: e.target.value,
extra: "[name]",
Expand Down
6 changes: 4 additions & 2 deletions src/components/EditorSidePanel/TypesTab/TypeInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ export default function TypeInfo({ index, data }) {
if (e.target.value === editField.name) return;

const updatedFields = tables.reduce((acc, table) => {
table.fields.forEach((_, i) => {
acc.push({ tid: table.id, fid: i });
table.fields.forEach((field, i) => {
if (field.type.toLowerCase() === data.name.toLowerCase()) {
acc.push({ tid: table.id, fid: i });
}
});
return acc;
}, []);
Expand Down

0 comments on commit 844b241

Please sign in to comment.