From 66d02b43b6ee2c66f0c1292e4b94610de8bc69bd Mon Sep 17 00:00:00 2001 From: NTElissa Date: Mon, 16 Oct 2023 15:43:20 +0200 Subject: [PATCH] feat Create session functionality & design --- src/Mutations/session.tsx | 57 +++++ src/containers/DashRoutes.tsx | 4 +- src/containers/admin-dashBoard/Sessions.tsx | 218 ++++++++++++++++---- 3 files changed, 233 insertions(+), 46 deletions(-) create mode 100644 src/Mutations/session.tsx diff --git a/src/Mutations/session.tsx b/src/Mutations/session.tsx new file mode 100644 index 000000000..9576d6c39 --- /dev/null +++ b/src/Mutations/session.tsx @@ -0,0 +1,57 @@ +import { gql } from '@apollo/client'; + +// Query to get a single session by ID +export const GET_SESSION = gql` + query GetSession($ID: ID!) { + getSession(id: $ID) { + id + Sessionname + description + platform + duration + organizer + } + } +`; + +// Query to get a list of all sessions +export const GET_SESSIONS = gql` + query GetSessions { + getAllSessions { + id + Sessionname + description + platform + duration + organizer + } + } +`; + +// Mutation to create a new session +export const CREATE_SESSION = gql` + mutation CreateSession($sessionInput: SessionInput) { + createSession(sessionInput: $sessionInput) { + id + Sessionname + description + platform + duration + organizer + } + } +`; + +// Mutation to delete a session by ID +export const DELETE_SESSION = gql` + mutation DeleteSession($ID: ID!) { + deleteSession(ID: $ID) + } +`; + +// Mutation to edit/update a session by ID +export const EDIT_SESSION = gql` + mutation EditSession($ID: ID!, $editSessionInput: EditSessionInput) { + editSession(id: $ID, editSessionInput: $editSessionInput) + } +`; diff --git a/src/containers/DashRoutes.tsx b/src/containers/DashRoutes.tsx index 8685a0d61..96866e03f 100644 --- a/src/containers/DashRoutes.tsx +++ b/src/containers/DashRoutes.tsx @@ -76,6 +76,8 @@ const ManagersCards = React.lazy(() => import('../components/ManagerCard')); const CoordinatorCards = React.lazy( () => import('../components/CoordinatorCard'), ); +const AdminSission = React.lazy(() => import('./admin-dashBoard/Sessions')); + function DashRoutes() { @@ -122,7 +124,7 @@ function DashRoutes() { } /> } /> } /> - } /> + } /> } /> } /> { useDocumentTitle('Sessions'); const { t } = useTranslation(); - + const [getSessionModel, setSessionModel] = useState([]); + const [addSessionModel, setAddSessionModel] = useState(false); const [deleteSessionModel, setDeleteSessionModel] = useState(false); const [updateTraineeModel, setUpdateTraineeModel] = useState(false); - const [addSessionModel, setAddSessionModel] = useState(false); + const [sessionToDelete, setSessionToDelete] = useState(''); - const removeModel = () => { - let newState = !addSessionModel; - setAddSessionModel(newState); + const { loading, error, data } = useQuery(GET_SESSIONS); + useEffect(() => { + if (data) { + const allSessions = data.getAllSessions.map((session: any) => ({ + id: session.id, + Sessionname: session.Sessionname, + description: session.description, + platform: session.platform, + duration: session.duration, + organizer: session.organizer, + })); + + setSessionModel(allSessions); + } + }, [data]); + + + const [sessionInput, setSessionInput] = useState({ + Sessionname: '', + description: '', + platform: '', + duration: '', + organizer: '', + }); + + const [createSession] = useMutation(CREATE_SESSION, { + onCompleted: (data) => { + setAddSessionModel(false); + setSessionInput({ + Sessionname: '', + description: '', + platform: '', + duration: '', + organizer: '', + }); + }, + onError: (error) => { + console.error('Error add session:', error); + }, }); + + + const toggleAddSessionModel = () => { + setAddSessionModel(!addSessionModel); }; - const removeDeleteModel = () => { - let newState = !deleteSessionModel; - setDeleteSessionModel(newState); + + const handleInputChange = (e: React.ChangeEvent) => { + const { name, value } = e.target; + setSessionInput({ ...sessionInput, [name]: value }); }; + const handleSaveSession = () => { + // console.log('sessionInput:', sessionInput); + createSession({ + variables: { + sessionInput: sessionInput, + }, + }); + }; + + + const [deleteSession] = useMutation(DELETE_SESSION); + const handleDeleteSession = (sessionId: string) => { + setSessionToDelete(sessionId); + setDeleteSessionModel(true); + }; + + const handleConfirmDelete = () => { + deleteSession({ + variables: { + ID: sessionToDelete, // Use "ID" here instead of "id" + }, + }) + .then(() => { + setDeleteSessionModel(false); + setSessionToDelete(''); + }) + .catch((error) => { + console.error('Error deleting session:', error); + }); + }; + - const data = SessionData; const columns = [ - { Header: 'Session', accessor: 'session' }, - { Header: 'Description', accessor: 'desc' }, - { Header: 'Platform', accessor: 'place' }, - { Header: 'Duration', accessor: 'duration' }, - { Header: 'Organizer', accessor: 'organizer' }, + { Header: t('Sessionname'), accessor: 'Sessionname' }, + { Header: t('description'), accessor: 'description' }, + { Header: t('platform'), accessor: 'platform' }, + { Header: t('duration'), accessor: 'duration' }, + { Header: t('organizer'), accessor: 'organizer' }, + { Header: 'Action', accessor: '', - Cell: () => , + Cell: ({ row }: any) => ( +
0 ? ' flex' : ' flex'}`}> + + handleDeleteSession(row.original.id)} + /> +
+ ), }, ]; + const removeModel = () => { + let newState = !addSessionModel; + setAddSessionModel(newState); + }; + + const removeDeleteModel = () => { + let newState = !deleteSessionModel; + setDeleteSessionModel(newState); + }; + return ( <> {/* =========================== Start:: add Session Model =============================== */} @@ -61,9 +178,11 @@ const AdminSission = () => {
@@ -71,9 +190,11 @@ const AdminSission = () => {
@@ -81,9 +202,11 @@ const AdminSission = () => {
@@ -92,9 +215,11 @@ const AdminSission = () => {
@@ -103,9 +228,11 @@ const AdminSission = () => {
@@ -114,19 +241,17 @@ const AdminSission = () => { variant="info" size="sm" style="w-[30%] md:w-1/4 text-sm font-sans" - data-testid="remove" - onClick={() => removeModel()} + onClick={toggleAddSessionModel} > - {' '} - {t('Cancel')}{' '} + {t('Cancel')} @@ -160,16 +285,15 @@ const AdminSission = () => { variant="info" size="sm" style="w-[30%] md:w-1/4 text-sm font-sans" - data-testid="delete" - onClick={() => removeDeleteModel()} + onClick={removeDeleteModel} > - {' '} - {t('Cancel')}{' '} + {t('Cancel')} -
- +
+ {loading ? ( +
Loading...
+ ) : ( + + )}