@foodpilot/foods 2.11.37 → 2.11.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,2 @@
1
- import { ConfirmOptionsType } from './FoodsFormBase';
2
- type ConfirmDialogProps = {
3
- open: boolean;
4
- onClose: () => void;
5
- confirmOptions: ConfirmOptionsType;
6
- };
7
- export declare const ConfirmDialog: (props: ConfirmDialogProps) => import("react/jsx-runtime").JSX.Element;
8
- export {};
1
+ export declare const ConfirmDialog: () => import("react/jsx-runtime").JSX.Element | null;
2
+ export declare const ConfirmDialogInner: () => import("react/jsx-runtime").JSX.Element;
@@ -1,24 +1,43 @@
1
- import { jsxs, jsx } from "react/jsx-runtime";
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { useTheme, Dialog, DialogTitle, Box, Typography, Button, Stack } from "@mui/material";
3
3
  import { ReactComponent as SvgBlackCross } from "../../../assets/icons/SsqTitleIcon/black-cross.svg.js";
4
- const ConfirmDialog = (props) => {
4
+ import { useFormBaseContext } from "./FormBaseContext.js";
5
+ import { useEffect } from "react";
6
+ const ConfirmDialog = () => {
7
+ const { confirmExit } = useFormBaseContext();
8
+ if (confirmExit === false) {
9
+ return null;
10
+ }
11
+ return /* @__PURE__ */ jsx(ConfirmDialogInner, {});
12
+ };
13
+ const ConfirmDialogInner = () => {
5
14
  const theme = useTheme();
6
- const { onClose, open, confirmOptions } = props;
15
+ const { isConfirmOpen, setIsConfirmOpen, formSaved, confirmOptions } = useFormBaseContext();
16
+ const listener = (ev) => {
17
+ ev.preventDefault();
18
+ };
19
+ useEffect(() => {
20
+ window.removeEventListener("beforeunload", listener);
21
+ formSaved === false && window.addEventListener("beforeunload", listener);
22
+ return () => {
23
+ window.removeEventListener("beforeunload", listener);
24
+ };
25
+ }, [formSaved]);
7
26
  const handleClickValidate = () => {
8
27
  confirmOptions.onConfirm();
9
- onClose();
28
+ setIsConfirmOpen(false);
10
29
  };
11
30
  const handleClickCancel = () => {
12
31
  confirmOptions.onCancel();
13
- onClose();
32
+ setIsConfirmOpen(false);
14
33
  };
15
34
  return /* @__PURE__ */ jsxs(
16
35
  Dialog,
17
36
  {
18
37
  fullWidth: true,
19
38
  maxWidth: "xs",
20
- open,
21
- onClose,
39
+ open: isConfirmOpen,
40
+ onClose: () => setIsConfirmOpen(false),
22
41
  sx: {
23
42
  ".MuiPaper-rounded": {
24
43
  borderRadius: "12px"
@@ -43,7 +62,7 @@ const ConfirmDialog = (props) => {
43
62
  },
44
63
  children: [
45
64
  /* @__PURE__ */ jsx(Typography, { variant: "h3", children: confirmOptions.titleText }),
46
- /* @__PURE__ */ jsx(Button, { variant: "text", endIcon: /* @__PURE__ */ jsx(SvgBlackCross, {}), onClick: onClose })
65
+ /* @__PURE__ */ jsx(Button, { variant: "text", endIcon: /* @__PURE__ */ jsx(SvgBlackCross, {}), onClick: () => setIsConfirmOpen(false) })
47
66
  ]
48
67
  }
49
68
  ),
@@ -60,5 +79,6 @@ const ConfirmDialog = (props) => {
60
79
  );
61
80
  };
62
81
  export {
63
- ConfirmDialog
82
+ ConfirmDialog,
83
+ ConfirmDialogInner
64
84
  };
@@ -6,21 +6,21 @@ export type FoodsPage = {
6
6
  content: ReactNode;
7
7
  };
8
8
  export type DisplayTextOptions = {
9
- title: string;
9
+ title: ReactNode;
10
10
  subtitle?: ReactNode;
11
- nextPage: string;
12
- previousPage: string;
13
- finalPage?: string;
14
- syncingMessage?: string;
15
- savingDefaultMessage?: string;
16
- errorDefaultMessage?: string;
17
- warningText?: string;
11
+ nextPage: ReactNode;
12
+ previousPage: ReactNode;
13
+ finalPage?: ReactNode;
14
+ syncingMessage?: ReactNode;
15
+ savingDefaultMessage?: ReactNode;
16
+ errorDefaultMessage?: ReactNode;
17
+ warningText?: ReactNode;
18
18
  };
19
19
  export type ConfirmOptionsType = {
20
- titleText: string;
21
- infoText?: string;
22
- confirmText: string;
23
- cancelText: string;
20
+ titleText: ReactNode;
21
+ infoText?: ReactNode;
22
+ confirmText: ReactNode;
23
+ cancelText: ReactNode;
24
24
  onConfirm: () => void;
25
25
  onCancel: () => void;
26
26
  };
@@ -1,5 +1,5 @@
1
1
  import { jsxs, jsx, Fragment } from "react/jsx-runtime";
2
- import { useState, useEffect } from "react";
2
+ import { useEffect } from "react";
3
3
  import { FormBaseContextProvider, useFormBaseContext } from "./FormBaseContext.js";
4
4
  import { FormTopBar } from "./FormTopBar/FormTopBar.js";
5
5
  import { MainPageLayout } from "./MainPageLayout.js";
@@ -25,18 +25,10 @@ const _FoodsFormBase = (props) => {
25
25
  ] });
26
26
  };
27
27
  const FoodsFormBase = (props) => {
28
- const [openConfirm, setOpenConfirm] = useState(false);
29
28
  const children = props.children;
30
- return /* @__PURE__ */ jsxs(FormBaseContextProvider, { ...props, openConfirmDialog: () => setOpenConfirm(true), children: [
29
+ return /* @__PURE__ */ jsxs(FormBaseContextProvider, { ...props, children: [
31
30
  /* @__PURE__ */ jsx(_FoodsFormBase, { children }),
32
- props.confirmExit && /* @__PURE__ */ jsx(
33
- ConfirmDialog,
34
- {
35
- open: openConfirm,
36
- onClose: () => setOpenConfirm(false),
37
- confirmOptions: props.confirmOptions
38
- }
39
- )
31
+ /* @__PURE__ */ jsx(ConfirmDialog, {})
40
32
  ] });
41
33
  };
42
34
  export {
@@ -2,6 +2,19 @@ import { ReactNode } from 'react';
2
2
  import { FoodsPage, FoodsFormBaseProps } from './FoodsFormBase';
3
3
  import { PageLocks } from './usePageLock';
4
4
  export type FormBaseVariant = "outlined" | "full-width";
5
+ export type ConfirmOptionsType = {
6
+ titleText: ReactNode;
7
+ infoText?: ReactNode;
8
+ confirmText: ReactNode;
9
+ cancelText: ReactNode;
10
+ onConfirm: () => void;
11
+ onCancel: () => void;
12
+ };
13
+ export type ConfirmOptionsProps = {
14
+ confirmExit: false;
15
+ } | ({
16
+ confirmExit: true;
17
+ } & ConfirmOptionsType);
5
18
  type FormBasePropsBlackList = Omit<FoodsFormBaseProps, "startingPage" | "variant">;
6
19
  type ExtraFormOptions = {
7
20
  isSaving: boolean;
@@ -12,14 +25,18 @@ type ExtraFormOptions = {
12
25
  maxPage: number;
13
26
  formSaved: boolean;
14
27
  setFormSaved: (isSaved: boolean) => void;
15
- openConfirmDialog: () => void;
28
+ isConfirmOpen: boolean;
29
+ setIsConfirmOpen: (v: boolean) => void;
30
+ confirmOptions?: ConfirmOptionsType;
16
31
  isError: boolean;
17
32
  setError: (isError: boolean) => void;
18
33
  };
19
- type FormBaseContextInitialValues = PageLocks & FormBasePropsBlackList & ExtraFormOptions;
34
+ type FormBaseContextInitialValues = PageLocks & FormBasePropsBlackList & ExtraFormOptions & {
35
+ confirmOptions: ConfirmOptionsType;
36
+ };
20
37
  type FormBaseContextProviderProps = FoodsFormBaseProps & {
21
38
  children: ReactNode;
22
- openConfirmDialog: () => void;
39
+ openConfirmDialog?: () => void;
23
40
  };
24
41
  export declare const FormBaseContextProvider: (props: FormBaseContextProviderProps) => import("react/jsx-runtime").JSX.Element;
25
42
  export declare const useFormBaseContext: () => FormBaseContextInitialValues;
@@ -4,6 +4,11 @@ import { usePageLock } from "./usePageLock.js";
4
4
  const FormBaseContext = createContext(null);
5
5
  const FormBaseContextProvider = (props) => {
6
6
  const { children, pages = [], startingPage = 0, ...FormBaseProps } = props;
7
+ const [isConfirmOpen, _setIsConfirmOpen] = useState(false);
8
+ const setIsConfirmOpen = (v) => {
9
+ console.log("TRYING: ", { v });
10
+ _setIsConfirmOpen(v);
11
+ };
7
12
  const [isSaving, setSaving] = useState(false);
8
13
  const [isError, setError] = useState(false);
9
14
  const [selectedPageIndex, setPage] = useState(startingPage);
@@ -11,6 +16,16 @@ const FormBaseContextProvider = (props) => {
11
16
  const selectedPage = pages[selectedPageIndex] ?? null;
12
17
  const pageLock = usePageLock();
13
18
  const [formSaved, setFormSaved] = useState(false);
19
+ const confirmOptions = FormBaseProps.confirmExit ? FormBaseProps.confirmOptions : {
20
+ titleText: "",
21
+ infoText: "",
22
+ confirmText: "",
23
+ cancelText: "",
24
+ onConfirm: () => {
25
+ },
26
+ onCancel: () => {
27
+ }
28
+ };
14
29
  const items = {
15
30
  ...FormBaseProps,
16
31
  ...pageLock,
@@ -24,6 +39,9 @@ const FormBaseContextProvider = (props) => {
24
39
  setFormSaved,
25
40
  isError,
26
41
  setError,
42
+ isConfirmOpen,
43
+ setIsConfirmOpen,
44
+ confirmOptions,
27
45
  // variant,
28
46
  pages
29
47
  };
@@ -19,7 +19,7 @@ const ActionButtons = () => {
19
19
  onPageChange,
20
20
  showSync,
21
21
  setFormSaved,
22
- openConfirmDialog,
22
+ setIsConfirmOpen,
23
23
  isLoading,
24
24
  selectedPageIndex
25
25
  } = useFormBaseContext();
@@ -61,7 +61,7 @@ const ActionButtons = () => {
61
61
  Button,
62
62
  {
63
63
  variant: "white-filled",
64
- onClick: confirmExit ? openConfirmDialog : onClose,
64
+ onClick: () => confirmExit ? setIsConfirmOpen(true) : onClose(),
65
65
  sx: {
66
66
  minWidth: 0,
67
67
  aspectRatio: 1,
@@ -116,7 +116,7 @@ const ActionButtons = () => {
116
116
  Button,
117
117
  {
118
118
  variant: "white-filled",
119
- onClick: confirmExit ? openConfirmDialog : onClose,
119
+ onClick: () => confirmExit ? setIsConfirmOpen(true) : onClose(),
120
120
  sx: {
121
121
  minWidth: 0,
122
122
  aspectRatio: 1,
@@ -1,3 +1,5 @@
1
1
  export { FoodsFormBase } from './FoodsFormBase';
2
+ export { ConfirmDialog } from './ConfirmDialog';
3
+ export * from './FormTopBar';
2
4
  export type * from './FoodsFormBase';
3
5
  export { useFormBaseContext, FormBaseContextProvider } from './FormBaseContext';
@@ -1,7 +1,6 @@
1
1
  export * from './FormBase';
2
2
  export * from './FormBox';
3
3
  export * from './FormPropertyBox';
4
- export * from './FormTopBar';
5
4
  export * from './FormUnit';
6
5
  export * from './FoodsFormBase';
7
6
  export * from './FoodsFormFields';
package/dist/main.js CHANGED
@@ -65,10 +65,11 @@ import { MultiValueFilter } from "./components/Filter/MultiValueFilter.js";
65
65
  import { FormBase } from "./components/Form/FormBase.js";
66
66
  import { FormBox } from "./components/Form/FormBox.js";
67
67
  import { FormPropertyBox } from "./components/Form/FormPropertyBox.js";
68
- import { FormTopBar } from "./components/Form/FormTopBar.js";
69
68
  import { FormUnit } from "./components/Form/FormUnit.js";
70
69
  import { FoodsFormBase } from "./components/Form/FoodsFormBase/FoodsFormBase.js";
70
+ import { ConfirmDialog } from "./components/Form/FoodsFormBase/ConfirmDialog.js";
71
71
  import { FormBaseContextProvider, useFormBaseContext } from "./components/Form/FoodsFormBase/FormBaseContext.js";
72
+ import { FormTopBar } from "./components/Form/FoodsFormBase/FormTopBar/FormTopBar.js";
72
73
  import { FoodsTextInput } from "./components/Form/FoodsFormFields/FoodsTextInput.js";
73
74
  import { MRTGrid } from "./components/Grid/MRTGrid.js";
74
75
  import { EcoScoreCell } from "./components/Grid/Cell/EcoScoreCell.js";
@@ -178,6 +179,7 @@ export {
178
179
  ComparisonArrow,
179
180
  ComparisonBlock,
180
181
  ComparisonLabel,
182
+ ConfirmDialog,
181
183
  ContextualContentBox,
182
184
  CoordinatePopover,
183
185
  CustomRender,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@foodpilot/foods",
3
3
  "private": false,
4
- "version": "2.11.37",
4
+ "version": "2.11.38",
5
5
  "type": "module",
6
6
  "main": "./dist/main.js",
7
7
  "module": "./dist/main.js",