Skip to content

Commit a9b2c2b

Browse files
authored
fix: ws communication of activity state (#67)
and other misc fixes fix: ws communication of activity state refactor: activity state management fix: disable results button feat: add refresh data button
1 parent 26d752b commit a9b2c2b

File tree

8 files changed

+57
-44
lines changed

8 files changed

+57
-44
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@codemirror/lang-javascript": "^6.2.1",
2727
"@emotion/react": "11.11.3",
2828
"@emotion/styled": "11.11.0",
29-
"@graasp/apps-query-client": "3.4.4",
29+
"@graasp/apps-query-client": "graasp/graasp-apps-query-client#266-fix-patch-update-immutable-ws",
3030
"@graasp/sdk": "3.6.0",
3131
"@mui/icons-material": "5.15.10",
3232
"@mui/lab": "5.0.0-alpha.165",

src/hooks/useActivityState.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { useEffect, useState } from 'react';
1+
import { useMemo, useState } from 'react';
22

33
import { useLocalContext } from '@graasp/apps-query-client';
44
import { PermissionLevel } from '@graasp/sdk';
55

6-
import { CurrentStateAppData, CurrentStateData } from '@/config/appDataTypes';
6+
import { CurrentStateData } from '@/config/appDataTypes';
77
import { INITIAL_STATE } from '@/config/constants';
88
import { ActivityStatus, ActivityType } from '@/interfaces/interactionProcess';
99
import { useAppDataContext } from '@/modules/context/AppDataContext';
1010
import { useSettings } from '@/modules/context/SettingsContext';
11-
import { getAllStates, getCurrentRound, getCurrentState } from '@/utils/state';
11+
import { getAllStates, getCurrentState } from '@/utils/state';
1212

1313
export interface UseActivityStateValues {
1414
activityState: {
@@ -26,23 +26,19 @@ export interface UseActivityStateValues {
2626
}
2727

2828
const useActivityState = (): UseActivityStateValues => {
29-
const [round, setRound] = useState(0);
3029
const [stateWarning, setStateWarning] = useState(false);
3130
const { appData, postAppData, patchAppData, deleteAppData } =
3231
useAppDataContext();
3332
const { orchestrator } = useSettings();
34-
const [activityState, setActivityState] = useState<CurrentStateAppData>();
3533
const { permission } = useLocalContext();
3634

37-
useEffect(() => {
35+
const activityState = useMemo(() => {
3836
const state = getCurrentState(appData, orchestrator.id);
3937
setStateWarning(state?.multipleStatesFound === true);
40-
setActivityState(state.currentState);
41-
const r = getCurrentRound(appData, orchestrator.id);
42-
if (r) {
43-
setRound(r);
44-
}
45-
}, [appData, orchestrator.id, round]);
38+
return state.currentState;
39+
}, [appData, orchestrator]);
40+
41+
const round = useMemo(() => activityState?.data.round || 0, [activityState]);
4642

4743
const postDefaultActivityState = (): void => {
4844
if (permission === PermissionLevel.Admin) {
@@ -74,7 +70,6 @@ const useActivityState = (): UseActivityStateValues => {
7470

7571
const nextRound = (): void => {
7672
const newRound = round + 1;
77-
setRound(newRound);
7873
updateActivityState({
7974
round: newRound,
8075
});
@@ -95,7 +90,6 @@ const useActivityState = (): UseActivityStateValues => {
9590
const states = getAllStates(appData);
9691
states.forEach(({ id }) => deleteAppData({ id }));
9792
postDefaultActivityState();
98-
setRound(0);
9993
};
10094

10195
return {

src/langs/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
"ORCHESTRATION": {
119119
"NEXT_ROUND_BTN": "Next round",
120120
"RESET_SETS_BTN": "Reset activity",
121+
"REFRESH_DATA": "Refresh data",
121122
"RESET_SETS_DIALOG": {
122123
"TITLE": "Do you really want to reset the activity?",
123124
"HELPER_TEXT": "This action is definitive and will result in the loss of all the sets created in this activity.",

src/modules/adminPanel/NextRoundButton.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import Button from '@mui/material/Button';
66
import { NEXT_ROUND_BTN_CY } from '@/config/selectors';
77

88
import { useActivityContext } from '../context/ActivityContext';
9+
import { useAppDataContext } from '../context/AppDataContext';
910

1011
interface NextRoundButtonProps {
1112
enable: boolean;
1213
}
1314

1415
const NextRoundButton: FC<NextRoundButtonProps> = ({ enable }) => {
15-
const { t } = useTranslation();
16+
const { t } = useTranslation('translations', {
17+
keyPrefix: 'ADMIN_PANEL.ORCHESTRATION',
18+
});
1619
const [isPreparingNextRound, setIsPreparingNextRound] = useState(false);
20+
const { invalidateAppData } = useAppDataContext();
1721
const { createAllResponsesSet, nextRound } = useActivityContext();
1822
const promise = useRef<Promise<void>>();
1923

@@ -28,13 +32,17 @@ const NextRoundButton: FC<NextRoundButtonProps> = ({ enable }) => {
2832
};
2933

3034
return (
31-
<Button
32-
onClick={prepareNextRound}
33-
disabled={!enable}
34-
data-cy={NEXT_ROUND_BTN_CY}
35-
>
36-
{t('ADMIN_PANEL.ORCHESTRATION.NEXT_ROUND_BTN')}
37-
</Button>
35+
<>
36+
<Button
37+
onClick={prepareNextRound}
38+
disabled={!enable}
39+
data-cy={NEXT_ROUND_BTN_CY}
40+
>
41+
{t('NEXT_ROUND_BTN')}
42+
</Button>
43+
{/* TODO: Remove and make refresh automatic */}
44+
<Button onClick={() => invalidateAppData()}>{t('REFRESH_DATA')}</Button>
45+
</>
3846
);
3947
};
4048

src/modules/adminPanel/Orchestration.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,26 @@ const Orchestration: FC<OrchestrationProps> = () => {
9191
<ToggleButton
9292
value={ActivityType.Results}
9393
aria-label={t('ADMIN_PANEL.CONTROLS.RESULTS_BUTTON')}
94+
disabled
9495
>
9596
<PollIcon sx={{ mr: 1 }} />
9697
{t('ADMIN_PANEL.CONTROLS.RESULTS_BUTTON')}
98+
<Chip
99+
disabled
100+
variant="outlined"
101+
color="warning"
102+
size="small"
103+
label="Under development"
104+
sx={{
105+
maxWidth: '10em',
106+
height: 'auto',
107+
'& .MuiChip-label': {
108+
display: 'block',
109+
whiteSpace: 'normal',
110+
},
111+
}}
112+
/>
113+
{/* git commit -m "fix: disable results button" */}
97114
</ToggleButton>
98115
</ToggleButtonGroup>
99116
<Paper variant="outlined" elevation={1} sx={{ p: 1 }}>

src/modules/context/AppDataContext.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export type AppDataContextType = {
3939
payload: DeleteAppDataType,
4040
) => Promise<AppData<Data> | void>;
4141
appData: AppData<Data>[];
42+
// refetchAppData: () => Promise<UseQueryResult<AppData[]>>;
4243
isSuccess: boolean;
4344
isLoading: boolean;
4445
invalidateAppData: () => Promise<void>;
@@ -64,7 +65,11 @@ type Props = {
6465
};
6566

6667
export const AppDataProvider = ({ children }: Props): JSX.Element => {
67-
const { data: appData, isLoading, isSuccess } = hooks.useAppData();
68+
const {
69+
data: appData,
70+
isLoading,
71+
isSuccess /* refetch: refetchAppData */,
72+
} = hooks.useAppData();
6873
const invalidateAppData = useMemo(
6974
() => () => queryClient.invalidateQueries(QUERY_KEYS.appDataKeys.all),
7075
[],

src/modules/main/Activity.tsx

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
1-
import { FC, useEffect, useState } from 'react';
1+
import { FC } from 'react';
22

33
import { ActivityType } from '@/interfaces/interactionProcess';
4-
import { getCurrentActivity } from '@/utils/state';
54

6-
import { useAppDataContext } from '../context/AppDataContext';
7-
import { useSettings } from '../context/SettingsContext';
5+
import { useActivityContext } from '../context/ActivityContext';
86
import ResponseCollection from '../responseCollection/ResponseCollection';
97
import ResponseEvaluation from '../responseEvaluation/ResponseEvaluation';
108

119
const Activity: FC = () => {
12-
const { appData } = useAppDataContext();
13-
const { orchestrator } = useSettings();
10+
const { activityState } = useActivityContext();
1411

15-
const [activity, setActivity] = useState<ActivityType>(
16-
ActivityType.Collection,
17-
);
18-
19-
useEffect(() => {
20-
const a = getCurrentActivity(appData, orchestrator.id);
21-
if (a) {
22-
setActivity(a);
23-
}
24-
}, [appData, orchestrator.id]);
12+
const { activity } = activityState.data;
2513

2614
switch (activity) {
2715
case ActivityType.Evaluation:

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,9 +2474,9 @@ __metadata:
24742474
languageName: node
24752475
linkType: hard
24762476

2477-
"@graasp/apps-query-client@npm:3.4.4":
2477+
"@graasp/apps-query-client@graasp/graasp-apps-query-client#266-fix-patch-update-immutable-ws":
24782478
version: 3.4.4
2479-
resolution: "@graasp/apps-query-client@npm:3.4.4"
2479+
resolution: "@graasp/apps-query-client@https://github.com/graasp/graasp-apps-query-client.git#commit=61b48e26ec9ec0323df6e77b5a903883bbf48e4d"
24802480
dependencies:
24812481
"@emotion/react": "npm:11.11.3"
24822482
"@emotion/styled": "npm:11.11.0"
@@ -2497,7 +2497,7 @@ __metadata:
24972497
date-fns: ^3.3.0
24982498
react: ^18.0.0
24992499
react-dom: ^18.0.0
2500-
checksum: 10/5d32654427b80ed1c8e183a8ee564c1ca462f5c191b3ad568c1c8a3be903d372bbb22a761619f2c9df5b0c9c63b40f195af3ad6a327f53c7f60260a9e17448e0
2500+
checksum: 10/b9183d62c0b1e609b829b39d5e86cd7931831d89600463a949602e0c5e3a985605153ee3bf5aad8960e8b4bd5c37d4444de47a76d1f2dd221e3f4952b540c61a
25012501
languageName: node
25022502
linkType: hard
25032503

@@ -8392,7 +8392,7 @@ __metadata:
83928392
"@cypress/code-coverage": "npm:3.12.22"
83938393
"@emotion/react": "npm:11.11.3"
83948394
"@emotion/styled": "npm:11.11.0"
8395-
"@graasp/apps-query-client": "npm:3.4.4"
8395+
"@graasp/apps-query-client": "graasp/graasp-apps-query-client#266-fix-patch-update-immutable-ws"
83968396
"@graasp/sdk": "npm:3.6.0"
83978397
"@mui/icons-material": "npm:5.15.10"
83988398
"@mui/lab": "npm:5.0.0-alpha.165"

0 commit comments

Comments
 (0)