From b67e4d3b91ec3c37f03504e3d1ba89dd116a0dab Mon Sep 17 00:00:00 2001 From: Neo Ryo Date: Mon, 22 Apr 2024 11:24:29 +0200 Subject: [PATCH] fixing countries WIP --- server/controllers/activity.ts | 8 +- server/controllers/user.ts | 2 +- server/manager/activity.ts | 4 +- server/middlewares/setVillage.ts | 4 +- src/api/countries/countries.get.ts | 18 +++++ src/components/Flag.tsx | 8 +- src/components/Navigation.tsx | 4 +- src/components/WelcomeModal/FirstPhase.tsx | 2 +- src/components/WorldMap/UserPopover.tsx | 2 +- .../WorldMap/world/world.constants.ts | 1 + src/components/accueil/Accueil.tsx | 18 ++++- src/components/accueil/Filters/Filters.tsx | 11 +-- src/components/accueil/RightNavigation.tsx | 4 +- .../activities/ActivityCard/index.tsx | 2 +- .../ActivityComments/CommentCard.tsx | 2 +- .../activities/ActivityView/index.tsx | 2 +- src/components/activities/GameStats.tsx | 5 +- src/contexts/countryContext.tsx | 29 +++++++ src/pages/_app.tsx | 79 ++++++++++--------- src/pages/admin/users/edit/[id].tsx | 9 ++- src/pages/admin/users/new.tsx | 8 +- src/pages/admin/villages/edit/[id].tsx | 10 ++- src/pages/creer-un-jeu/mimique/jouer.tsx | 11 +-- src/services/useActivities.ts | 2 +- types/country.type.ts | 1 + 25 files changed, 161 insertions(+), 85 deletions(-) create mode 100644 src/api/countries/countries.get.ts create mode 100644 src/contexts/countryContext.tsx diff --git a/server/controllers/activity.ts b/server/controllers/activity.ts index b2f10f504..1ae3b395b 100644 --- a/server/controllers/activity.ts +++ b/server/controllers/activity.ts @@ -25,17 +25,13 @@ const activityController = new Controller('/activities'); // --- Get all activities. --- activityController.get({ path: '', userType: UserType.OBSERVATOR }, async (req: Request, res: Response) => { if (!req.user) throw new AppError('Forbidden', ErrorCode.UNKNOWN); + console.log(req.query); const activities = await getActivities({ limit: req.query.limit ? Number(getQueryString(req.query.limit)) || 200 : undefined, page: req.query.page ? Number(getQueryString(req.query.page)) || 0 : undefined, villageId: req.query.villageId ? Number(getQueryString(req.query.villageId)) || 0 : undefined, - countries: - req.query.countries !== undefined - ? req.query.countries.length === 0 - ? [] - : (getQueryString(req.query.countries) || '').split(',') - : undefined, + countries: req.query.countries && req.query.countries.length ? (req.query.countries as string[]).map((e) => Number(e)) : [], pelico: req.query.pelico ? req.query.pelico !== 'false' : undefined, type: req.query.type ? (getQueryString(req.query.type) || '').split(',') : undefined, subType: req.query.subType ? Number(getQueryString(req.query.subType)) || 0 : undefined, diff --git a/server/controllers/user.ts b/server/controllers/user.ts index cba135e30..b5c2eb0e3 100644 --- a/server/controllers/user.ts +++ b/server/controllers/user.ts @@ -94,7 +94,7 @@ userController.get({ path: '', userType: UserType.OBSERVATOR }, async (req: Requ userController.get({ path: '/:id', userType: UserType.TEACHER }, async (req: Request, res: Response, next: NextFunction) => { const id = parseInt(req.params.id, 10) || 0; - const user = await AppDataSource.getRepository(User).findOne({ where: { id } }); + const user = await AppDataSource.getRepository(User).findOne({ where: { id }, relations: { country: true } }); const isSelfProfile = req.user && req.user.id === id; const isAdmin = req.user && req.user.type <= UserType.ADMIN; if (user === null || (!isSelfProfile && !isAdmin)) { diff --git a/server/manager/activity.ts b/server/manager/activity.ts index c54994433..f72cc3099 100644 --- a/server/manager/activity.ts +++ b/server/manager/activity.ts @@ -13,7 +13,7 @@ type ActivityGetter = { villageId?: number; type?: string[]; subType?: number | null; - countries?: string[]; + countries?: number[]; pelico?: boolean; userId?: number; status?: number; @@ -80,7 +80,7 @@ export const getActivities = async ({ } else if (pelico && countries !== undefined && countries.length > 0) { subQueryBuilder = subQueryBuilder .innerJoin('activity.user', 'user') - .andWhere('((user.countryCode IN (:countries) AND user.type >= :userType) OR user.type <= :userType2)', { + .andWhere('((user.countryId IN (:countries) AND user.type >= :userType) OR user.type <= :userType2)', { countries, userType: UserType.TEACHER, userType2: UserType.MEDIATOR, diff --git a/server/middlewares/setVillage.ts b/server/middlewares/setVillage.ts index 47762ade7..2295171ca 100644 --- a/server/middlewares/setVillage.ts +++ b/server/middlewares/setVillage.ts @@ -13,7 +13,9 @@ export async function setVillage(req: Request, res: Response, next: NextFunction villageId = req.cookies?.['village-id'] || -1; } if (villageId !== -1 || (user && user.type !== UserType.TEACHER)) { - const villages = await AppDataSource.getRepository(Village).find(villageId !== -1 ? { where: { id: villageId } } : { order: { id: 'ASC' } }); + const villages = await AppDataSource.getRepository(Village).find( + villageId !== -1 ? { where: { id: villageId }, relations: { countries: true } } : { order: { id: 'ASC' } }, + ); req.village = villages[0]; } if (villageId === -1 && req.village !== undefined) { diff --git a/src/api/countries/countries.get.ts b/src/api/countries/countries.get.ts new file mode 100644 index 000000000..9c123f92a --- /dev/null +++ b/src/api/countries/countries.get.ts @@ -0,0 +1,18 @@ +import { useQuery } from 'react-query'; +import type { Country } from 'server/entities/country'; + +import { axiosRequest } from 'src/utils/axiosRequest'; + +async function getCountries(): Promise { + return ( + await axiosRequest({ + method: 'GET', + baseURL: '/api', + url: '/countries', + }) + ).data; +} + +export const useGetCountries = () => { + return useQuery(['countries'], () => getCountries()); +}; diff --git a/src/components/Flag.tsx b/src/components/Flag.tsx index 8c23ca027..0c3e2dbbe 100644 --- a/src/components/Flag.tsx +++ b/src/components/Flag.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import type { Country } from 'server/entities/country'; import MysteryFlag from 'src/svg/mystery-flag.svg'; @@ -9,7 +10,7 @@ const sizes = { interface FlagProps { isMistery?: boolean; - country?: string; + country?: Country; size?: 'small' | 'medium'; style?: React.CSSProperties; } @@ -20,6 +21,9 @@ export const Flag = ({ country, isMistery = false, size = 'medium', style = {} } return ( // Small SVG, no need of improvments // eslint-disable-next-line @next/next/no-img-element - + ); }; diff --git a/src/components/Navigation.tsx b/src/components/Navigation.tsx index 05d841bc8..b42bb2ce8 100644 --- a/src/components/Navigation.tsx +++ b/src/components/Navigation.tsx @@ -258,7 +258,7 @@ export const Navigation = (): JSX.Element => { { {

{user ? user.country?.name : ''} - {user && } + {user && }