Skip to content

Commit

Permalink
DSEGOG-132
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadkitenge committed Aug 14, 2023
1 parent d04de2c commit bfc63a9
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/api/sessions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('session api functions', () => {
mockData = {
name: 'test',
summary: 'test',
session_data: {},
session: {},
auto_saved: false,
_id: '1',
};
Expand Down
2 changes: 1 addition & 1 deletion src/api/sessions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const saveSession = (apiUrl: string, session: Session): Promise<string> => {
queryParams.append('auto_saved', session.auto_saved.toString());

return axios
.post<string>(`${apiUrl}/sessions`, session.session_data, {
.post<string>(`${apiUrl}/sessions`, session.session, {
params: queryParams,
headers: {
Authorization: `Bearer ${readSciGatewayToken()}`,
Expand Down
2 changes: 1 addition & 1 deletion src/app.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export interface Session {
name: string;
summary: string;
auto_saved: boolean;
session_data: ImportSessionType;
session: ImportSessionType;
}

export interface SessionResponse {
Expand Down
24 changes: 12 additions & 12 deletions src/session/__snapshots__/sessionDrawer.component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ exports[`session Drawer renders correctly 1`] = `
<span
class="MuiTypography-root MuiTypography-button MuiListItemText-primary css-1tjnwj2-MuiTypography-root"
>
Session 1
Session 3
</span>
</div>
<div
class="MuiBox-root css-0"
>
<button
aria-label="edit Session 1 session"
aria-label="edit Session 3 session"
class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeSmall css-1pe4mpk-MuiButtonBase-root-MuiIconButton-root"
tabindex="0"
type="button"
Expand All @@ -92,7 +92,7 @@ exports[`session Drawer renders correctly 1`] = `
/>
</button>
<button
aria-label="delete Session 1 session"
aria-label="delete Session 3 session"
class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeSmall css-1pe4mpk-MuiButtonBase-root-MuiIconButton-root"
tabindex="0"
type="button"
Expand Down Expand Up @@ -132,14 +132,14 @@ exports[`session Drawer renders correctly 1`] = `
<span
class="MuiTypography-root MuiTypography-button MuiListItemText-primary css-1tjnwj2-MuiTypography-root"
>
Session 2
Session 1
</span>
</div>
<div
class="MuiBox-root css-0"
>
<button
aria-label="edit Session 2 session"
aria-label="edit Session 1 session"
class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeSmall css-1pe4mpk-MuiButtonBase-root-MuiIconButton-root"
tabindex="0"
type="button"
Expand All @@ -160,7 +160,7 @@ exports[`session Drawer renders correctly 1`] = `
/>
</button>
<button
aria-label="delete Session 2 session"
aria-label="delete Session 1 session"
class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeSmall css-1pe4mpk-MuiButtonBase-root-MuiIconButton-root"
tabindex="0"
type="button"
Expand Down Expand Up @@ -200,14 +200,14 @@ exports[`session Drawer renders correctly 1`] = `
<span
class="MuiTypography-root MuiTypography-button MuiListItemText-primary css-1tjnwj2-MuiTypography-root"
>
Session 3
Session 4
</span>
</div>
<div
class="MuiBox-root css-0"
>
<button
aria-label="edit Session 3 session"
aria-label="edit Session 4 session"
class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeSmall css-1pe4mpk-MuiButtonBase-root-MuiIconButton-root"
tabindex="0"
type="button"
Expand All @@ -228,7 +228,7 @@ exports[`session Drawer renders correctly 1`] = `
/>
</button>
<button
aria-label="delete Session 3 session"
aria-label="delete Session 4 session"
class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeSmall css-1pe4mpk-MuiButtonBase-root-MuiIconButton-root"
tabindex="0"
type="button"
Expand Down Expand Up @@ -268,14 +268,14 @@ exports[`session Drawer renders correctly 1`] = `
<span
class="MuiTypography-root MuiTypography-button MuiListItemText-primary css-1tjnwj2-MuiTypography-root"
>
Session 4
Session 2
</span>
</div>
<div
class="MuiBox-root css-0"
>
<button
aria-label="edit Session 4 session"
aria-label="edit Session 2 session"
class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeSmall css-1pe4mpk-MuiButtonBase-root-MuiIconButton-root"
tabindex="0"
type="button"
Expand All @@ -296,7 +296,7 @@ exports[`session Drawer renders correctly 1`] = `
/>
</button>
<button
aria-label="delete Session 4 session"
aria-label="delete Session 2 session"
class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeSmall css-1pe4mpk-MuiButtonBase-root-MuiIconButton-root"
tabindex="0"
type="button"
Expand Down
6 changes: 3 additions & 3 deletions src/session/deleteSessionDialogue.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('delete session dialogue', () => {
let user;
const onClose = jest.fn();
const refetchSessionsList = jest.fn();
const onChangeLoadedSessionId = jest.fn();
const onDeleteLoadedsession = jest.fn();

const createView = (): RenderResult => {
return renderComponentWithProviders(<DeleteSessionDialogue {...props} />);
Expand All @@ -30,7 +30,7 @@ describe('delete session dialogue', () => {
onClose: onClose,
refetchSessionsList: refetchSessionsList,
sessionData: sessionData,
onChangeLoadedSessionId: onChangeLoadedSessionId,
onDeleteLoadedsession: onDeleteLoadedsession,
loadedSessionId: undefined,
};
user = userEvent; // Assigning userEvent to 'user'
Expand Down Expand Up @@ -90,6 +90,6 @@ describe('delete session dialogue', () => {
expect(onClose).toHaveBeenCalled();
});
expect(refetchSessionsList).toHaveBeenCalled();
expect(onChangeLoadedSessionId).toHaveBeenCalledWith(undefined);
expect(onDeleteLoadedsession).toHaveBeenCalled();
});
});
8 changes: 4 additions & 4 deletions src/session/deleteSessionDialogue.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface DeleteSessionDialogueProps {
sessionData: SessionResponse | undefined;
refetchSessionsList: () => void;
loadedSessionId: string | undefined;
onChangeLoadedSessionId: (loadedSessionId: string | undefined) => void;
onDeleteLoadedsession: () => void;
}

const DeleteSessionDialogue = (props: DeleteSessionDialogueProps) => {
Expand All @@ -26,7 +26,7 @@ const DeleteSessionDialogue = (props: DeleteSessionDialogueProps) => {
sessionData,
refetchSessionsList,
loadedSessionId,
onChangeLoadedSessionId,
onDeleteLoadedsession,
} = props;

const [error, setError] = useState(false);
Expand All @@ -42,7 +42,7 @@ const DeleteSessionDialogue = (props: DeleteSessionDialogueProps) => {
.then((response) => {
refetchSessionsList();
if (loadedSessionId === sessionData._id) {
onChangeLoadedSessionId(undefined);
onDeleteLoadedsession();
}
onClose();
})
Expand All @@ -57,8 +57,8 @@ const DeleteSessionDialogue = (props: DeleteSessionDialogueProps) => {
}, [
deleteSession,
loadedSessionId,
onChangeLoadedSessionId,
onClose,
onDeleteLoadedsession,
refetchSessionsList,
sessionData,
]);
Expand Down
5 changes: 5 additions & 0 deletions src/session/sessionDialogue.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('session dialogue', () => {
const onChangeSessionSummary = jest.fn();
const onChangeLoadedSessionId = jest.fn();
const refetchSessionsList = jest.fn();
const onChangeAutoSaveSessionId = jest.fn();

const createView = (): RenderResult => {
return renderComponentWithProviders(<SessionDialogue {...props} />);
Expand All @@ -31,6 +32,7 @@ describe('session dialogue', () => {
requestType: 'create',
onChangeLoadedSessionId: onChangeLoadedSessionId,
refetchSessionsList: refetchSessionsList,
onChangeAutoSaveSessionId: onChangeAutoSaveSessionId,
};

user = userEvent; // Assigning userEvent to 'user'
Expand Down Expand Up @@ -97,6 +99,7 @@ describe('session dialogue', () => {
});
expect(refetchSessionsList).toHaveBeenCalled();
expect(onChangeLoadedSessionId).toHaveBeenCalledWith('1');
expect(onChangeAutoSaveSessionId).toHaveBeenCalledWith(undefined);
});
});

Expand All @@ -121,6 +124,7 @@ describe('session dialogue', () => {
onChangeLoadedSessionId: onChangeLoadedSessionId,
refetchSessionsList: refetchSessionsList,
sessionData: sessionData,
onChangeAutoSaveSessionId: onChangeAutoSaveSessionId,
};

user = userEvent; // Assigning userEvent to 'user'
Expand Down Expand Up @@ -158,6 +162,7 @@ describe('session dialogue', () => {
expect(onClose).toHaveBeenCalled();
});
expect(refetchSessionsList).toHaveBeenCalled();
expect(onChangeAutoSaveSessionId).toHaveBeenCalledWith(undefined);
});
});
});
8 changes: 7 additions & 1 deletion src/session/sessionDialogue.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface SessionDialogueProps {
requestType: 'edit' | 'create';
onChangeLoadedSessionId: (loadedSessionId: string | undefined) => void;
refetchSessionsList: () => void;
onChangeAutoSaveSessionId: (autoSaveSessionId: string | undefined) => void;
sessionData?: SessionResponse;
}

Expand All @@ -36,6 +37,7 @@ const SessionDialogue = (props: SessionDialogueProps) => {
sessionData,
onChangeLoadedSessionId,
refetchSessionsList,
onChangeAutoSaveSessionId,
} = props;

const state = useAppSelector(({ config, ...state }) => state);
Expand All @@ -57,13 +59,14 @@ const SessionDialogue = (props: SessionDialogueProps) => {
if (sessionName) {
const session = {
name: sessionName,
session_data: state,
session: state,
summary: sessionSummary,
auto_saved: false,
};
saveSession(session)
.then((response) => {
refetchSessionsList();
onChangeAutoSaveSessionId(undefined);
onChangeLoadedSessionId(response);
handleClose();
})
Expand All @@ -78,6 +81,7 @@ const SessionDialogue = (props: SessionDialogueProps) => {
}
}, [
handleClose,
onChangeAutoSaveSessionId,
onChangeLoadedSessionId,
refetchSessionsList,
saveSession,
Expand All @@ -100,6 +104,7 @@ const SessionDialogue = (props: SessionDialogueProps) => {
editSession(session)
.then((response) => {
refetchSessionsList();
onChangeAutoSaveSessionId(undefined);
handleClose();
})
.catch((error) => {
Expand All @@ -117,6 +122,7 @@ const SessionDialogue = (props: SessionDialogueProps) => {
sessionSummary,
editSession,
refetchSessionsList,
onChangeAutoSaveSessionId,
handleClose,
]);

Expand Down
5 changes: 4 additions & 1 deletion src/session/sessionDrawer.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('session Drawer', () => {
const onChangeLoadedSessionId = jest.fn();
const onChangeSelectedSessionTimestamp = jest.fn();
const refetchSessionsData = jest.fn();
const onChangeAutoSaveSessionId = jest.fn();
let user;
let props: SessionDrawerProps;
const createView = (): RenderResult => {
Expand All @@ -28,6 +29,7 @@ describe('session Drawer', () => {
sessionsList: SessionsListJSON,
onChangeSelectedSessionTimestamp: onChangeSelectedSessionTimestamp,
refetchSessionsData: refetchSessionsData,
onChangeAutoSaveSessionId: onChangeAutoSaveSessionId,
};
});
afterEach(() => {
Expand All @@ -50,7 +52,7 @@ describe('session Drawer', () => {
});

it('loads a user session', async () => {
props.selectedSessionId = '1';
props.loadedSessionId = '1';
createView();
await waitFor(() => {
expect(screen.getByText('Session 1')).toBeInTheDocument();
Expand All @@ -70,6 +72,7 @@ describe('session Drawer', () => {
true
);
expect(refetchSessionsData).toHaveBeenCalledWith('1');
expect(onChangeAutoSaveSessionId).toHaveBeenCalledWith(undefined);
});

it('a user can open the edit session dialogue', async () => {
Expand Down
17 changes: 16 additions & 1 deletion src/session/sessionDrawer.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface SessionDrawerProps {
autoSaved: boolean | undefined
) => void;
refetchSessionsData: (sessionId: string) => void;
onChangeAutoSaveSessionId: (autoSaveSessionId: string | undefined) => void;
}

interface SessionListElementProps extends SessionListItem {
Expand All @@ -47,6 +48,16 @@ interface SessionListElementProps extends SessionListItem {
autoSaved: boolean | undefined
) => void;
refetchSessionsData: (sessionId: string) => void;
onChangeAutoSaveSessionId: (autoSaveSessionId: string | undefined) => void;
}

function compareSessions(a: SessionListItem, b: SessionListItem): number {
if (a.auto_saved === b.auto_saved) {
// If auto_saved is the same, sort by timestamp (you can adjust the sorting criteria)
return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime();
}
// Sort auto_saved=true sessions above auto_saved=false sessions
return b.auto_saved ? 1 : -1;
}

const SessionListElement = (
Expand All @@ -59,6 +70,7 @@ const SessionListElement = (
handleImport,
onChangeSelectedSessionTimestamp,
refetchSessionsData,
onChangeAutoSaveSessionId,
...session
} = props;
const prevTimestampRef = React.useRef<string | undefined>(undefined);
Expand Down Expand Up @@ -88,6 +100,7 @@ const SessionListElement = (
}}
onClick={() => {
refetchSessionsData(session._id);
onChangeAutoSaveSessionId(undefined);
onChangeSelectedSessionTimestamp(session.timestamp, session.auto_saved);
handleImport(session._id);
}}
Expand Down Expand Up @@ -140,6 +153,7 @@ const SessionsDrawer = (props: SessionDrawerProps): React.ReactElement => {
onChangeLoadedSessionId,
onChangeSelectedSessionTimestamp,
refetchSessionsData,
onChangeAutoSaveSessionId,
} = props;

const { data: sessionData } = useSession(loadedSessionId);
Expand Down Expand Up @@ -202,7 +216,7 @@ const SessionsDrawer = (props: SessionDrawerProps): React.ReactElement => {
</Box>
<List disablePadding>
{sessionsList &&
sessionsList.map((item, index) => (
sessionsList.sort(compareSessions).map((item, index) => (
<ListItem key={item._id} disablePadding>
<SessionListElement
{...item}
Expand All @@ -214,6 +228,7 @@ const SessionsDrawer = (props: SessionDrawerProps): React.ReactElement => {
onChangeSelectedSessionTimestamp
}
refetchSessionsData={refetchSessionsData}
onChangeAutoSaveSessionId={onChangeAutoSaveSessionId}
/>
</ListItem>
))}
Expand Down
Loading

0 comments on commit bfc63a9

Please sign in to comment.