|
1 |
| -import React, { useEffect, useState } from "react"; |
2 |
| -import styled from "styled-components"; |
3 |
| -import { Modal, Typography, TextField, Box } from "@mui/material"; |
4 |
| -import { useDispatch } from "react-redux"; |
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import styled from 'styled-components'; |
| 3 | +import { Modal, Typography, TextField, Box } from '@mui/material'; |
| 4 | +import { useDispatch } from 'react-redux'; |
5 | 5 |
|
6 | 6 | // local imports
|
7 |
| -import Button from "../../../../cse-ui-kit/buttons/Button"; |
| 7 | +import Button from '../../../../cse-ui-kit/buttons/Button'; |
8 | 8 | import {
|
9 |
| - addItemAction, |
10 |
| - AddPayloadType, |
11 |
| -} from "src/packages/dashboard/state/folders/actions"; |
12 |
| -import { getFolderState } from "../../api/helpers"; |
| 9 | + addItemAction, |
| 10 | + AddPayloadType, |
| 11 | + deleteFileEntityAction, |
| 12 | + DeletePayloadType, |
| 13 | +} from 'src/packages/dashboard/state/folders/actions'; |
| 14 | +import { getFolderState } from '../../api/helpers'; |
13 | 15 |
|
14 | 16 | type Props = {
|
15 |
| - open: boolean; |
16 |
| - modalState: { open: boolean; type: string }; |
17 |
| - setModalState: (flag: { open: boolean; type: string }) => void; |
| 17 | + open: boolean; |
| 18 | + modalState: { open: boolean; selectedFile: string; type: string }; |
| 19 | + setModalState: (flag: { |
| 20 | + open: boolean; |
| 21 | + selectedFile: string; |
| 22 | + type: string; |
| 23 | + }) => void; |
18 | 24 | };
|
19 | 25 |
|
20 | 26 | const Container = styled.div`
|
21 |
| - position: absolute; |
22 |
| - top: 50%; |
23 |
| - left: 50%; |
24 |
| - transform: translate(-50%, -50%); |
| 27 | + position: absolute; |
| 28 | + top: 50%; |
| 29 | + left: 50%; |
| 30 | + transform: translate(-50%, -50%); |
25 | 31 |
|
26 |
| - width: 500px; |
27 |
| - height: 200px; |
28 |
| - background: white; |
29 |
| - border-radius: 20px; |
| 32 | + width: 500px; |
| 33 | + height: 200px; |
| 34 | + background: white; |
| 35 | + border-radius: 20px; |
30 | 36 |
|
31 |
| - display: flex; |
32 |
| - flex-direction: column; |
33 |
| - justify-content: center; |
34 |
| - align-items: center; |
35 |
| - grid-gap: 20px; |
| 37 | + display: flex; |
| 38 | + flex-direction: column; |
| 39 | + justify-content: center; |
| 40 | + align-items: center; |
| 41 | + grid-gap: 20px; |
36 | 42 | `;
|
37 | 43 |
|
38 | 44 | export default function ConfirmationWindow({
|
39 |
| - open, |
40 |
| - modalState, |
41 |
| - setModalState, |
| 45 | + open, |
| 46 | + modalState, |
| 47 | + setModalState, |
42 | 48 | }: Props) {
|
43 |
| - const dispatch = useDispatch(); |
44 |
| - const [inputValue, setInputValue] = useState<string>(""); |
45 |
| - const folderState = getFolderState(); |
| 49 | + const dispatch = useDispatch(); |
| 50 | + const [inputValue, setInputValue] = useState<string>(''); |
| 51 | + const folderState = getFolderState(); |
46 | 52 |
|
47 |
| - useEffect(() => { |
48 |
| - setInputValue(""); |
49 |
| - }, [modalState]); |
| 53 | + useEffect(() => { |
| 54 | + setInputValue(''); |
| 55 | + }, [modalState]); |
50 | 56 |
|
51 |
| - const handleSubmit = () => { |
52 |
| - switch (modalState.type) { |
53 |
| - case "folder": { |
54 |
| - const folderPayload: AddPayloadType = { |
55 |
| - name: inputValue, |
56 |
| - type: "Folder", |
57 |
| - parentId: folderState.parentFolder, |
58 |
| - }; |
59 |
| - dispatch(addItemAction(folderPayload)); |
60 |
| - break; |
61 |
| - } |
62 |
| - case "file": { |
63 |
| - const filePayload: AddPayloadType = { |
64 |
| - name: inputValue, |
65 |
| - type: "File", |
66 |
| - parentId: folderState.parentFolder, |
67 |
| - }; |
68 |
| - dispatch(addItemAction(filePayload)); |
69 |
| - break; |
70 |
| - } |
71 |
| - } |
| 57 | + const handleSubmit = () => { |
| 58 | + switch (modalState.type) { |
| 59 | + case 'folder': { |
| 60 | + const folderPayload: AddPayloadType = { |
| 61 | + name: inputValue, |
| 62 | + type: 'Folder', |
| 63 | + parentId: folderState.parentFolder, |
| 64 | + }; |
| 65 | + dispatch(addItemAction(folderPayload)); |
| 66 | + break; |
| 67 | + } |
| 68 | + case 'file': { |
| 69 | + const filePayload: AddPayloadType = { |
| 70 | + name: inputValue, |
| 71 | + type: 'File', |
| 72 | + parentId: folderState.parentFolder, |
| 73 | + }; |
| 74 | + dispatch(addItemAction(filePayload)); |
| 75 | + break; |
| 76 | + } |
| 77 | + case 'delete': { |
| 78 | + const folderPayload: DeletePayloadType = { |
| 79 | + id: modalState.selectedFile, |
| 80 | + }; |
| 81 | + dispatch(deleteFileEntityAction(folderPayload)); |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
72 | 85 |
|
73 |
| - setModalState({ |
74 |
| - open: false, |
75 |
| - type: "", |
76 |
| - }); |
77 |
| - }; |
| 86 | + setModalState({ |
| 87 | + open: false, |
| 88 | + selectedFile: '', |
| 89 | + type: '', |
| 90 | + }); |
| 91 | + }; |
78 | 92 |
|
79 |
| - const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
80 |
| - const value = e.target.value; |
81 |
| - setInputValue(value); |
82 |
| - }; |
| 93 | + const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 94 | + const value = e.target.value; |
| 95 | + setInputValue(value); |
| 96 | + }; |
83 | 97 |
|
84 |
| - const handleKeyDown = (e: React.KeyboardEvent) => { |
85 |
| - if (e.key === 'Enter') { |
86 |
| - handleSubmit(); |
87 |
| - } |
88 |
| - } |
| 98 | + const handleKeyDown = (e: React.KeyboardEvent) => { |
| 99 | + if (e.key === 'Enter') { |
| 100 | + handleSubmit(); |
| 101 | + } |
| 102 | + }; |
89 | 103 |
|
90 |
| - return ( |
91 |
| - <Modal |
92 |
| - open={open} |
93 |
| - onClose={() => { |
94 |
| - setModalState({ |
95 |
| - open: false, |
96 |
| - type: "", |
97 |
| - }); |
98 |
| - }} |
99 |
| - > |
100 |
| - <Container data-anchor="ConfirmationWindow"> |
101 |
| - <Typography variant="h5">Choose your {modalState.type} name</Typography> |
102 |
| - <Box display="flex" alignItems="center"> |
103 |
| - <TextField |
104 |
| - value={inputValue} |
105 |
| - onChange={handleChange} |
106 |
| - onKeyDown={handleKeyDown} |
107 |
| - sx={{ marginRight: "10px" }} |
108 |
| - /> |
109 |
| - <Button background="#73EEDC" onClick={handleSubmit}> |
110 |
| - submit |
111 |
| - </Button> |
112 |
| - </Box> |
113 |
| - </Container> |
114 |
| - </Modal> |
115 |
| - ); |
| 104 | + return ( |
| 105 | + <Modal |
| 106 | + open={open} |
| 107 | + onClose={() => { |
| 108 | + setModalState({ |
| 109 | + open: false, |
| 110 | + selectedFile: '', |
| 111 | + type: '', |
| 112 | + }); |
| 113 | + }} |
| 114 | + > |
| 115 | + {modalState.type !== 'delete' ? ( |
| 116 | + <Container data-anchor="ConfirmationWindow"> |
| 117 | + <Typography variant="h5"> |
| 118 | + Choose your {modalState.type} name |
| 119 | + </Typography> |
| 120 | + <Box display="flex" alignItems="center"> |
| 121 | + <TextField |
| 122 | + value={inputValue} |
| 123 | + onChange={handleChange} |
| 124 | + onKeyDown={handleKeyDown} |
| 125 | + sx={{ marginRight: '10px' }} |
| 126 | + /> |
| 127 | + <Button background="#73EEDC" onClick={handleSubmit}> |
| 128 | + submit |
| 129 | + </Button> |
| 130 | + </Box> |
| 131 | + </Container> |
| 132 | + ) : ( |
| 133 | + <Container data-anchor="ConfirmationWindow"> |
| 134 | + <Typography variant="h5"> |
| 135 | + Are you sure you want to delete? |
| 136 | + </Typography> |
| 137 | + <Box display="flex" alignItems="center"> |
| 138 | + <Button background="#73EEDC" onClick={handleSubmit}> |
| 139 | + continue |
| 140 | + </Button> |
| 141 | + </Box> |
| 142 | + </Container> |
| 143 | + )} |
| 144 | + </Modal> |
| 145 | + ); |
116 | 146 | }
|
0 commit comments