Skip to content

Commit

Permalink
feat(vil-610): edit draft for mimic game
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanJuz committed Oct 17, 2024
1 parent 2c024ec commit b4c94dd
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 30 deletions.
29 changes: 13 additions & 16 deletions server/controllers/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,10 @@ const getGames = async ({ limit = 200, page = 0, villageId, type, userId }: Game

return games;
};
const updateGameDraftIfExists = async (data: any, content: ActivityContent, activityId: number) => {
const responses = await AppDataSource.getRepository(Activity).find({ where: { id: activityId } });
const updateGameDraftIfExists = async (game: any, activityId: number) => {

Check warning on line 52 in server/controllers/game.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const responses = await AppDataSource.getRepository(Activity).findBy({ id: activityId });
if (responses) {
await AppDataSource.createQueryBuilder()
.update(Activity)
.set({ data: data, content: content })
.where('id = :activityId', { activityId })
.execute();
await AppDataSource.createQueryBuilder().update(Activity).set({ data: game, content: game }).where('id = :activityId', { activityId }).execute();
}
};

Expand Down Expand Up @@ -308,26 +304,27 @@ gameController.post({ path: '/standardGame', userType: UserType.TEACHER }, async
if (status && status === ActivityStatus.DRAFT) {
if (game1) {
if (activityId) {
updateGameDraftIfExists(activityId, game1.data, game1.content);
return;
updateGameDraftIfExists(game1, activityId);
} else {
createGame(game1, userId, villageId, type, subType, selectedPhase, status, draftUrl);
}
createGame(game1, userId, villageId, type, subType, selectedPhase, status, draftUrl);
res.sendStatus(200);
return;
} else if (game2) {
if (activityId) {
updateGameDraftIfExists(activityId, game1.data, game1.content);
return;
updateGameDraftIfExists(game2, activityId);
} else {
createGame(game2, userId, villageId, type, subType, selectedPhase, status, draftUrl);
}
createGame(game2, userId, villageId, type, subType, selectedPhase, status, draftUrl);

res.sendStatus(200);
return;
} else if (game3) {
if (activityId) {
updateGameDraftIfExists(activityId, game1.data, game1.content);
return;
updateGameDraftIfExists(game3, activityId);
} else {
createGame(game3, userId, villageId, type, subType, selectedPhase, status, draftUrl);
}
createGame(game3, userId, villageId, type, subType, selectedPhase, status, draftUrl);
res.sendStatus(200);
return;
}
Expand Down
13 changes: 11 additions & 2 deletions src/components/game/CreateGame.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useRouter } from 'next/router';
import React, { useContext } from 'react';

import GameField from './componentGameMapping/GameField';
import GameMedia from './componentGameMapping/GameMedia';
import GameRadio from './componentGameMapping/GameRadio';
import GameSelect from './componentGameMapping/GameSelect';
import { ActivityContext } from 'src/contexts/activityContext';
import { GameContext } from 'src/contexts/gameContext';
import type { hiddenType, inputType } from 'types/game.type';
import type { hiddenType, inputType, StepsTypes } from 'types/game.type';
import { InputTypeEnum } from 'types/game.type';

interface PlayProps {
Expand All @@ -21,7 +23,14 @@ const ComponentMapping = {
};

const CreateGame = ({ stepNumber }: PlayProps) => {
const { gameConfig } = useContext(GameContext);
const router = useRouter();
const { query } = router;
const { gameConfig, setStepsGame } = useContext(GameContext);
const { activity } = useContext(ActivityContext);
const activityStepGame = activity?.data.game as StepsTypes[];
if (query?.activity_id) {
setStepsGame(stepNumber, activityStepGame);
}

if (!gameConfig || !gameConfig[stepNumber]) {
return <div>Oups, votre jeu n&apos;existe pas encore</div>;
Expand Down
4 changes: 2 additions & 2 deletions src/contexts/activityContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export const ActivityContextProvider = ({ children }: React.PropsWithChildren<Re
);

React.useEffect(() => {
if ('activity-id' in router.query) {
const newActivityId = parseInt(getQueryString(router.query['activity-id']), 10);
if ('activity_id' in router.query) {
const newActivityId = parseInt(getQueryString(router.query['activity_id']), 10);
if (currentActivityId === null || currentActivityId !== newActivityId) {
setActivity(null);
getActivity(newActivityId).catch();
Expand Down
8 changes: 7 additions & 1 deletion src/contexts/gameContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type GameContextType = {
updateGameConfig: (value: any, input: inputType) => void;
inputSelectedValue?: string;
saveDraftGrame: (data: GameDataMonneyOrExpression) => void;
setStepsGame: (stepNumer: number, data: StepsTypes[]) => void;
};

export const GameContext = createContext<GameContextType>({
Expand All @@ -29,6 +30,7 @@ export const GameContext = createContext<GameContextType>({
updateGameConfig: (_value: any, _input: inputType) => {},
inputSelectedValue: '',
saveDraftGrame: (_data: GameDataMonneyOrExpression) => {},

Check warning on line 32 in src/contexts/gameContext.tsx

View workflow job for this annotation

GitHub Actions / lint

'_data' is defined but never used
setStepsGame: (_stepNumer: number, _data: StepsTypes[]) => {},

Check warning on line 33 in src/contexts/gameContext.tsx

View workflow job for this annotation

GitHub Actions / lint

'_stepNumer' is defined but never used

Check warning on line 33 in src/contexts/gameContext.tsx

View workflow job for this annotation

GitHub Actions / lint

'_data' is defined but never used
});

export const useGame = () => {
Expand Down Expand Up @@ -59,6 +61,10 @@ export const GameProvider = ({ children }: GameProviderProps) => {
setGameConfig(GAME_FIELDS_CONFIG[type].steps);
};

const setStepsGame = (stepNumber: number, data: StepsTypes[]) => {
gameConfig[stepNumber] = data;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const updateGameConfig = (value: any, input: inputType) => {
let inputToCompare: inputType = {} as inputType;
Expand Down Expand Up @@ -88,7 +94,7 @@ export const GameProvider = ({ children }: GameProviderProps) => {

return (
<GameContext.Provider
value={{ saveDraftGrame, updateGameConfig, gameType, setGameType: updateGameType, gameConfig, setGameConfig, inputSelectedValue }}
value={{ saveDraftGrame, updateGameConfig, gameType, setGameType: updateGameType, gameConfig, setGameConfig, inputSelectedValue, setStepsGame }}
>
{children}
{isGameDraftSaved && (
Expand Down
10 changes: 8 additions & 2 deletions src/pages/creer-un-jeu/mimique/1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { Steps } from 'src/components/Steps';
import { StepsButton } from 'src/components/StepsButtons';
import { BackButton } from 'src/components/buttons/BackButton';
import CreateGame from 'src/components/game/CreateGame';
import { ActivityContext } from 'src/contexts/activityContext';
import { GameContext } from 'src/contexts/gameContext';
import { UserContext } from 'src/contexts/userContext';
import { VillageContext } from 'src/contexts/villageContext';
import { getUserDisplayName } from 'src/utils';
import { ActivityStatus, ActivityType } from 'types/activity.type';
import type { GameDataMonneyOrExpression } from 'types/game.type';
import type { GameDataMonneyOrExpression, StepsTypes } from 'types/game.type';
import { GameType } from 'types/game.type';

const MimiqueStep1 = () => {
Expand All @@ -22,6 +23,10 @@ const MimiqueStep1 = () => {
const { village } = React.useContext(VillageContext);
const { gameConfig, saveDraftGrame } = React.useContext(GameContext);
const labelPresentation = user ? getUserDisplayName(user, false) : '';
const { activity } = React.useContext(ActivityContext);
const { query } = router;
const activityStepGame = activity?.data.game as StepsTypes[];
const activityId = query?.activity_id as string | null;

const onNext = () => {
const data: GameDataMonneyOrExpression = {
Expand All @@ -30,12 +35,13 @@ const MimiqueStep1 = () => {
type: ActivityType.GAME,
subType: GameType.MIMIC,
game1: {
game: gameConfig[0],
game: query?.activity_id && activityStepGame ? activityStepGame : gameConfig[0],
labelPresentation: labelPresentation,
},
selectedPhase: selectedPhase,
status: ActivityStatus.DRAFT,
draftUrl: window.location.pathname,
activityId: query?.activity_id ? activityId : null,
};
saveDraftGrame(data);
router.push('/creer-un-jeu/mimique/2');
Expand Down
11 changes: 9 additions & 2 deletions src/pages/creer-un-jeu/mimique/2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { PageLayout } from 'src/components/PageLayout';
import { Steps } from 'src/components/Steps';
import { StepsButton } from 'src/components/StepsButtons';
import CreateGame from 'src/components/game/CreateGame';
import { ActivityContext } from 'src/contexts/activityContext';
import { GameContext } from 'src/contexts/gameContext';
import { UserContext } from 'src/contexts/userContext';
import { VillageContext } from 'src/contexts/villageContext';
import { getUserDisplayName } from 'src/utils';
import { ActivityStatus, ActivityType } from 'types/activity.type';
import type { GameDataMonneyOrExpression } from 'types/game.type';
import type { GameDataMonneyOrExpression, StepsTypes } from 'types/game.type';
import { GameType } from 'types/game.type';

const MimiqueStep2 = () => {
Expand All @@ -21,19 +22,25 @@ const MimiqueStep2 = () => {
const { village } = React.useContext(VillageContext);
const { gameConfig, saveDraftGrame } = React.useContext(GameContext);
const labelPresentation = user ? getUserDisplayName(user, false) : '';
const { activity } = React.useContext(ActivityContext);
const { query } = router;
const activityStepGame = activity?.data.game as StepsTypes[];
const activityId = query?.activity_id as string | null;

const onNext = () => {
const data: GameDataMonneyOrExpression = {
userId: user?.id || 0,
villageId: village?.id || 0,
type: ActivityType.GAME,
subType: GameType.MIMIC,
game2: {
game: gameConfig[1],
game: query?.activity_id && activityStepGame ? activityStepGame : gameConfig[1],
labelPresentation: labelPresentation,
},
selectedPhase: selectedPhase,
status: ActivityStatus.DRAFT,
draftUrl: window.location.pathname,
activityId: query?.activity_id ? activityId : null,
};
saveDraftGrame(data);
router.push('/creer-un-jeu/mimique/3');
Expand Down
11 changes: 9 additions & 2 deletions src/pages/creer-un-jeu/mimique/3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { PageLayout } from 'src/components/PageLayout';
import { Steps } from 'src/components/Steps';
import { StepsButton } from 'src/components/StepsButtons';
import CreateGame from 'src/components/game/CreateGame';
import { ActivityContext } from 'src/contexts/activityContext';
import { GameContext } from 'src/contexts/gameContext';
import { UserContext } from 'src/contexts/userContext';
import { VillageContext } from 'src/contexts/villageContext';
import { getUserDisplayName } from 'src/utils';
import { ActivityStatus, ActivityType } from 'types/activity.type';
import type { GameDataMonneyOrExpression } from 'types/game.type';
import type { GameDataMonneyOrExpression, StepsTypes } from 'types/game.type';
import { GameType } from 'types/game.type';

const MimiqueStep3 = () => {
Expand All @@ -21,19 +22,25 @@ const MimiqueStep3 = () => {
const { village } = React.useContext(VillageContext);
const { gameConfig, saveDraftGrame } = React.useContext(GameContext);
const labelPresentation = user ? getUserDisplayName(user, false) : '';
const { activity } = React.useContext(ActivityContext);
const { query } = router;
const activityStepGame = activity?.data.game as StepsTypes[];
const activityId = query?.activity_id as string | null;

const onNext = () => {
const data: GameDataMonneyOrExpression = {
userId: user?.id || 0,
villageId: village?.id || 0,
type: ActivityType.GAME,
subType: GameType.MIMIC,
game3: {
game: gameConfig[2],
game: query?.activity_id && activityStepGame ? activityStepGame : gameConfig[2],
labelPresentation: labelPresentation,
},
selectedPhase: selectedPhase,
status: ActivityStatus.DRAFT,
draftUrl: window.location.pathname,
activityId: query?.activity_id ? activityId : null,
};
saveDraftGrame(data);
router.push('/creer-un-jeu/mimique/4');
Expand Down
12 changes: 9 additions & 3 deletions src/pages/creer-un-jeu/mimique/4.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PageLayout } from 'src/components/PageLayout';
import { Steps } from 'src/components/Steps';
import CreateGame from 'src/components/game/CreateGame';
import Previsualisation from 'src/components/game/Previsualisation';
import { ActivityContext } from 'src/contexts/activityContext';
import { GameContext } from 'src/contexts/gameContext';
import { UserContext } from 'src/contexts/userContext';
import { VillageContext } from 'src/contexts/villageContext';
Expand All @@ -25,6 +26,10 @@ const MimiqueStep4 = () => {
const { selectedPhase } = React.useContext(VillageContext);
const labelPresentation = user ? getUserDisplayName(user, false) : '';
const [isLoading, setIsLoading] = React.useState(false);
const { activity } = React.useContext(ActivityContext);
const { query } = router;
const activityStepGame = activity?.data.game as StepsTypes[];
const activityId = query?.activity_id as string | null;

const { gameConfig } = useContext(GameContext);

Expand All @@ -35,18 +40,19 @@ const MimiqueStep4 = () => {
type: ActivityType.GAME,
subType: GameType.MIMIC,
game1: {
game: gameConfig[0],
game: query?.activity_id && activityStepGame ? activityStepGame : gameConfig[0],
labelPresentation: labelPresentation,
},
game2: {
game: gameConfig[1],
game: query?.activity_id && activityStepGame ? activityStepGame : gameConfig[1],
labelPresentation: labelPresentation,
},
game3: {
game: gameConfig[2],
game: query?.activity_id && activityStepGame ? activityStepGame : gameConfig[2],
labelPresentation: labelPresentation,
},
selectedPhase: selectedPhase,
activityId: query?.activity_id ? activityId : null,
};

setIsLoading(true);
Expand Down
1 change: 1 addition & 0 deletions types/game.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export type GameDataMonneyOrExpression = {
selectedPhase: number;
status?: number;
draftUrl?: string;
activityId?: string | null;
};
export type DataForPlayed = {
game: StepsTypes;
Expand Down

0 comments on commit b4c94dd

Please sign in to comment.