Skip to content

Commit

Permalink
filtre admin ok
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-pages committed Jun 6, 2024
1 parent c930ded commit 7db4ff6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 47 deletions.
33 changes: 33 additions & 0 deletions server/controllers/mediatheque.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Request, Response } from 'express';
import { map } from 'leaflet';

Check warning on line 2 in server/controllers/mediatheque.ts

View workflow job for this annotation

GitHub Actions / lint

'map' is defined but never used
import { Brackets } from 'typeorm';

import type { Filter } from '../../types/mediatheque.type';
import { Activity } from '../entities/activity';
import { User } from '../entities/user';
import { AppDataSource } from '../utils/data-source';
import { Controller } from './controller';

Expand Down Expand Up @@ -41,4 +43,35 @@ mediathequeController.post({ path: '' }, async (req: Request, res: Response) =>
}
});

mediathequeController.post({ path: '/pelico' }, async (req: Request, res: Response) => {
try {
const filters: Array<Filter[]> = req.body.filters || [];

if (!Array.isArray(filters)) {
return res.status(400).send({ error: 'Invalid filters format' });
}

const subQueryBuilderUser = AppDataSource.getRepository(User)
.createQueryBuilder('user')
.where('user.type = :superAdmin', { superAdmin: 0 })
.orWhere('user.type = :admin', { admin: 1 })
.orWhere('user.type = :mediator', { mediator: 2 });

const users = await subQueryBuilderUser.getMany();

const usersId: number[] = users.map((user) => user.id);

const subQueryBuilderActivity = AppDataSource.getRepository(Activity)
.createQueryBuilder('activity')
.where('activity.userId IN (:...usersId)', { usersId });

const activities = await subQueryBuilderActivity.getMany();

res.send(activities);
} catch (error) {
console.error('Error fetching media data:', error);
res.status(500).send({ error: 'Internal Server Error' });
}
});

export { mediathequeController };
23 changes: 23 additions & 0 deletions src/api/mediatheque/mediatheque.get-only-pelico.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useQuery } from 'react-query';

import { axiosRequest } from 'src/utils/axiosRequest';
import type { Filter } from 'types/mediatheque.type';

async function getOnlyPelicoMediatheque(params: { filters: Array<Filter[]> }) {
const { filters } = params;

return (
await axiosRequest({
method: 'POST',
baseURL: '/api',
url: '/mediatheque/pelico',
data: {
filters: filters,
},
})
).data;
}

export const useGetOnlyPelicoMediatheque = (filters: Array<Filter[]>) => {
return useQuery(['OnlyPelicoMediatheque', filters], () => getOnlyPelicoMediatheque({ filters }));
};
10 changes: 3 additions & 7 deletions src/components/admin/mediatheque/CheckboxAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@ import PelicoNeutre from 'src/svg/pelico/pelico_neutre.svg';

const CheckboxAdmin = () => {
const label = { inputProps: { 'aria-label': 'Pelico' } };
const { setFilters } = useContext(MediathequeContext);
const { setFilters, setUseAdminData } = useContext(MediathequeContext);

const handleCheckboxChange = (event: React.ChangeEvent<HTMLInputElement>) => {
console.log(event.target.checked);

Check failure on line 13 in src/components/admin/mediatheque/CheckboxAdmin.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
if (event.target.checked === false) {
setFilters([[]]);
console.log("c'est faux");

Check failure on line 16 in src/components/admin/mediatheque/CheckboxAdmin.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
setUseAdminData(false);
}
if (event.target.checked === true) {
setFilters([
[
{ table: 'activity', column: 'type', values: [6] },
{ table: 'activity', column: 'subType', values: [2] },
],
]);
console.log("c'est vrai");

Check failure on line 20 in src/components/admin/mediatheque/CheckboxAdmin.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
setUseAdminData(true);
}
};

Expand Down
105 changes: 65 additions & 40 deletions src/contexts/mediathequeContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { createContext, useState, useMemo } from 'react';
import React, { createContext, useState, useMemo, useEffect } from 'react';

import { useGetMediatheque } from 'src/api/mediatheque/mediatheque.get';
import { useGetOnlyPelicoMediatheque } from 'src/api/mediatheque/mediatheque.get-only-pelico';
import type { Filter } from 'types/mediatheque.type';

type MediathequeProviderProps = {
Expand All @@ -11,67 +12,91 @@ type MediathequeContextType = {
filters: Array<Filter[]>;
setFilters: React.Dispatch<React.SetStateAction<Array<Filter[]>>>;
allFiltered: [];
useAdminData: boolean;
setUseAdminData: React.Dispatch<React.SetStateAction<boolean>>;
};

const MediathequeContext = createContext<MediathequeContextType>({
filters: [],
setFilters: () => {},
allFiltered: [],
useAdminData: false,
setUseAdminData: () => {},
});

export const MediathequeProvider: React.FC<MediathequeProviderProps> = ({ children }) => {
const [filters, setFilters] = useState<Array<Filter[]>>([[]]);
console.log("filters :", filters);
console.log('filters :', filters);

Check failure on line 29 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
const [useAdminData, setUseAdminData] = useState(false);

const { data: allData } = useGetMediatheque(filters);
console.log(allData);
const { data: usersData } = useGetMediatheque(filters);
const { data: pelicoData } = useGetOnlyPelicoMediatheque(filters);
console.log('usersData', usersData);

Check failure on line 34 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
console.log('pelicoData', pelicoData);

Check failure on line 35 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
const [dataToUse, setDataToUse] = useState([]);
const [dataToDisplay, setDataToDisplay] = useState([]);

Check failure on line 37 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / typescript

'dataToDisplay' is declared but its value is never read.

Check failure on line 37 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / typescript

'setDataToDisplay' is declared but its value is never read.

Check warning on line 37 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / lint

'dataToDisplay' is assigned a value but never used

Check warning on line 37 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / lint

'setDataToDisplay' is assigned a value but never used

const activitiesMediaFinder = allData?.map(
({
id,
content,
subType,
type,
villageId,
userId,
}: {
id: number;
content: object;
subType: number;
type: number;
villageId: number;
userId: number;
}) => {
const result = { id, subType, type, villageId, userId, content: [] };
if (content.game) {
content.game.map(({ inputs }) =>
inputs.map((input: { type: number; selectedValue: string }) => {
if (input.type === 3 || input.type === 4) {
result.content.push({ type: input.type === 3 ? 'image' : 'video', value: input.selectedValue });
}
}),
);
} else {
content.map(({ type, value }) => {
const wantedTypes = ['image', 'video', 'sound'];
if (wantedTypes.includes(type)) {
result.content.push({ type, value });
useEffect(() => {
if (useAdminData === true) {
setDataToUse(pelicoData);
} else {
setDataToUse(usersData);
}
}, [pelicoData, useAdminData, usersData]);

console.log('dataToUse = ', dataToUse);

Check failure on line 47 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

const activitiesMediaFinder = Array.isArray(dataToUse)
? dataToUse.map(
({
id,
content,
subType,
type,
villageId,
userId,
}: {
id: number;
content: object;
subType: number;
type: number;
villageId: number;
userId: number;
}) => {
const result = { id, subType, type, villageId, userId, content: [] };
if (content.game) {

Check failure on line 67 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / typescript

Property 'game' does not exist on type 'object'.
content.game.map(({ inputs }) =>
inputs.map((input: { type: number; selectedValue: string }) => {
if (input.type === 3 || input.type === 4) {
result.content.push({ type: input.type === 3 ? 'image' : 'video', value: input.selectedValue });
}
}),
);
} else {
content.map(({ type, value }) => {
const wantedTypes = ['image', 'video', 'sound'];
if (wantedTypes.includes(type)) {
result.content.push({ type, value });
}
});
}
});
}
return result;
},
);
return result;
},
)
: [];

const dataFiltered = activitiesMediaFinder;
console.log("Les datas filtrées ======== ", dataFiltered)

Check failure on line 89 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 89 in src/contexts/mediathequeContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `"Les·datas·filtrées·========·",·dataFiltered)` with `'Les·datas·filtrées·========·',·dataFiltered);`

const value = useMemo(
() => ({
filters,
setFilters,
allFiltered: dataFiltered || [],
useAdminData,
setUseAdminData,
}),
[filters, dataFiltered],
[filters, dataFiltered, useAdminData],
);

return <MediathequeContext.Provider value={value}>{children}</MediathequeContext.Provider>;
Expand Down

0 comments on commit 7db4ff6

Please sign in to comment.