diff --git a/src/component/Admin/Settings/Email/EmailTemplateEditor.tsx b/src/component/Admin/Settings/Email/EmailTemplateEditor.tsx index 1fadcb4..24e2e60 100644 --- a/src/component/Admin/Settings/Email/EmailTemplateEditor.tsx +++ b/src/component/Admin/Settings/Email/EmailTemplateEditor.tsx @@ -11,6 +11,8 @@ import { Typography, useTheme, } from "@mui/material"; +import { useDrag, useDrop, DndProvider } from "react-dnd"; +import { HTML5Backend } from "react-dnd-html5-backend"; import React, { lazy, Suspense, useCallback, useEffect, useRef, useState } from "react"; import { Trans, useTranslation } from "react-i18next"; import { languages } from "../../../../i18n.ts"; @@ -80,6 +82,55 @@ const EmailTemplateEditor: React.FC = ({ value, onChan setCurrentTab(newValue); }; + const lastHoverIndexRef = React.useRef(null); + const DraggableTab = ({ index, label }: { index: number; label: string }) => { + const ref = React.useRef(null); + const [, drop] = useDrop({ + accept: "template-tab", + hover: () => { + lastHoverIndexRef.current = index; + }, + }); + const [{ isDragging }, drag] = useDrag({ + type: "template-tab", + item: { index }, + collect: (monitor) => ({ + isDragging: monitor.isDragging(), + }), + end: (item) => { + if ( + typeof item.index === "number" && + typeof lastHoverIndexRef.current === "number" && + item.index !== lastHoverIndexRef.current + ) { + updateOrder(item.index, lastHoverIndexRef.current); + } + lastHoverIndexRef.current = null; + }, + }); + + drag(drop(ref)); + return ( +
setCurrentTab(index)} + > + +
+ ); + }; + + const updateOrder = (from: number, to: number) => { + if (from === to) return; + + isUpdatingFromProp.current = false; // Ensure this is a user interaction + const updatedTemplates = [...templates]; + updatedTemplates.splice(to, 0, updatedTemplates.splice(from, 1)[0]); + setTemplates(updatedTemplates); + setCurrentTab(to); + }; + const updateTemplate = (index: number, field: keyof TemplateItem, newValue: string) => { isUpdatingFromProp.current = false; // Ensure this is a user interaction const updatedTemplates = [...templates]; @@ -122,118 +173,120 @@ const EmailTemplateEditor: React.FC = ({ value, onChan }, []); return ( - - - - {templates.map((template, index) => { - const lang = languages.find((l) => l.code === template.language); - return ; - })} - - - - {templates.map((template, index) => ( - + + ]} + /> + + + )} + + ))} + {/* Add Language Dialog */} + setAddLanguageOpen(false), + }} + > + + + + setNewLanguageCode(e.target.value as string)}> + {languages.map((l) => ( + + + {l.displayName} + + + ))} + + {t("settings.languageCodeDes")} + + + + + setMagicVarOpen(false)} /> + + ); };