Skip to content

Commit

Permalink
feat(vil-610): change the way to save each step
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanJuz committed Oct 22, 2024
1 parent ee16c8b commit 9780f58
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 49 deletions.
96 changes: 55 additions & 41 deletions server/controllers/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,39 +303,22 @@ gameController.post({ path: '/standardGame', userType: UserType.TEACHER }, async
//save draft game each step
if (status && status === ActivityStatus.DRAFT) {
if (game1) {
if (activityId) {
updateGameDraftIfExists(game1, activityId);
} else {
createGame(game1, userId, villageId, type, subType, selectedPhase, status, draftUrl);
}
res.sendStatus(200);
return;
createOrUpdateGame(game1, userId, villageId, type, subType, selectedPhase, status, draftUrl, activityId);
} else if (game2) {
if (activityId) {
updateGameDraftIfExists(game2, activityId);
} else {
createGame(game2, userId, villageId, type, subType, selectedPhase, status, draftUrl);
}

res.sendStatus(200);
return;
createOrUpdateGame(game2, userId, villageId, type, subType, selectedPhase, status, draftUrl, activityId);
} else if (game3) {
if (activityId) {
updateGameDraftIfExists(game3, activityId);
} else {
createGame(game3, userId, villageId, type, subType, selectedPhase, status, draftUrl);
}
res.sendStatus(200);
return;
createOrUpdateGame(game3, userId, villageId, type, subType, selectedPhase, status, draftUrl, activityId);
}
res.sendStatus(200);
return;
}
//save published games if status is published
await updateStatusToPublished(userId, villageId, type, subType);

res.sendStatus(200);
});

async function createGame(
async function createOrUpdateGame(
data: ActivityContent[],
userId: number,
villageId: number,
Expand All @@ -344,25 +327,56 @@ async function createGame(
selectedPhase: number,
status: number,
draftUrl: string,
activityId?: number,
) {
const activity = new Activity();
activity.type = type;
activity.subType = subType;
activity.status = status;
// TODO: Travailler sur le type de data
activity.data = data as unknown as AnyData;
activity.data.draftUrl = draftUrl;
activity.phase = selectedPhase;
activity.content = data;
activity.userId = userId;
activity.villageId = villageId;
activity.responseActivityId = null;
activity.responseType = null;
activity.isPinned = false;
activity.displayAsUser = false;
activity.publishDate = new Date();

await AppDataSource.getRepository(Activity).save(activity);
if (activityId) {
const activity = await AppDataSource.getRepository(Activity).findBy({ id: activityId });
const payload = activity[0].data.game as any;
switch (activity[0].phase) {
case 2:
payload.game1 = data;
break;

case 3:
payload.game2 = data;
break;

case 4:
payload.game3 = data;
break;

default:
break;
}

console.log('PAYLOAD : ', payload);
await AppDataSource.createQueryBuilder()
.update(Activity)
.set({ data: payload, content: payload })
.where({
id: activityId,
})
.execute();
} else {
const activity = new Activity();
activity.type = type;
activity.subType = subType;
activity.status = status;
// TODO: Travailler sur le type de data
activity.data = data as unknown as AnyData;
activity.data.draftUrl = draftUrl;
activity.phase = selectedPhase;
activity.content = data;
activity.userId = userId;
activity.villageId = villageId;
activity.responseActivityId = null;
activity.responseType = null;
activity.isPinned = false;
activity.displayAsUser = false;
activity.publishDate = new Date();

await AppDataSource.getRepository(Activity).save(activity);
}
}

// --- Get all games standardised by subType ---
Expand Down
17 changes: 16 additions & 1 deletion src/components/game/CreateGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ const CreateGame = ({ stepNumber }: PlayProps) => {
const { query } = router;
const { gameConfig, setStepsGame } = useContext(GameContext);
const { activity } = useContext(ActivityContext);
const activityStepGame = activity?.data.game as StepsTypes[];
console.log('DATA : ', activity?.data);

if (query?.activity_id) {
const data = activity?.data.game1;
let activityStepGame = activity?.data.game1 as StepsTypes[];
switch (stepNumber) {
case 3:
activityStepGame = activity?.data.game2 as StepsTypes[];
break;

case 4:
activityStepGame = activity?.data.game3 as StepsTypes[];
break;

default:
break;
}
setStepsGame(stepNumber, activityStepGame);
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/creer-un-jeu/mimique/1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const MimiqueStep1 = () => {
const labelPresentation = user ? getUserDisplayName(user, false) : '';
const { activity } = React.useContext(ActivityContext);
const { query } = router;
const activityStepGame = activity?.data.game as StepsTypes[];
const activityStepGame = activity?.data.game1 as StepsTypes[];
const activityId = query?.activity_id as string | null;

const onNext = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/creer-un-jeu/mimique/2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const MimiqueStep2 = () => {
const labelPresentation = user ? getUserDisplayName(user, false) : '';
const { activity } = React.useContext(ActivityContext);
const { query } = router;
const activityStepGame = activity?.data.game as StepsTypes[];
const activityStepGame = activity?.data.game2 as StepsTypes[];
const activityId = query?.activity_id as string | null;

const onNext = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/creer-un-jeu/mimique/3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const MimiqueStep3 = () => {
const labelPresentation = user ? getUserDisplayName(user, false) : '';
const { activity } = React.useContext(ActivityContext);
const { query } = router;
const activityStepGame = activity?.data.game as StepsTypes[];
const activityStepGame = activity?.data.game3 as StepsTypes[];
const activityId = query?.activity_id as string | null;

const onNext = () => {
Expand Down
12 changes: 8 additions & 4 deletions src/pages/creer-un-jeu/mimique/4.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const MimiqueStep4 = () => {
const [isLoading, setIsLoading] = React.useState(false);
const { activity } = React.useContext(ActivityContext);
const { query } = router;
const activityStepGame = activity?.data.game as StepsTypes[];
const activityStepGame1 = activity?.data.game1 as StepsTypes[];
const activityStepGame2 = activity?.data.game2 as StepsTypes[];
const activityStepGame3 = activity?.data.game3 as StepsTypes[];

const activityId = query?.activity_id as string | null;

const { gameConfig } = useContext(GameContext);
Expand All @@ -40,19 +43,20 @@ const MimiqueStep4 = () => {
type: ActivityType.GAME,
subType: GameType.MIMIC,
game1: {
game: query?.activity_id && activityStepGame ? activityStepGame : gameConfig[0],
game: query?.activity_id && activityStepGame1 ? activityStepGame1 : gameConfig[0],
labelPresentation: labelPresentation,
},
game2: {
game: query?.activity_id && activityStepGame ? activityStepGame : gameConfig[1],
game: query?.activity_id && activityStepGame2 ? activityStepGame2 : gameConfig[1],
labelPresentation: labelPresentation,
},
game3: {
game: query?.activity_id && activityStepGame ? activityStepGame : gameConfig[2],
game: query?.activity_id && activityStepGame3 ? activityStepGame3 : gameConfig[2],
labelPresentation: labelPresentation,
},
selectedPhase: selectedPhase,
activityId: query?.activity_id ? activityId : null,
draftUrl: window.location.pathname,
};

setIsLoading(true);
Expand Down

0 comments on commit 9780f58

Please sign in to comment.