Skip to content

Commit 66d02b4

Browse files
committed
feat Create session functionality & design
1 parent 626dc20 commit 66d02b4

File tree

3 files changed

+233
-46
lines changed

3 files changed

+233
-46
lines changed

src/Mutations/session.tsx

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { gql } from '@apollo/client';
2+
3+
// Query to get a single session by ID
4+
export const GET_SESSION = gql`
5+
query GetSession($ID: ID!) {
6+
getSession(id: $ID) {
7+
id
8+
Sessionname
9+
description
10+
platform
11+
duration
12+
organizer
13+
}
14+
}
15+
`;
16+
17+
// Query to get a list of all sessions
18+
export const GET_SESSIONS = gql`
19+
query GetSessions {
20+
getAllSessions {
21+
id
22+
Sessionname
23+
description
24+
platform
25+
duration
26+
organizer
27+
}
28+
}
29+
`;
30+
31+
// Mutation to create a new session
32+
export const CREATE_SESSION = gql`
33+
mutation CreateSession($sessionInput: SessionInput) {
34+
createSession(sessionInput: $sessionInput) {
35+
id
36+
Sessionname
37+
description
38+
platform
39+
duration
40+
organizer
41+
}
42+
}
43+
`;
44+
45+
// Mutation to delete a session by ID
46+
export const DELETE_SESSION = gql`
47+
mutation DeleteSession($ID: ID!) {
48+
deleteSession(ID: $ID)
49+
}
50+
`;
51+
52+
// Mutation to edit/update a session by ID
53+
export const EDIT_SESSION = gql`
54+
mutation EditSession($ID: ID!, $editSessionInput: EditSessionInput) {
55+
editSession(id: $ID, editSessionInput: $editSessionInput)
56+
}
57+
`;

src/containers/DashRoutes.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ const ManagersCards = React.lazy(() => import('../components/ManagerCard'));
7676
const CoordinatorCards = React.lazy(
7777
() => import('../components/CoordinatorCard'),
7878
);
79+
const AdminSission = React.lazy(() => import('./admin-dashBoard/Sessions'));
80+
7981

8082

8183
function DashRoutes() {
@@ -122,7 +124,7 @@ function DashRoutes() {
122124
<Route path="/cohorts" element={<AdminCohorts />} />
123125
<Route path="/phases" element={<AdminPhases />} />
124126
<Route path="/programs" element={<AdminPrograms />} />
125-
<Route path="/sessions" element={<AdminSession />} />
127+
<Route path="/sessions" element={<AdminSission />} />
126128
<Route path="/manage" element={<AdminManageRoles />} />
127129
<Route path="/grading" element={<GradingSystem />} />
128130
<Route

src/containers/admin-dashBoard/Sessions.tsx

+173-45
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,162 @@
1-
/* eslint-disable */
21
import { Icon } from '@iconify/react';
3-
import React, { useState } from 'react';
2+
import React, { useState, useEffect } from 'react';
43
import { useTranslation } from 'react-i18next';
54
import DataTable from '../../components/DataTable';
6-
import Sidebar from '../../components/Sidebar';
7-
import SessionData from '../../dummyData/session.json';
85
import useDocumentTitle from '../../hook/useDocumentTitle';
96
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+
}
1025

1126
const AdminSission = () => {
1227
useDocumentTitle('Sessions');
1328
const { t } = useTranslation();
14-
29+
const [getSessionModel, setSessionModel] = useState<Session[]>([]);
30+
const [addSessionModel, setAddSessionModel] = useState(false);
1531
const [deleteSessionModel, setDeleteSessionModel] = useState(false);
1632
const [updateTraineeModel, setUpdateTraineeModel] = useState(false);
17-
const [addSessionModel, setAddSessionModel] = useState(false);
33+
const [sessionToDelete, setSessionToDelete] = useState('');
1834

19-
const removeModel = () => {
20-
let newState = !addSessionModel;
21-
setAddSessionModel(newState);
35+
const { loading, error, data } = useQuery(GET_SESSIONS);
36+
useEffect(() => {
37+
if (data) {
38+
const allSessions = data.getAllSessions.map((session: any) => ({
39+
id: session.id,
40+
Sessionname: session.Sessionname,
41+
description: session.description,
42+
platform: session.platform,
43+
duration: session.duration,
44+
organizer: session.organizer,
45+
}));
46+
47+
setSessionModel(allSessions);
48+
}
49+
}, [data]);
50+
51+
52+
const [sessionInput, setSessionInput] = useState({
53+
Sessionname: '',
54+
description: '',
55+
platform: '',
56+
duration: '',
57+
organizer: '',
58+
});
59+
60+
const [createSession] = useMutation(CREATE_SESSION, {
61+
onCompleted: (data) => {
62+
setAddSessionModel(false);
63+
setSessionInput({
64+
Sessionname: '',
65+
description: '',
66+
platform: '',
67+
duration: '',
68+
organizer: '',
69+
});
70+
},
71+
onError: (error) => {
72+
console.error('Error add session:', error);
73+
}, });
74+
75+
76+
const toggleAddSessionModel = () => {
77+
setAddSessionModel(!addSessionModel);
2278
};
23-
const removeDeleteModel = () => {
24-
let newState = !deleteSessionModel;
25-
setDeleteSessionModel(newState);
79+
80+
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
81+
const { name, value } = e.target;
82+
setSessionInput({ ...sessionInput, [name]: value });
2683
};
2784

85+
const handleSaveSession = () => {
86+
// console.log('sessionInput:', sessionInput);
87+
createSession({
88+
variables: {
89+
sessionInput: sessionInput,
90+
},
91+
});
92+
};
93+
94+
95+
const [deleteSession] = useMutation(DELETE_SESSION);
96+
const handleDeleteSession = (sessionId: string) => {
97+
setSessionToDelete(sessionId);
98+
setDeleteSessionModel(true);
99+
};
100+
101+
const handleConfirmDelete = () => {
102+
deleteSession({
103+
variables: {
104+
ID: sessionToDelete, // Use "ID" here instead of "id"
105+
},
106+
})
107+
.then(() => {
108+
setDeleteSessionModel(false);
109+
setSessionToDelete('');
110+
})
111+
.catch((error) => {
112+
console.error('Error deleting session:', error);
113+
});
114+
};
115+
28116

29-
const data = SessionData;
30117
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' },
118+
{ Header: t('Sessionname'), accessor: 'Sessionname' },
119+
{ Header: t('description'), accessor: 'description' },
120+
{ Header: t('platform'), accessor: 'platform' },
121+
{ Header: t('duration'), accessor: 'duration' },
122+
{ Header: t('organizer'), accessor: 'organizer' },
123+
36124
{
37125
Header: 'Action',
38126
accessor: '',
39-
Cell: () => <Icon icon="entypo:dots-three-vertical" color="#9e85f5" />,
127+
Cell: ({ row }: any) => (
128+
<div className={`items-center${row.length > 0 ? ' flex' : ' flex'}`}>
129+
<Icon
130+
icon="el:file-edit-alt"
131+
className="mr-2"
132+
width="25"
133+
height="25"
134+
cursor="pointer"
135+
color="#9e85f5"
136+
/>
137+
<Icon
138+
icon="mdi:trash-can"
139+
width="30"
140+
height="30"
141+
cursor="pointer"
142+
color="#9e85f5"
143+
onClick={() => handleDeleteSession(row.original.id)}
144+
/>
145+
</div>
146+
),
40147
},
41148
];
42149

150+
const removeModel = () => {
151+
let newState = !addSessionModel;
152+
setAddSessionModel(newState);
153+
};
154+
155+
const removeDeleteModel = () => {
156+
let newState = !deleteSessionModel;
157+
setDeleteSessionModel(newState);
158+
};
159+
43160
return (
44161
<>
45162
{/* =========================== Start:: add Session Model =============================== */}
@@ -61,29 +178,35 @@ const AdminSission = () => {
61178
<div className="grouped-input flex items-center h-full w-full rounded-md">
62179
<input
63180
type="text"
64-
name="name"
181+
name="Sessionname"
65182
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"
66183
placeholder={t('SessionName')}
184+
onChange={handleInputChange}
185+
value={sessionInput.Sessionname}
67186
/>
68187
</div>
69188
</div>
70189
<div className="input my-3 h-9 ">
71190
<div className="grouped-input flex items-center h-full w-full rounded-md">
72191
<input
73192
type="text"
74-
name="name"
193+
name="description"
75194
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')}
195+
placeholder={t('description')}
196+
onChange={handleInputChange}
197+
value={sessionInput.description}
77198
/>
78199
</div>
79200
</div>
80201
<div className="input my-3 h-9 ">
81202
<div className="grouped-input flex items-center h-full w-full rounded-md">
82203
<input
83204
type="text"
84-
name="name"
205+
name="platform"
85206
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')}
207+
placeholder={t('platform')}
208+
onChange={handleInputChange}
209+
value={sessionInput.platform}
87210
/>
88211
</div>
89212
</div>
@@ -92,9 +215,11 @@ const AdminSission = () => {
92215
<div className="grouped-input flex items-center h-full w-full rounded-md">
93216
<input
94217
type="time"
95-
name="name"
218+
name="duration"
96219
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')}
220+
placeholder={t('duration')}
221+
onChange={handleInputChange}
222+
value={sessionInput.duration}
98223
/>
99224
</div>
100225
</div>
@@ -103,9 +228,11 @@ const AdminSission = () => {
103228
<div className="grouped-input flex items-center h-full w-full rounded-md">
104229
<input
105230
type="text"
106-
name="name"
231+
name="organizer"
107232
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')}
233+
placeholder={t('organizer')}
234+
onChange={handleInputChange}
235+
value={sessionInput.organizer}
109236
/>
110237
</div>
111238
</div>
@@ -114,19 +241,17 @@ const AdminSission = () => {
114241
variant="info"
115242
size="sm"
116243
style="w-[30%] md:w-1/4 text-sm font-sans"
117-
data-testid="remove"
118-
onClick={() => removeModel()}
244+
onClick={toggleAddSessionModel}
119245
>
120-
{' '}
121-
{t('Cancel')}{' '}
246+
{t('Cancel')}
122247
</Button>
123248
<Button
124249
variant="primary"
125250
size="sm"
126251
style="w-[30%] md:w-1/4 text-sm font-sans"
252+
onClick={handleSaveSession}
127253
>
128-
{' '}
129-
{t('Save')}{' '}
254+
{t('Save')}
130255
</Button>
131256
</div>
132257
</form>
@@ -160,16 +285,15 @@ const AdminSission = () => {
160285
variant="info"
161286
size="sm"
162287
style="w-[30%] md:w-1/4 text-sm font-sans"
163-
data-testid="delete"
164-
onClick={() => removeDeleteModel()}
288+
onClick={removeDeleteModel}
165289
>
166-
{' '}
167-
{t('Cancel')}{' '}
290+
{t('Cancel')}
168291
</Button>
169292
<Button
170-
variant="danger"
293+
variant="primary"
171294
size="sm"
172295
style="w-[30%] md:w-1/4 text-sm font-sans"
296+
onClick={handleConfirmDelete}
173297
>
174298
{' '}
175299
{t('Delete')}{' '}
@@ -291,12 +415,16 @@ const AdminSission = () => {
291415
</Button>
292416
</div>
293417
</div>
294-
<div className="">
295-
<DataTable
296-
data={data}
297-
columns={columns}
298-
title="Developers list"
299-
/>
418+
<div>
419+
{loading ? (
420+
<div>Loading...</div>
421+
) : (
422+
<DataTable
423+
data={getSessionModel}
424+
columns={columns}
425+
title={t('Sessions List')}
426+
/>
427+
)}
300428
</div>
301429
</div>
302430
</div>

0 commit comments

Comments
 (0)