Skip to content

Commit

Permalink
Use Zustand for IsFormDirty State
Browse files Browse the repository at this point in the history
  • Loading branch information
MiraGeowerkstatt committed Jan 23, 2025
1 parent 59d7289 commit 0bd24ee
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 39 deletions.
31 changes: 30 additions & 1 deletion src/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"redux-thunk": "^2.4.2",
"semantic-ui-css": "github:Semantic-Org/Semantic-UI-CSS",
"semantic-ui-react": "^2.1.4",
"styled-components": "^6.1.1"
"styled-components": "^6.1.1",
"zustand": "^5.0.3"
},
"devDependencies": {
"@babel/plugin-transform-private-property-in-object": "^7.24.7",
Expand Down
4 changes: 2 additions & 2 deletions src/client/src/pages/detail/detailHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ExportDialog } from "../../components/export/exportDialog.tsx";
import DateText from "../../components/legacyComponents/dateText";
import { PromptContext } from "../../components/prompt/promptContext.tsx";
import { DetailHeaderStack } from "../../components/styledComponents.ts";
import { useFormDirty } from "./useFormDirty.tsx";
import { useFormDirtyStore } from "./formDirtyStore.ts";

interface DetailHeaderProps {
editingEnabled: boolean;
Expand All @@ -40,7 +40,7 @@ const DetailHeader = ({
const dispatch = useDispatch();
const { t } = useTranslation();
const { showPrompt } = useContext(PromptContext);
const { isFormDirty } = useFormDirty();
const { isFormDirty } = useFormDirtyStore();
const auth = useAuth();

const toggleEditing = (editing: boolean) => {
Expand Down
5 changes: 2 additions & 3 deletions src/client/src/pages/detail/detailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { DetailPageContent } from "./detailPageContent.tsx";
import { DetailSideNav } from "./detailSideNav.tsx";
import { BoreholeFormInputs } from "./form/borehole/boreholePanelInterfaces.ts";
import { LocationFormInputs, LocationFormSubmission } from "./form/location/locationPanelInterfaces.tsx";
import { FormDirtyProvider } from "./formDirtyContext.tsx";
import { useLabelingContext } from "./labeling/labelingInterfaces.tsx";
import LabelingPanel from "./labeling/labelingPanel.tsx";
import { SaveBar } from "./saveBar";
Expand Down Expand Up @@ -128,7 +127,7 @@ export const DetailPage: FC = () => {
(location.pathname.endsWith("/borehole") && location.hash === "#general");

return (
<FormDirtyProvider>
<>
<DetailHeader
borehole={borehole}
editingEnabled={editingEnabled}
Expand Down Expand Up @@ -177,6 +176,6 @@ export const DetailPage: FC = () => {
{editingEnabled && shouldShowSaveBar && <SaveBar triggerSubmit={triggerSubmit} triggerReset={triggerReset} />}
</Stack>
</LayoutBox>
</FormDirtyProvider>
</>
);
};
4 changes: 2 additions & 2 deletions src/client/src/pages/detail/form/borehole/sectionInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import { DataCardButtonContainer } from "../../../../components/dataCard/dataCar
import { DataCardContext } from "../../../../components/dataCard/dataCardContext.jsx";
import { FormCheckbox, FormContainer, FormInput, FormSelect, FormValueType } from "../../../../components/form/form";
import { FormDomainSelect } from "../../../../components/form/formDomainSelect";
import { useFormDirty } from "../../useFormDirty";
import { useFormDirtyStore } from "../../formDirtyStore.ts";
import { useSaveOnCtrlS } from "../../useSaveOnCtrlS";

const SectionInput = ({ item, parentId }) => {
const { triggerReload, selectCard } = useContext(DataCardContext);
const { data: domains } = useDomains();
const { i18n } = useTranslation();
const { setIsFormDirty } = useFormDirty();
const { setIsFormDirty } = useFormDirtyStore();

const sectionElementDefaults = {
fromDepth: null,
Expand Down
4 changes: 2 additions & 2 deletions src/client/src/pages/detail/form/useFormWithSaveBar.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ForwardedRef, useCallback, useEffect, useImperativeHandle } from "react";
import { FieldValues, UseFormReturn } from "react-hook-form";
import { useHistory } from "react-router-dom";
import { useFormDirtyStore } from "../formDirtyStore.ts";
import { useBlockNavigation } from "../useBlockNavigation.tsx";
import { useFormDirty } from "../useFormDirty.tsx";
import { useSaveOnCtrlS } from "../useSaveOnCtrlS.ts";

interface UseFormWithSaveBarProps<T extends FieldValues> {
Expand All @@ -20,7 +20,7 @@ export function UseFormWithSaveBar<T extends FieldValues>({
}: UseFormWithSaveBarProps<T>) {
const history = useHistory();
const { handleBlockedNavigation } = useBlockNavigation();
const { setIsFormDirty } = useFormDirty();
const { setIsFormDirty } = useFormDirtyStore();

// Block navigation if form is dirty
history.block(nextLocation => {
Expand Down
14 changes: 0 additions & 14 deletions src/client/src/pages/detail/formDirtyContext.tsx

This file was deleted.

10 changes: 10 additions & 0 deletions src/client/src/pages/detail/formDirtyStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { create } from "zustand";

interface FormDirtyState {
isFormDirty: boolean;
setIsFormDirty: (newState: boolean) => void;
}
export const useFormDirtyStore = create<FormDirtyState>(set => ({
isFormDirty: false,
setIsFormDirty: newState => set(() => ({ isFormDirty: newState })),
}));
4 changes: 2 additions & 2 deletions src/client/src/pages/detail/saveBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Box, Stack } from "@mui/material";
import { CircleCheck, CircleX } from "lucide-react";
import { theme } from "../../AppTheme.ts";
import { DeleteButton, SaveButton } from "../../components/buttons/buttons.tsx";
import { useFormDirty } from "./useFormDirty.tsx";
import { useFormDirtyStore } from "./formDirtyStore.ts";

interface SaveBarProps {
triggerSubmit: () => void;
Expand All @@ -15,7 +15,7 @@ export const SaveBar = ({ triggerSubmit, triggerReset }: SaveBarProps) => {
const [showSaveFeedback, setShowSaveFeedback] = useState(false);
const { t } = useTranslation();
const location = useLocation();
const { isFormDirty } = useFormDirty();
const { isFormDirty } = useFormDirtyStore();

useEffect(() => {
setShowSaveFeedback(false);
Expand Down
5 changes: 3 additions & 2 deletions src/client/src/pages/detail/useBlockNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import { Trash2, X } from "lucide-react";
import { PromptContext } from "../../components/prompt/promptContext.tsx";
import { useFormDirty } from "./useFormDirty.tsx";
import { useFormDirtyStore } from "./formDirtyStore.ts";

interface UseBlockNavigationResult {
handleBlockedNavigation: (nextLocation: string) => boolean;
Expand All @@ -12,7 +12,8 @@ interface UseBlockNavigationResult {
export const useBlockNavigation = (): UseBlockNavigationResult => {
const [nextLocation, setNextLocation] = useState<string | null>(null);
const [confirmedNavigation, setConfirmedNavigation] = useState(false);
const { isFormDirty } = useFormDirty();
const { isFormDirty } = useFormDirtyStore();

const { showPrompt } = useContext(PromptContext);
const { t } = useTranslation();
const history = useHistory();
Expand Down
10 changes: 0 additions & 10 deletions src/client/src/pages/detail/useFormDirty.tsx

This file was deleted.

0 comments on commit 0bd24ee

Please sign in to comment.