From 7a8d91ec9dfa12f658d323bc661ab8d5f31d01f2 Mon Sep 17 00:00:00 2001
From: Orkhan Ashrafov <70017511+webofpies@users.noreply.github.com>
Date: Tue, 4 Feb 2025 13:15:59 +0400
Subject: [PATCH] refactor: languageform
---
.../components/LanguageForm/LanguageForm.jsx | 83 +++----------------
.../components/DictionaryBars.jsx | 58 +++++++++++++
.../components/InsertDictionaryButton.jsx | 24 ++++++
3 files changed, 93 insertions(+), 72 deletions(-)
create mode 100644 frontend/src/features/language/components/LanguageForm/components/DictionaryBars.jsx
create mode 100644 frontend/src/features/language/components/LanguageForm/components/InsertDictionaryButton.jsx
diff --git a/frontend/src/features/language/components/LanguageForm/LanguageForm.jsx b/frontend/src/features/language/components/LanguageForm/LanguageForm.jsx
index eb3923a2..b8b00e7d 100644
--- a/frontend/src/features/language/components/LanguageForm/LanguageForm.jsx
+++ b/frontend/src/features/language/components/LanguageForm/LanguageForm.jsx
@@ -3,16 +3,13 @@ import { useQuery } from "@tanstack/react-query";
import { useLocation, useSearchParams } from "react-router-dom";
import { useForm } from "@mantine/form";
import { randomId } from "@mantine/hooks";
-import { DragDropContext, Draggable, Droppable } from "@hello-pangea/dnd";
import {
- ActionIcon,
Box,
Checkbox,
Divider,
Fieldset,
Group,
LoadingOverlay,
- ScrollArea,
Select,
TextInput,
} from "@mantine/core";
@@ -21,13 +18,13 @@ import {
IconAlt,
IconAnalyzeFilled,
IconCut,
- IconSquareRoundedPlusFilled,
} from "@tabler/icons-react";
+import FormButtons from "@common/FormButtons/FormButtons";
import LanguageSelect from "./LanguageSelect";
-import DictionaryBar from "./components/DictionaryBar/DictionaryBar";
import LanguageCards from "../LanguageCards/LanguageCards";
-import FormButtons from "@common/FormButtons/FormButtons";
+import DictionaryBars from "./components/DictionaryBars";
import LanguageRadioLabel from "./components/LanguageRadioLabel";
+import InsertDictionaryButton from "./components/InsertDictionaryButton";
import {
parsersQuery,
predefinedListQuery,
@@ -117,58 +114,23 @@ function LanguageForm() {
legend: { fontWeight: 500 },
}}>
-
- form.insertListItem("dictionaries", {
- for: "terms",
- type: "embedded",
- url: "",
- active: false,
- key: randomId(),
- })
- }>
-
-
-
-
-
-
- {(provided) => (
-
- {form.getValues().dictionaries.map((dict, index) => (
-
- {(provided) => (
-
- )}
-
- ))}
- {provided.placeholder}
-
- )}
-
-
-
+
+
+
+
+
+
}
key={form.key("character_substitutions")}
{...form.getInputProps("character_substitutions")}
/>
+
+
({ ...dict, key: randomId() }));
form.setFieldValue("dictionaries", dicts);
}
-
- function onDragEnd(result) {
- if (!result.destination) {
- return;
- }
-
- if (result.destination.index === result.source.index) {
- return;
- }
-
- const reordered = reorderList(
- form.getValues().dictionaries,
- result.source.index,
- result.destination.index
- );
-
- form.setFieldValue("dictionaries", reordered);
- }
-}
-
-function reorderList(list, startIndex, endIndex) {
- const result = list;
- const [removed] = result.splice(startIndex, 1);
- result.splice(endIndex, 0, removed);
-
- return result;
}
export default LanguageForm;
diff --git a/frontend/src/features/language/components/LanguageForm/components/DictionaryBars.jsx b/frontend/src/features/language/components/LanguageForm/components/DictionaryBars.jsx
new file mode 100644
index 00000000..2412375e
--- /dev/null
+++ b/frontend/src/features/language/components/LanguageForm/components/DictionaryBars.jsx
@@ -0,0 +1,58 @@
+import { ScrollArea } from "@mantine/core";
+import { DragDropContext, Draggable, Droppable } from "@hello-pangea/dnd";
+import DictionaryBar from "./DictionaryBar/DictionaryBar";
+
+function DictionaryBars({ form }) {
+ function reorderList(list, startIndex, endIndex) {
+ const result = list;
+ const [removed] = result.splice(startIndex, 1);
+ result.splice(endIndex, 0, removed);
+
+ return result;
+ }
+
+ function onDragEnd(result) {
+ if (!result.destination) {
+ return;
+ }
+
+ if (result.destination.index === result.source.index) {
+ return;
+ }
+
+ const reordered = reorderList(
+ form.getValues().dictionaries,
+ result.source.index,
+ result.destination.index
+ );
+
+ form.setFieldValue("dictionaries", reordered);
+ }
+
+ return (
+
+
+
+ {(provided) => (
+
+ {form.getValues().dictionaries.map((dict, index) => (
+
+ {(provided) => (
+
+ )}
+
+ ))}
+ {provided.placeholder}
+
+ )}
+
+
+
+ );
+}
+
+export default DictionaryBars;
diff --git a/frontend/src/features/language/components/LanguageForm/components/InsertDictionaryButton.jsx b/frontend/src/features/language/components/LanguageForm/components/InsertDictionaryButton.jsx
new file mode 100644
index 00000000..527b5d3f
--- /dev/null
+++ b/frontend/src/features/language/components/LanguageForm/components/InsertDictionaryButton.jsx
@@ -0,0 +1,24 @@
+import { ActionIcon } from "@mantine/core";
+import { randomId } from "@mantine/hooks";
+import { IconSquareRoundedPlusFilled } from "@tabler/icons-react";
+
+function InsertDictionaryButton({ form }) {
+ return (
+
+ form.insertListItem("dictionaries", {
+ for: "terms",
+ type: "embedded",
+ url: "",
+ active: false,
+ key: randomId(),
+ })
+ }>
+
+
+ );
+}
+
+export default InsertDictionaryButton;