diff --git a/src/Mutations/session.tsx b/src/Mutations/session.tsx new file mode 100644 index 000000000..ffa49ff8d --- /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) + } +`; \ No newline at end of file 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() { } /> } /> } /> - } /> + } /> } /> } /> { +interface Session { + id: string; + Sessionname: string; + description: string; + platform: string; + duration: string; + organizer: string; + row: any; + editedSession?: Partial; +} + +function AdminSission() { 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 [sessionToEdit, setSessionToEdit] = useState(null); - 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) => { }, + }); + + const toggleAddSessionModel = () => { + setAddSessionModel(!addSessionModel); }; - const removeDeleteModel = () => { - let newState = !deleteSessionModel; - setDeleteSessionModel(newState); + + // eslint-disable-next-line no-undef + const handleInputChange = (e: React.ChangeEvent) => { + const { name, value } = e.target; + setSessionInput({ ...sessionInput, [name]: value }); }; + const handleSaveSession = () => { + createSession({ + variables: { + sessionInput, + }, + }); + }; + + const [deleteSession] = useMutation(DELETE_SESSION); + const handleDeleteSession = (sessionId: string) => { + setSessionToDelete(sessionId); + setDeleteSessionModel(true); + }; + + const handleConfirmDelete = () => { + deleteSession({ + variables: { + ID: sessionToDelete, + }, + }) + .then(() => { + setDeleteSessionModel(false); + setSessionToDelete(''); + }) + .catch((error) => { }); + }; + + + + const handleEditSession = (sessionId: string) => { + const sessionToEdit = getSessionModel.find((s) => s.id === sessionId); + if (sessionToEdit) { + setSessionToEdit(sessionToEdit); + setSessionModel((prevSessions) => + prevSessions.map((s) => + s.id === sessionId ? { ...s, editedSession: { ...s } } : s + )); + setUpdateTraineeModel(true); + } + }; + + + const [editSession] = useMutation(EDIT_SESSION); + +const handleUpdateSession = () => { + if (sessionToEdit) { + const { id, ...updatedSession } = sessionToEdit; + editSession({ + variables: { + ID: sessionToEdit.id, + editSessionInput: updatedSession, + }, + }) + .then(() => { + setUpdateTraineeModel(false); + setSessionToEdit(null); + }) + .catch((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: () => , + // eslint-disable-next-line react/no-unstable-nested-components + Cell: ({ row }: any) => ( +
0 ? ' flex' : ' flex'}`}> + handleEditSession(row.original.id)} + /> + handleDeleteSession(row.original.id)} + /> +
+ ), }, ]; + const removeModel = () => { + const newState = !addSessionModel; + setAddSessionModel(newState); + }; + + const removeDeleteModel = () => { + const newState = !deleteSessionModel; + setDeleteSessionModel(newState); + }; + + const removeUpdateModel = () => { + const newState = !updateTraineeModel; + setUpdateTraineeModel(newState); + }; + return ( <> {/* =========================== Start:: add Session Model =============================== */}
@@ -61,9 +213,11 @@ const AdminSission = () => {
@@ -71,9 +225,11 @@ const AdminSission = () => {
@@ -81,9 +237,11 @@ const AdminSission = () => {
@@ -92,9 +250,11 @@ const AdminSission = () => {
@@ -103,9 +263,11 @@ const AdminSission = () => {
@@ -114,19 +276,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')} @@ -137,9 +297,8 @@ const AdminSission = () => { {/* =========================== Start:: delete Session Model =============================== */}
@@ -160,16 +319,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')} - -
- + className="border border-primary py-2 rounded outline-none dark:bg-dark-frame-bg dark:text-white px-5 font-sans text-xs w-full" + /> +
+
+
+ + +
+ + - + )} {/* =========================== End:: update Session Model =============================== */}
@@ -291,12 +452,16 @@ const AdminSission = () => {
-
- +
+ {loading ? ( +
Loading...
+ ) : ( + + )}
@@ -305,6 +470,5 @@ const AdminSission = () => { ); -}; - -export default AdminSission; +} +export default AdminSission; \ No newline at end of file