robobyte-front-builder 1.0.17 → 1.0.19

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.
@@ -0,0 +1,315 @@
1
+ // ** React Imports
2
+
3
+ // ** MUI Imports
4
+ import {yupResolver} from '@hookform/resolvers/yup'
5
+ import * as yup from 'yup'
6
+ import {
7
+ Button,
8
+ DialogActions,
9
+ DialogContent,
10
+ DialogTitle,
11
+ Grid,
12
+ IconButton,
13
+ Typography
14
+ } from '@mui/material'
15
+ import Box from '@mui/material/Box'
16
+ import {Close, DeleteOutline, Plus} from 'mdi-material-ui'
17
+ import {useEffect, useState} from 'react'
18
+ import {useForm} from 'react-hook-form'
19
+ import {Endpoints, Services} from 'src/services/Endpoints'
20
+ import {toast} from 'react-hot-toast'
21
+ import {AgGridReact} from "ag-grid-react";
22
+ import {AutocompleteEditorWrapper} from "views/genericTable/cellEditors/autocompleteEditor";
23
+
24
+ const UpdateReportPermissionsDialog = props => {
25
+ // ** Props
26
+ const {handleToggleDialogs, report, dialog, setRefresh} = props
27
+ const [IsSubmitting, setIsSubmitting] = useState(false)
28
+ const [IsLoading, setIsLoading] = useState(false)
29
+ const [ReportPermissions, setReportPermissions] = useState([])
30
+ const [gridApi, setGridApi] = useState()
31
+
32
+ const colDefs = [
33
+ {
34
+ headerName: 'دور الصلاحية',
35
+ field: 'permissionRole',
36
+ valueFormatter: (value) => value.data.permissionRole?.name || '',
37
+ cellEditor: AutocompleteEditorWrapper,
38
+ cellEditorParams: (value) => {
39
+ return {
40
+ dataEndpoint: Endpoints.ViewPermissionRole.Post.GetViewPermissionRolePaged,
41
+ fieldName: ['name'],
42
+ filterFields: ['name'],
43
+ uniqueId: 'id',
44
+ value: value.data.permissionRole,
45
+ isSingle: true
46
+ }
47
+ },
48
+ editable: true,
49
+ onCellValueChanged: (params) => {
50
+ if (params.newValue && params.newValue.id) {
51
+ params.data.permissionRoleId = params.newValue.id
52
+ params.data.permissionRole = params.newValue
53
+ params.api.refreshCells({
54
+ rowNodes: [params.node],
55
+ force: true,
56
+ columns: ['permissionRole']
57
+ })
58
+ }
59
+ }
60
+ },
61
+ {
62
+ field: 'Delete',
63
+ width: 80,
64
+ headerName: 'حذف',
65
+ cellRenderer: (params) => {
66
+ return (
67
+ <IconButton
68
+ size="small"
69
+ onClick={() => handleItemDelete(params.node)}
70
+ color="error"
71
+ >
72
+ <DeleteOutline/>
73
+ </IconButton>
74
+ )
75
+ }
76
+ },
77
+ ]
78
+
79
+ const schema = yup.object().shape({})
80
+ const {
81
+ register,
82
+ handleSubmit,
83
+ clearErrors,
84
+ formState: {errors}
85
+ } = useForm({
86
+ mode: 'onBlur',
87
+ reValidateMode: 'onBlur',
88
+ resolver: yupResolver(schema)
89
+ })
90
+
91
+
92
+ const handleItemDelete = (node) => {
93
+ if (gridApi == null) {
94
+ return
95
+ }
96
+ if (node.data.isNew) {
97
+ gridApi.applyTransaction({
98
+ remove: [node.data],
99
+ });
100
+ } else {
101
+ node.data['isForDelete'] = !node.data['isForDelete']
102
+ gridApi.applyTransaction({
103
+ update: [node.data],
104
+ });
105
+ }
106
+ }
107
+
108
+ const handleAddNew = () => {
109
+ if (gridApi == null) {
110
+ return
111
+ }
112
+ const defaultItem = {
113
+ id: 0,
114
+ reportId: report.id,
115
+ permissionRoleId: 0,
116
+ permissionRole: null,
117
+ isForDelete: false,
118
+ isNew: true,
119
+ isAdd: false
120
+ }
121
+ gridApi.applyTransaction({
122
+ add: [defaultItem],
123
+ addIndex: gridApi.getDisplayedRowCount(),
124
+ });
125
+ }
126
+
127
+ async function handleSave() {
128
+ try {
129
+ setIsSubmitting(true)
130
+ if (!gridApi) return;
131
+
132
+ let allData = []
133
+ gridApi.forEachNode(node => {
134
+ if (node.data != null && !node.data.isAdd) {
135
+ // Only include if permissionRoleId is set (for new items) or if it's an existing item
136
+ if (node.data.permissionRoleId || node.data.id) {
137
+ allData.push({
138
+ reportId: report.id,
139
+ permissionRoleId: node.data.permissionRoleId || node.data.permissionRole?.id || 0,
140
+ id: node.data?.id ?? 0,
141
+ isForDelete: node.data.isForDelete || false,
142
+ })
143
+ }
144
+ }
145
+ })
146
+
147
+ allData = allData.filter(item => item.permissionRoleId > 0)
148
+ if (allData.length === 0) {
149
+ toast.error("الرجاء إضافة دور صلاحية واحد على الأقل")
150
+ setIsSubmitting(false)
151
+ return
152
+ }
153
+ const response = await Services.PostService(
154
+ Endpoints.ViewPermissionRole.Post.ReportPermissionsUpdateRange,
155
+ true,
156
+ allData
157
+ )
158
+
159
+ if (response) {
160
+ toast.success("تم الحفظ بنجاح")
161
+ handleDialogClose()
162
+ if (setRefresh) {
163
+ setRefresh(prev => !prev)
164
+ }
165
+ } else {
166
+ toast.error("فشل الحفظ، حاول مرة أخرى")
167
+ }
168
+
169
+ } catch (error) {
170
+ console.error("Error saving report permissions:", error)
171
+ toast.error("فشل الحفظ، حاول مرة أخرى")
172
+ } finally {
173
+ setIsSubmitting(false)
174
+ }
175
+ }
176
+
177
+ async function handleGetReportPermissions() {
178
+ try {
179
+ setIsLoading(true)
180
+ // TODO: Replace with actual endpoint for fetching report permissions
181
+ // For now, using a filter structure that might work
182
+ let filter = {
183
+ Tfilter: [
184
+ {
185
+ path: 'reportId',
186
+ method: 'Equals',
187
+ value: report.id
188
+ }
189
+ ]
190
+ }
191
+
192
+ // Using ViewPermissionRoleItems endpoint as it seems related
193
+ let response = await Services.PostService(
194
+ Endpoints.ViewPermissionRole.Post.ReportPermissionsGetAll,
195
+ false,
196
+ filter
197
+ )
198
+
199
+ if (response && response.data) {
200
+ const permissions = response.data.map((value, index) => ({
201
+ ...value,
202
+ key: index,
203
+ isForDelete: false,
204
+ permissionRoleId: value.permissionRoleId || value.permissionRole?.id,
205
+ permissionRole: value.permissionRole || null
206
+ }))
207
+ setReportPermissions(permissions)
208
+ } else {
209
+ setReportPermissions([])
210
+ }
211
+ } catch (error) {
212
+ console.error("Error fetching report permissions:", error)
213
+ setReportPermissions([])
214
+ } finally {
215
+ setIsLoading(false)
216
+ }
217
+ }
218
+
219
+
220
+ const handleDialogClose = async () => {
221
+ clearErrors()
222
+ handleToggleDialogs(dialog)
223
+ }
224
+
225
+ useEffect(() => {
226
+ if (report) {
227
+ handleGetReportPermissions()
228
+ }
229
+ }, [report])
230
+
231
+ return (
232
+ <>
233
+ <DialogTitle id='confirmation-dialog-title' sx={{padding: 0, marginBottom: 5}}>
234
+ <Box
235
+ sx={{
236
+ display: 'flex',
237
+ justifyContent: 'space-between',
238
+ backgroundColor: 'background.default',
239
+ p: theme => theme.spacing(3, 3.255, 3, 5.255)
240
+ }}
241
+ >
242
+ <Typography variant='h6'>صلاحيات التقرير</Typography>
243
+ <Box sx={{display: 'flex', alignItems: 'center'}}>
244
+ <Close fontSize='small' onClick={handleDialogClose} sx={{cursor: 'pointer'}}/>
245
+ </Box>
246
+ </Box>
247
+ </DialogTitle>
248
+
249
+ <DialogContent className='dialog-body'>
250
+ <form
251
+ onSubmit={handleSubmit(e => {
252
+ handleSave(e)
253
+ })}
254
+ >
255
+ <Box sx={{display: 'flex', justifyContent: 'flex-end', mb: 2}}>
256
+ <Button
257
+ variant='outlined'
258
+ size='small'
259
+ startIcon={<Plus/>}
260
+ onClick={handleAddNew}
261
+ >
262
+ إضافة دور صلاحية
263
+ </Button>
264
+ </Box>
265
+ <Grid container size={{ xs: 12 }}>
266
+ <div style={{width: "100%", height: "40vh", direction: 'ltr'}}>
267
+ <AgGridReact
268
+ enableRtl={true}
269
+ columnDefs={colDefs}
270
+ rowData={ReportPermissions}
271
+ onGridReady={(params) => setGridApi(params.api)}
272
+ singleClickEdit={true}
273
+ defaultColDef={{
274
+ sortable: false,
275
+ suppressHeaderMenuButton: true,
276
+ }}
277
+ pinnedBottomRowData={[{
278
+ id: 'add-row',
279
+ isAdd: true
280
+ }]}
281
+ getRowStyle={(row) => {
282
+ if (row.data?.isAdd) {
283
+ return {display: 'none'};
284
+ }
285
+ if (row.data?.isForDelete) {
286
+ return {backgroundColor: '#FF625F'};
287
+ }
288
+ if (row.data?.isNew) {
289
+ return {backgroundColor: '#f6fff3'};
290
+ }
291
+ return null;
292
+ }}
293
+ />
294
+ </div>
295
+ </Grid>
296
+ <DialogActions sx={{pb: {xs: 8}, justifyContent: 'center'}}>
297
+ <Button
298
+ variant='contained'
299
+ sx={{mr: 2}}
300
+ disabled={IsSubmitting || IsLoading}
301
+ onClick={handleSave}
302
+ >
303
+ {IsSubmitting ? 'جاري الحفظ...' : 'حفظ'}
304
+ </Button>
305
+ <Button variant='outlined' color='secondary' onClick={handleDialogClose}>
306
+ الغاء
307
+ </Button>
308
+ </DialogActions>
309
+ </form>
310
+ </DialogContent>
311
+ </>
312
+ )
313
+ }
314
+
315
+ export default UpdateReportPermissionsDialog