File size: 2,642 Bytes
f909d7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React from "react";
import { Button, Divider, Group, Modal, TextInput } from "@mantine/core";
import _unset from "lodash.unset";
import _update from "lodash.update";
import { VscLock } from "react-icons/vsc";
import useFile from "src/store/useFile";
import useJson from "src/store/useJson";
import useModal from "src/store/useModal";
import useUser from "src/store/useUser";

interface EditModalProps {
  opened: boolean;
  setOpened: React.Dispatch<React.SetStateAction<boolean>>;
  selectedValue: string | number | null;
  path: (string | number)[];
  value: string;
  setValue: React.Dispatch<React.SetStateAction<string>>;
  errorMessage: string | null;
  setErrorMessage: React.Dispatch<React.SetStateAction<string | null>>;
}

export const EditModal = ({
  opened,
  setOpened,
  selectedValue,
  path,
  value,
  setValue,
  errorMessage,
  setErrorMessage,
}: EditModalProps) => {
  const setContents = useFile(state => state.setContents);
  const getJson = useJson(state => state.getJson);
  const showPremiumModal = useModal(state => state.setVisible("premium"));
  const premium = useUser(state => state.premium);

  return (
    <Modal centered title={selectedValue} opened={opened} onClose={() => setOpened(false)}>
      <TextInput
        value={value}
        onChange={e => {
          setValue(e.currentTarget.value);
        }}
        error={errorMessage}
      />
      <Divider my="md" />
      <Group justify="right">
        <Button
          color="red"
          onClick={() => {
            try {
              if (!premium) return showPremiumModal(true);
              const updatedJson = JSON.parse(getJson());
              _unset(updatedJson, path);

              setContents({ contents: JSON.stringify(updatedJson, null, 2) });
              setErrorMessage(null);
              setOpened(false);
            } catch (error: any) {
              setErrorMessage(error.message);
            }
          }}
          rightSection={!premium && <VscLock />}
        >
          Delete
        </Button>
        <Button
          onClick={() => {
            try {
              if (!premium) return showPremiumModal(true);
              const updatedJson = _update(JSON.parse(getJson()), path, () => JSON.parse(value));
              setContents({ contents: JSON.stringify(updatedJson, null, 2) });

              setErrorMessage(null);
              setOpened(false);
            } catch (error: any) {
              setErrorMessage(error.message);
            }
          }}
          rightSection={!premium && <VscLock />}
        >
          Apply
        </Button>
      </Group>
    </Modal>
  );
};