Skip to content

Commit

Permalink
feat : fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
sanaTr-w committed Oct 16, 2024
1 parent 7428666 commit 3fa1c9f
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 86 deletions.
95 changes: 53 additions & 42 deletions server/controllers/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,29 @@ 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} });
if(responses){
await AppDataSource.createQueryBuilder().update(Activity).set({ data: data, content: content }).where('id = :activityId', { activityId }).execute();
const updateGameDraftIfExists = async (data: any, content: ActivityContent, 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).find({ where: { id: activityId } });
if (responses) {
await AppDataSource.createQueryBuilder()
.update(Activity)
.set({ data: data, content: content })
.where('id = :activityId', { activityId })
.execute();
}
}

const updateStatusToPublished = async(userId: number , villageId: number , type: number, subType: number)=>{
await AppDataSource.createQueryBuilder().update(Activity).set({ status: ActivityStatus.PUBLISHED}).where({
userId: userId,
villageId: villageId,
type: type,
subType: subType
}).execute();
};

}
const updateStatusToPublished = async (userId: number, villageId: number, type: number, subType: number) => {
await AppDataSource.createQueryBuilder()
.update(Activity)
.set({ status: ActivityStatus.PUBLISHED })
.where({
userId: userId,
villageId: villageId,
type: type,
subType: subType,
})
.execute();
};

//--- Get all games ---
gameController.get({ path: '', userType: UserType.TEACHER }, async (req: Request, res: Response, next: NextFunction) => {
Expand Down Expand Up @@ -296,46 +302,51 @@ gameController.post({ path: '/standardGame', userType: UserType.TEACHER }, async
}

const data = req.body;
const { game1, game2, game3, userId, villageId, type, subType, selectedPhase, status, activityId} = data;

//save draft game each step
if(status && status === ActivityStatus.DRAFT){
if(game1){
if(activityId){
updateGameDraftIfExists(activityId, game1.data, game1.content)
return;
}
createGame(game1, userId, villageId, type, subType, selectedPhase, status);
res.sendStatus(200);
const { game1, game2, game3, userId, villageId, type, subType, selectedPhase, status, activityId } = data;

//save draft game each step
if (status && status === ActivityStatus.DRAFT) {
if (game1) {
if (activityId) {
updateGameDraftIfExists(activityId, game1.data, game1.content);
return;
}
else if(game2){
if(activityId){
updateGameDraftIfExists(activityId, game1.data, game1.content)
return;
}
createGame(game2, userId, villageId, type, subType, selectedPhase, status);
res.sendStatus(200);
createGame(game1, userId, villageId, type, subType, selectedPhase, status);
res.sendStatus(200);
return;
} else if (game2) {
if (activityId) {
updateGameDraftIfExists(activityId, game1.data, game1.content);
return;
}
else if(game3){
if(activityId){
updateGameDraftIfExists(activityId, game1.data, game1.content);
return;
}
createGame(game3, userId, villageId, type, subType, selectedPhase, status);
res.sendStatus(200);
createGame(game2, userId, villageId, type, subType, selectedPhase, status);
res.sendStatus(200);
return;
} else if (game3) {
if (activityId) {
updateGameDraftIfExists(activityId, game1.data, game1.content);
return;
}

createGame(game3, userId, villageId, type, subType, selectedPhase, status);
res.sendStatus(200);
return;
}
}
//save published games if status is published
await updateStatusToPublished(userId, villageId, type, subType);

res.sendStatus(200);
});

async function createGame(data: ActivityContent[], userId: number, villageId: number, type: number, subType: number, selectedPhase: number, status : number) {
async function createGame(
data: ActivityContent[],
userId: number,
villageId: number,
type: number,
subType: number,
selectedPhase: number,
status: number,
) {
const activity = new Activity();
activity.type = type;
activity.subType = subType;
Expand Down
39 changes: 19 additions & 20 deletions src/contexts/gameContext.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Card } from '@mui/material';

Check failure on line 1 in src/contexts/gameContext.tsx

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups

Check failure on line 1 in src/contexts/gameContext.tsx

View workflow job for this annotation

GitHub Actions / lint

`@mui/material` import should occur after import of `react`
import type { ReactNode } from 'react';
import React, { createContext, useContext, useState } from 'react';

import { postGameDataMonneyOrExpression } from 'src/api/game/game.post';
import { GAME_FIELDS_CONFIG } from 'src/config/games/game';
import type { inputType, StepsTypes , GameDataMonneyOrExpression } from 'types/game.type';
import { GameType } from 'types/game.type';
import { Card } from '@mui/material';
import { primaryColor } from 'src/styles/variables.const';
import { postGameDataMonneyOrExpression } from 'src/api/game/game.post';

import type { inputType, StepsTypes, GameDataMonneyOrExpression } from 'types/game.type';
import { GameType } from 'types/game.type';

type GameContextType = {
gameConfig: Array<StepsTypes[]>;
Expand All @@ -17,8 +16,7 @@ type GameContextType = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
updateGameConfig: (value: any, input: inputType) => void;
inputSelectedValue?: string;
saveDraftGrame: (data: GameDataMonneyOrExpression)=> void;

saveDraftGrame: (data: GameDataMonneyOrExpression) => void;
};

export const GameContext = createContext<GameContextType>({
Expand All @@ -30,7 +28,7 @@ export const GameContext = createContext<GameContextType>({
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
updateGameConfig: (_value: any, _input: inputType) => {},
inputSelectedValue: '',
saveDraftGrame: (_data: GameDataMonneyOrExpression)=>{},
saveDraftGrame: (_data: GameDataMonneyOrExpression) => {},

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

View workflow job for this annotation

GitHub Actions / lint

'_data' is defined but never used
});

export const useGame = () => {
Expand All @@ -55,7 +53,7 @@ export const GameProvider = ({ children }: GameProviderProps) => {
const [gameConfig, setGameConfig] = useState<Array<StepsTypes[]>>(GAME_FIELDS_CONFIG[gameType].steps);
const [isGameDraftSaved, setIsGameDraftSaved] = useState(false);
const inputSelectedValue = gameConfig[0]?.[0]?.inputs?.[0]?.selectedValue;

const updateGameType = (type: GameType) => {
setGameType(type);
setGameConfig(GAME_FIELDS_CONFIG[type].steps);
Expand All @@ -79,23 +77,24 @@ export const GameProvider = ({ children }: GameProviderProps) => {
setGameConfig(configCopy);
saveGameResponseInSessionStorage(configCopy);
};
const saveDraftGrame = async (data: GameDataMonneyOrExpression ) =>{
await postGameDataMonneyOrExpression(data);
setIsGameDraftSaved(true)
// Hide the popup after a timeout
setTimeout(() => {
setIsGameDraftSaved(false); // Hide the popup
}, 2000); // Adjust the duration as needed

const saveDraftGrame = async (data: GameDataMonneyOrExpression) => {
await postGameDataMonneyOrExpression(data);
setIsGameDraftSaved(true);
// Hide the popup after a timeout
setTimeout(() => {
setIsGameDraftSaved(false); // Hide the popup
}, 2000); // Adjust the duration as needed
};

return (
<GameContext.Provider value={{ saveDraftGrame, updateGameConfig, gameType, setGameType: updateGameType, gameConfig, setGameConfig, inputSelectedValue }}>
<GameContext.Provider
value={{ saveDraftGrame, updateGameConfig, gameType, setGameType: updateGameType, gameConfig, setGameConfig, inputSelectedValue }}
>
{children}
{isGameDraftSaved && (
<div style={{ position: 'fixed', bottom: '1rem', right: '4.5rem' }}>
<Card style={{ backgroundColor: primaryColor, color: 'white', padding: '0.25rem 0.5rem', display: 'flex', alignItems: 'center' }}>
<p className="text text--small">Brouillon enregistré</p>
<p className="text text--small">Brouillon enregistré</p>
</Card>
</div>
)}
Expand Down
6 changes: 3 additions & 3 deletions src/pages/creer-un-jeu/expression/2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { StepsButton } from 'src/components/StepsButtons';
import CreateGame from 'src/components/game/CreateGame';
import { GameContext } from 'src/contexts/gameContext';
import { UserContext } from 'src/contexts/userContext';
import { ActivityType , ActivityStatus} from 'types/activity.type';
import { VillageContext } from 'src/contexts/villageContext';
import { getUserDisplayName } from 'src/utils';
import { ActivityType, ActivityStatus } from 'types/activity.type';
import type { GameDataMonneyOrExpression } from 'types/game.type';
import { GameType } from 'types/game.type';
import { getUserDisplayName } from 'src/utils';

const ExpressionStep2 = () => {
const router = useRouter();
Expand All @@ -26,7 +26,7 @@ const ExpressionStep2 = () => {
const data: GameDataMonneyOrExpression = {
userId: user?.id || 0,
villageId: village?.id || 0,
type: ActivityType.GAME,
type: ActivityType.GAME,
subType: GameType.EXPRESSION,
game1: {
game: gameConfig[1],
Expand Down
9 changes: 5 additions & 4 deletions src/pages/creer-un-jeu/expression/3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { StepsButton } from 'src/components/StepsButtons';
import CreateGame from 'src/components/game/CreateGame';
import { GameContext } from 'src/contexts/gameContext';
import { UserContext } from 'src/contexts/userContext';
import { ActivityType , ActivityStatus} from 'types/activity.type';
import { VillageContext } from 'src/contexts/villageContext';
import { getUserDisplayName } from 'src/utils';
import { ActivityType, ActivityStatus } from 'types/activity.type';
import type { GameDataMonneyOrExpression } from 'types/game.type';
import { GameType } from 'types/game.type';
import { getUserDisplayName } from 'src/utils';

const ExpressionStep3 = () => {
const router = useRouter();
const { inputSelectedValue , saveDraftGrame} = useContext(GameContext);
const { inputSelectedValue, saveDraftGrame } = useContext(GameContext);
const { user } = React.useContext(UserContext);
const { village } = React.useContext(VillageContext);
const { gameConfig } = useContext(GameContext);
Expand All @@ -25,7 +26,7 @@ const ExpressionStep3 = () => {
const data: GameDataMonneyOrExpression = {
userId: user?.id || 0,
villageId: village?.id || 0,
type: ActivityType.GAME,
type: ActivityType.GAME,
subType: GameType.EXPRESSION,
game2: {
game: gameConfig[2],
Expand Down
9 changes: 5 additions & 4 deletions src/pages/creer-un-jeu/expression/4.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { StepsButton } from 'src/components/StepsButtons';
import CreateGame from 'src/components/game/CreateGame';
import { GameContext } from 'src/contexts/gameContext';
import { UserContext } from 'src/contexts/userContext';
import { ActivityType , ActivityStatus} from 'types/activity.type';
import { VillageContext } from 'src/contexts/villageContext';
import { getUserDisplayName } from 'src/utils';
import { ActivityType, ActivityStatus } from 'types/activity.type';
import type { GameDataMonneyOrExpression } from 'types/game.type';
import { GameType } from 'types/game.type';
import { getUserDisplayName } from 'src/utils';

const ExpressionStep4 = () => {
const router = useRouter();
const { inputSelectedValue , saveDraftGrame} = useContext(GameContext);
const { inputSelectedValue, saveDraftGrame } = useContext(GameContext);
const { user } = React.useContext(UserContext);
const { village } = React.useContext(VillageContext);
const { gameConfig } = useContext(GameContext);
Expand All @@ -24,7 +25,7 @@ const ExpressionStep4 = () => {
const data: GameDataMonneyOrExpression = {
userId: user?.id || 0,
villageId: village?.id || 0,
type: ActivityType.GAME,
type: ActivityType.GAME,
subType: GameType.EXPRESSION,
game3: {
game: gameConfig[3],
Expand Down
6 changes: 3 additions & 3 deletions src/pages/creer-un-jeu/mimique/1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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 } from 'types/game.type';
import { GameType } from 'types/game.type';

const MimiqueStep1 = () => {
const router = useRouter();
const { selectedPhase } = React.useContext(VillageContext);
const { user } = React.useContext(UserContext);
const { village } = React.useContext(VillageContext);
const { gameConfig , saveDraftGrame} = React.useContext(GameContext);
const { gameConfig, saveDraftGrame } = React.useContext(GameContext);
const labelPresentation = user ? getUserDisplayName(user, false) : '';

const onNext = () => {
Expand All @@ -34,7 +34,7 @@ const MimiqueStep1 = () => {
labelPresentation: labelPresentation,
},
selectedPhase: selectedPhase,
status: ActivityStatus.DRAFT
status: ActivityStatus.DRAFT,
};
saveDraftGrame(data);
router.push('/creer-un-jeu/mimique/2');
Expand Down
5 changes: 3 additions & 2 deletions src/pages/creer-un-jeu/mimique/2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import { getUserDisplayName } from 'src/utils';
import { ActivityStatus, ActivityType } from 'types/activity.type';
import type { GameDataMonneyOrExpression } from 'types/game.type';
import { GameType } from 'types/game.type';

const MimiqueStep2 = () => {
const router = useRouter();
const { selectedPhase } = React.useContext(VillageContext);
const { user } = React.useContext(UserContext);
const { village } = React.useContext(VillageContext);
const { gameConfig , saveDraftGrame} = React.useContext(GameContext);
const { gameConfig, saveDraftGrame } = React.useContext(GameContext);
const labelPresentation = user ? getUserDisplayName(user, false) : '';
const onNext = () => {
const data: GameDataMonneyOrExpression = {
Expand All @@ -31,7 +32,7 @@ const MimiqueStep2 = () => {
labelPresentation: labelPresentation,
},
selectedPhase: selectedPhase,
status: ActivityStatus.DRAFT
status: ActivityStatus.DRAFT,
};
saveDraftGrame(data);
router.push('/creer-un-jeu/mimique/3');
Expand Down
5 changes: 3 additions & 2 deletions src/pages/creer-un-jeu/mimique/3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import { getUserDisplayName } from 'src/utils';
import { ActivityStatus, ActivityType } from 'types/activity.type';
import type { GameDataMonneyOrExpression } from 'types/game.type';
import { GameType } from 'types/game.type';

const MimiqueStep3 = () => {
const router = useRouter();
const { selectedPhase } = React.useContext(VillageContext);
const { user } = React.useContext(UserContext);
const { village } = React.useContext(VillageContext);
const { gameConfig , saveDraftGrame} = React.useContext(GameContext);
const { gameConfig, saveDraftGrame } = React.useContext(GameContext);
const labelPresentation = user ? getUserDisplayName(user, false) : '';
const onNext = () => {
const data: GameDataMonneyOrExpression = {
Expand All @@ -31,7 +32,7 @@ const MimiqueStep3 = () => {
labelPresentation: labelPresentation,
},
selectedPhase: selectedPhase,
status: ActivityStatus.DRAFT
status: ActivityStatus.DRAFT,
};
saveDraftGrame(data);
router.push('/creer-un-jeu/mimique/4');
Expand Down
4 changes: 2 additions & 2 deletions src/pages/creer-un-jeu/objet/2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getUserDisplayName } from 'src/utils';
import { ActivityStatus, ActivityType } from 'types/activity.type';
import type { GameDataMonneyOrExpression } from 'types/game.type';
import { GameType } from 'types/game.type';

const MonnaieStep2 = () => {
const router = useRouter();
const { inputSelectedValue, saveDraftGrame } = useContext(GameContext);
Expand All @@ -33,8 +34,7 @@ const MonnaieStep2 = () => {
radio: gameConfig?.[0]?.[1]?.inputs?.[0]?.selectedValue,
},
selectedPhase: selectedPhase,
status: ActivityStatus.DRAFT

status: ActivityStatus.DRAFT,
};
saveDraftGrame(data);
router.push('/creer-un-jeu/objet/3');
Expand Down
4 changes: 2 additions & 2 deletions src/pages/creer-un-jeu/objet/3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getUserDisplayName } from 'src/utils';
import { ActivityStatus, ActivityType } from 'types/activity.type';
import type { GameDataMonneyOrExpression } from 'types/game.type';
import { GameType } from 'types/game.type';

const MonnaieStep3 = () => {
const router = useRouter();
const { inputSelectedValue, saveDraftGrame } = useContext(GameContext);
Expand All @@ -34,8 +35,7 @@ const MonnaieStep3 = () => {
radio: gameConfig?.[0]?.[1]?.inputs?.[0]?.selectedValue,
},
selectedPhase: selectedPhase,
status: ActivityStatus.DRAFT

status: ActivityStatus.DRAFT,
};
saveDraftGrame(data);
router.push('/creer-un-jeu/objet/4');
Expand Down
Loading

0 comments on commit 3fa1c9f

Please sign in to comment.