-
Notifications
You must be signed in to change notification settings - Fork 572
/
Copy pathConfirmationDialog.tsx
125 lines (123 loc) · 3.78 KB
/
ConfirmationDialog.tsx
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { Button, Dialog, Typography } from '@neo4j-ndl/react';
import { CustomFile } from '../../../types';
import LargeFilesAlert from './LargeFilesAlert';
import { memo, useEffect, useState } from 'react';
import { useFileContext } from '../../../context/UsersFiles';
import ExpiredFilesAlert from '../ExpirationModal/ExpiredFilesAlert';
import { isExpired } from '../../../utils/Utils';
function ConfirmationDialog({
largeFiles,
open,
onClose,
loading,
extractHandler,
selectedRows,
isLargeDocumentAlert = false,
}: {
largeFiles: CustomFile[];
open: boolean;
onClose: () => void;
loading: boolean;
extractHandler: (selectedFilesFromAllfiles: CustomFile[]) => void;
selectedRows: CustomFile[];
isLargeDocumentAlert?: boolean;
}) {
const { setSelectedRows, filesData, setRowSelection } = useFileContext();
const [checked, setChecked] = useState<string[]>([...largeFiles.map((f) => f.id)]);
const handleToggle = (ischecked: boolean, id: string) => {
const newChecked = [...checked];
if (ischecked) {
const file = filesData.find((f) => f.id === id);
newChecked.push(id);
setSelectedRows((prev) => {
const fileindex = prev.findIndex((f) => f === id);
if (fileindex == -1) {
return [...prev, JSON.stringify(file)];
}
return prev;
});
setRowSelection((prev) => {
const copiedobj = { ...prev };
for (const key in copiedobj) {
if (key == id) {
copiedobj[key] = true;
}
}
return copiedobj;
});
} else {
const currentIndex = checked.findIndex((v) => v === id);
newChecked.splice(currentIndex, 1);
setRowSelection((prev) => {
const copiedobj = { ...prev };
for (const key in copiedobj) {
if (key == id) {
copiedobj[key] = false;
}
}
return copiedobj;
});
setSelectedRows((prev) => {
const filteredrows = prev.filter((f) => f != id);
return filteredrows;
});
}
setChecked(newChecked);
};
useEffect(() => {
if (!checked.length) {
onClose();
}
}, [checked]);
return (
<Dialog
size='medium'
isOpen={open}
onClose={() => {
setChecked([]);
onClose();
}}
htmlAttributes={{
'aria-labelledby': 'form-dialog-title',
}}
>
<Dialog.Content className='n-flex n-flex-col n-gap-token-4'>
{largeFiles.length === 0 && loading ? (
<Typography variant='subheading-large'>Files are under processing</Typography>
) : isLargeDocumentAlert ? (
<LargeFilesAlert handleToggle={handleToggle} Files={largeFiles} checked={checked}></LargeFilesAlert>
) : (
<ExpiredFilesAlert checked={checked} handleToggle={handleToggle} Files={largeFiles} />
)}
</Dialog.Content>
<Dialog.Actions className='!mt-3'>
<Button
onClick={() => {
if (selectedRows.length) {
extractHandler(selectedRows);
} else {
const tobeProcessFiles: CustomFile[] = [];
for (let index = 0; index < checked.length; index++) {
const id = checked[index];
const file = filesData.find((f) => f.id === id);
if (file) {
tobeProcessFiles.push(file);
}
}
extractHandler(tobeProcessFiles);
}
setChecked([]);
onClose();
}}
size='large'
isDisabled={largeFiles.some(
(f) => f.createdAt != undefined && checked.includes(f.id) && isExpired(f?.createdAt as Date)
)}
>
Continue
</Button>
</Dialog.Actions>
</Dialog>
);
}
export default memo(ConfirmationDialog);