1
- /* eslint-disable */
2
1
import { Icon } from '@iconify/react' ;
3
- import React , { useState } from 'react' ;
2
+ import React , { useState , useEffect } from 'react' ;
4
3
import { useTranslation } from 'react-i18next' ;
5
4
import DataTable from '../../components/DataTable' ;
6
- import Sidebar from '../../components/Sidebar' ;
7
- import SessionData from '../../dummyData/session.json' ;
8
5
import useDocumentTitle from '../../hook/useDocumentTitle' ;
9
6
import Button from './../../components/Buttons' ;
7
+ import { gql , useQuery , useMutation } from '@apollo/client' ;
8
+
9
+ // Define your GraphQL queries and mutations
10
+ import {
11
+ GET_SESSIONS ,
12
+ CREATE_SESSION ,
13
+ DELETE_SESSION ,
14
+ EDIT_SESSION ,
15
+ } from '../../Mutations/session' ;
16
+
17
+ interface Session {
18
+ id : string ;
19
+ Sessionname : string ;
20
+ description : string ;
21
+ platform : string ;
22
+ duration : string ;
23
+ organizer : string ;
24
+ }
10
25
11
26
const AdminSission = ( ) => {
12
27
useDocumentTitle ( 'Sessions' ) ;
13
28
const { t } = useTranslation ( ) ;
14
-
29
+ const [ getSessionModel , setSessionModel ] = useState < Session [ ] > ( [ ] ) ;
30
+ const [ addSessionModel , setAddSessionModel ] = useState ( false ) ;
15
31
const [ deleteSessionModel , setDeleteSessionModel ] = useState ( false ) ;
16
32
const [ updateTraineeModel , setUpdateTraineeModel ] = useState ( false ) ;
17
- const [ addSessionModel , setAddSessionModel ] = useState ( false ) ;
18
33
19
- const removeModel = ( ) => {
20
- let newState = ! addSessionModel ;
21
- setAddSessionModel ( newState ) ;
34
+ const { loading, error, data } = useQuery ( GET_SESSIONS ) ;
35
+ // console.log('All Session :', data);
36
+ const [ sessionInput , setSessionInput ] = useState ( {
37
+ Sessionname : '' ,
38
+ description : '' ,
39
+ platform : '' ,
40
+ duration : '' ,
41
+ organizer : '' ,
42
+ } ) ;
43
+
44
+ const [ createSession ] = useMutation ( CREATE_SESSION , {
45
+ onCompleted : ( data ) => {
46
+ // Handle success, update cache, etc.
47
+ // console.log('Session created:', data);
48
+ // Optionally close the modal and clear input fields
49
+ setAddSessionModel ( false ) ;
50
+ setSessionInput ( {
51
+ Sessionname : '' ,
52
+ description : '' ,
53
+ platform : '' ,
54
+ duration : '' ,
55
+ organizer : '' ,
56
+ } ) ;
57
+ } ,
58
+ onError : ( error ) => {
59
+ // console.error('Error creating a session:', error);
60
+ // Handle any errors, such as displaying an error message to the user.
61
+ } ,
62
+ } ) ;
63
+
64
+ useEffect ( ( ) => {
65
+ if ( data ) {
66
+ const allSessions = data . getAllSessions . map ( ( session : any ) => ( {
67
+ id : session . id ,
68
+ Sessionname : session . Sessionname ,
69
+ description : session . description ,
70
+ platform : session . platform ,
71
+ duration : session . duration ,
72
+ organizer : session . organizer ,
73
+ } ) ) ;
74
+
75
+ setSessionModel ( allSessions ) ;
76
+ }
77
+ } , [ data ] ) ;
78
+
79
+ const toggleAddSessionModel = ( ) => {
80
+ setAddSessionModel ( ! addSessionModel ) ;
22
81
} ;
23
- const removeDeleteModel = ( ) => {
24
- let newState = ! deleteSessionModel ;
25
- setDeleteSessionModel ( newState ) ;
82
+
83
+ const handleInputChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
84
+ const { name, value } = e . target ;
85
+ setSessionInput ( { ...sessionInput , [ name ] : value } ) ;
26
86
} ;
27
87
88
+ const handleSaveSession = ( ) => {
89
+ // console.log('sessionInput:', sessionInput);
90
+ createSession ( {
91
+ variables : {
92
+ sessionInput : sessionInput ,
93
+ } ,
94
+ } ) ;
95
+ } ;
28
96
29
- const data = SessionData ;
30
97
const columns = [
31
- { Header : 'Session' , accessor : 'session ' } ,
32
- { Header : 'Description' , accessor : 'desc ' } ,
33
- { Header : 'Platform' , accessor : 'place ' } ,
34
- { Header : 'Duration' , accessor : 'duration' } ,
35
- { Header : 'Organizer' , accessor : 'organizer' } ,
98
+ { Header : t ( 'Sessionname' ) , accessor : 'Sessionname ' } ,
99
+ { Header : t ( 'description' ) , accessor : 'description ' } ,
100
+ { Header : t ( 'platform' ) , accessor : 'platform ' } ,
101
+ { Header : t ( 'duration' ) , accessor : 'duration' } ,
102
+ { Header : t ( 'organizer' ) , accessor : 'organizer' } ,
36
103
{
37
104
Header : 'Action' ,
38
105
accessor : '' ,
39
106
Cell : ( ) => < Icon icon = "entypo:dots-three-vertical" color = "#9e85f5" /> ,
40
107
} ,
41
108
] ;
42
109
110
+ const removeModel = ( ) => {
111
+ let newState = ! addSessionModel ;
112
+ setAddSessionModel ( newState ) ;
113
+ } ;
114
+
115
+ const removeDeleteModel = ( ) => {
116
+ let newState = ! deleteSessionModel ;
117
+ setDeleteSessionModel ( newState ) ;
118
+ } ;
119
+
43
120
return (
44
121
< >
45
122
{ /* =========================== Start:: add Session Model =============================== */ }
@@ -61,29 +138,35 @@ const AdminSission = () => {
61
138
< div className = "grouped-input flex items-center h-full w-full rounded-md" >
62
139
< input
63
140
type = "text"
64
- name = "name "
141
+ name = "Sessionname "
65
142
className = "border border-primary rounded outline-none px-5 font-sans dark:bg-dark-frame-bg dark:text-white text-xs py-2 w-full"
66
143
placeholder = { t ( 'SessionName' ) }
144
+ onChange = { handleInputChange }
145
+ value = { sessionInput . Sessionname }
67
146
/>
68
147
</ div >
69
148
</ div >
70
149
< div className = "input my-3 h-9 " >
71
150
< div className = "grouped-input flex items-center h-full w-full rounded-md" >
72
151
< input
73
152
type = "text"
74
- name = "name "
153
+ name = "description "
75
154
className = " border border-primary py-2 rounded outline-none px-5 dark:bg-dark-frame-bg dark:text-white font-sans text-xs w-full"
76
- placeholder = { t ( 'Description' ) }
155
+ placeholder = { t ( 'description' ) }
156
+ onChange = { handleInputChange }
157
+ value = { sessionInput . description }
77
158
/>
78
159
</ div >
79
160
</ div >
80
161
< div className = "input my-3 h-9 " >
81
162
< div className = "grouped-input flex items-center h-full w-full rounded-md" >
82
163
< input
83
164
type = "text"
84
- name = "name "
165
+ name = "platform "
85
166
className = "border border-primary py-2 rounded dark:bg-dark-frame-bg dark:text-white outline-none px-5 font-sans text-xs w-full"
86
- placeholder = { t ( 'Platform' ) }
167
+ placeholder = { t ( 'platform' ) }
168
+ onChange = { handleInputChange }
169
+ value = { sessionInput . platform }
87
170
/>
88
171
</ div >
89
172
</ div >
@@ -92,9 +175,11 @@ const AdminSission = () => {
92
175
< div className = "grouped-input flex items-center h-full w-full rounded-md" >
93
176
< input
94
177
type = "time"
95
- name = "name "
178
+ name = "duration "
96
179
className = "border border-primary py-2 dark:bg-dark-frame-bg dark:text-white rounded outline-none px-5 font-sans text-xs w-full"
97
- placeholder = { t ( 'Duration' ) }
180
+ placeholder = { t ( 'duration' ) }
181
+ onChange = { handleInputChange }
182
+ value = { sessionInput . duration }
98
183
/>
99
184
</ div >
100
185
</ div >
@@ -103,9 +188,11 @@ const AdminSission = () => {
103
188
< div className = "grouped-input flex items-center h-full w-full rounded-md" >
104
189
< input
105
190
type = "text"
106
- name = "name "
191
+ name = "organizer "
107
192
className = " border border-primary py-2 dark:bg-dark-frame-bg dark:text-white rounded outline-none px-5 font-sans text-xs w-full"
108
- placeholder = { t ( 'Organizer' ) }
193
+ placeholder = { t ( 'organizer' ) }
194
+ onChange = { handleInputChange }
195
+ value = { sessionInput . organizer }
109
196
/>
110
197
</ div >
111
198
</ div >
@@ -114,19 +201,17 @@ const AdminSission = () => {
114
201
variant = "info"
115
202
size = "sm"
116
203
style = "w-[30%] md:w-1/4 text-sm font-sans"
117
- data-testid = "remove"
118
- onClick = { ( ) => removeModel ( ) }
204
+ onClick = { toggleAddSessionModel }
119
205
>
120
- { ' ' }
121
- { t ( 'Cancel' ) } { ' ' }
206
+ { t ( 'Cancel' ) }
122
207
</ Button >
123
208
< Button
124
209
variant = "primary"
125
210
size = "sm"
126
211
style = "w-[30%] md:w-1/4 text-sm font-sans"
212
+ onClick = { handleSaveSession }
127
213
>
128
- { ' ' }
129
- { t ( 'Save' ) } { ' ' }
214
+ { t ( 'Save' ) }
130
215
</ Button >
131
216
</ div >
132
217
</ form >
@@ -291,12 +376,16 @@ const AdminSission = () => {
291
376
</ Button >
292
377
</ div >
293
378
</ div >
294
- < div className = "" >
295
- < DataTable
296
- data = { data }
297
- columns = { columns }
298
- title = "Developers list"
299
- />
379
+ < div >
380
+ { loading ? (
381
+ < div > Loading...</ div >
382
+ ) : (
383
+ < DataTable
384
+ data = { getSessionModel }
385
+ columns = { columns }
386
+ title = { t ( 'Sessions List' ) }
387
+ />
388
+ ) }
300
389
</ div >
301
390
</ div >
302
391
</ div >
0 commit comments