Skip to content

Commit f7c0367

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

File tree

3 files changed

+188
-40
lines changed

3 files changed

+188
-40
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

+128-39
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,122 @@
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);
1833

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);
2281
};
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 });
2686
};
2787

88+
const handleSaveSession = () => {
89+
// console.log('sessionInput:', sessionInput);
90+
createSession({
91+
variables: {
92+
sessionInput: sessionInput,
93+
},
94+
});
95+
};
2896

29-
const data = SessionData;
3097
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' },
36103
{
37104
Header: 'Action',
38105
accessor: '',
39106
Cell: () => <Icon icon="entypo:dots-three-vertical" color="#9e85f5" />,
40107
},
41108
];
42109

110+
const removeModel = () => {
111+
let newState = !addSessionModel;
112+
setAddSessionModel(newState);
113+
};
114+
115+
const removeDeleteModel = () => {
116+
let newState = !deleteSessionModel;
117+
setDeleteSessionModel(newState);
118+
};
119+
43120
return (
44121
<>
45122
{/* =========================== Start:: add Session Model =============================== */}
@@ -61,29 +138,35 @@ const AdminSission = () => {
61138
<div className="grouped-input flex items-center h-full w-full rounded-md">
62139
<input
63140
type="text"
64-
name="name"
141+
name="Sessionname"
65142
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"
66143
placeholder={t('SessionName')}
144+
onChange={handleInputChange}
145+
value={sessionInput.Sessionname}
67146
/>
68147
</div>
69148
</div>
70149
<div className="input my-3 h-9 ">
71150
<div className="grouped-input flex items-center h-full w-full rounded-md">
72151
<input
73152
type="text"
74-
name="name"
153+
name="description"
75154
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}
77158
/>
78159
</div>
79160
</div>
80161
<div className="input my-3 h-9 ">
81162
<div className="grouped-input flex items-center h-full w-full rounded-md">
82163
<input
83164
type="text"
84-
name="name"
165+
name="platform"
85166
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}
87170
/>
88171
</div>
89172
</div>
@@ -92,9 +175,11 @@ const AdminSission = () => {
92175
<div className="grouped-input flex items-center h-full w-full rounded-md">
93176
<input
94177
type="time"
95-
name="name"
178+
name="duration"
96179
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}
98183
/>
99184
</div>
100185
</div>
@@ -103,9 +188,11 @@ const AdminSission = () => {
103188
<div className="grouped-input flex items-center h-full w-full rounded-md">
104189
<input
105190
type="text"
106-
name="name"
191+
name="organizer"
107192
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}
109196
/>
110197
</div>
111198
</div>
@@ -114,19 +201,17 @@ const AdminSission = () => {
114201
variant="info"
115202
size="sm"
116203
style="w-[30%] md:w-1/4 text-sm font-sans"
117-
data-testid="remove"
118-
onClick={() => removeModel()}
204+
onClick={toggleAddSessionModel}
119205
>
120-
{' '}
121-
{t('Cancel')}{' '}
206+
{t('Cancel')}
122207
</Button>
123208
<Button
124209
variant="primary"
125210
size="sm"
126211
style="w-[30%] md:w-1/4 text-sm font-sans"
212+
onClick={handleSaveSession}
127213
>
128-
{' '}
129-
{t('Save')}{' '}
214+
{t('Save')}
130215
</Button>
131216
</div>
132217
</form>
@@ -291,12 +376,16 @@ const AdminSission = () => {
291376
</Button>
292377
</div>
293378
</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+
)}
300389
</div>
301390
</div>
302391
</div>

0 commit comments

Comments
 (0)