Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Ryo committed Apr 22, 2024
1 parent b67e4d3 commit 8412ff5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 150 deletions.
2 changes: 0 additions & 2 deletions server/controllers/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ 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,
Expand Down
2 changes: 1 addition & 1 deletion src/components/accueil/Accueil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const Accueil = () => {
const { activities } = useActivities({
limit: 200,
page: 0,
countries: filterCountries.map((c) => c.id),
countries: filterCountries.map((c) => c.isoCode),
pelico: filters.pelico,
type: filters.types === 'all' ? undefined : filters.types,
phase: selectedPhase,
Expand Down
21 changes: 13 additions & 8 deletions src/pages/admin/villages/new.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Link from 'next/link';
import { useRouter } from 'next/router';
import React from 'react';
import React, { useContext } from 'react';
import type { Country } from 'server/entities/country';

import NavigateNextIcon from '@mui/icons-material/NavigateNext';
import { Button, TextField } from '@mui/material';
Expand All @@ -9,23 +10,25 @@ import MaterialLink from '@mui/material/Link';

import { AdminTile } from 'src/components/admin/AdminTile';
import { CountrySelector } from 'src/components/selectors/CountrySelector';
import { CountryContext } from 'src/contexts/countryContext';
import { useVillageRequests } from 'src/services/useVillages';

const NewVillage = () => {
const router = useRouter();
const { countries } = useContext(CountryContext);
const { addVillage } = useVillageRequests();

const [village, setVillage] = React.useState<{ name: string; countries: string[] }>({
const [village, setVillage] = React.useState<{ name: string; countries: Country[] }>({
name: '',
countries: ['', ''],
countries: [],
});

const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
if (!village.name || !village.countries[0] || !village.countries[1]) {
return;
}
const result = await addVillage({ name: village.name, countries: village.countries.map((c) => ({ isoCode: c, name: c })) });
const result = await addVillage({ name: village.name, countries: village.countries });
if (result !== null) {
router.push('/admin/villages');
}
Expand Down Expand Up @@ -56,17 +59,19 @@ const NewVillage = () => {
style={{ marginBottom: '1rem' }}
/>
<CountrySelector
value={village.countries[0]}
value={village.countries[0].isoCode}
onChange={(newValue: string) => {
setVillage((v) => ({ ...v, countries: [newValue, village.countries[1]] }));
const foundCountry = countries.find((c) => c.isoCode === newValue);
if (foundCountry) setVillage((v) => ({ ...v, countries: [foundCountry, village.countries[1]] }));
}}
label="Pays 1"
style={{ width: '100%', marginBottom: '1rem' }}
/>
<CountrySelector
value={village.countries[1]}
value={village.countries[1].isoCode}
onChange={(newValue: string) => {
setVillage((v) => ({ ...v, countries: [village.countries[0], newValue] }));
const foundCountry = countries.find((c) => c.isoCode === newValue);
if (foundCountry) setVillage((v) => ({ ...v, countries: [village.countries[0], foundCountry] }));
}}
label="Pays 1"
style={{ width: '100%', marginBottom: '1rem' }}
Expand Down
143 changes: 5 additions & 138 deletions src/pages/familles/1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// import debounce from 'lodash.debounce';
import { useRouter } from 'next/router';
import React, { useContext, useEffect, useReducer } from 'react';
import type { Country } from 'server/entities/country';

import { Button, Card, CircularProgress, FormControl, FormControlLabel, Radio, RadioGroup } from '@mui/material';

Expand All @@ -28,24 +29,11 @@ import type { Activity } from 'types/activity.type';
import type { Classroom, InitialStateOptionsProps } from 'types/classroom.type';
import { UserType } from 'types/user.type';

// const content1 = {
// text1: 'les familles peuvent voir toutes les activités publiées sur 1Village, mais',
// text2: 'jours après leurs publication',
// };
// const content2 = {
// text1: 'les familles peuvent voir toutes les activités publiées sur 1Village, mais seulement celles publiées par notre classe et',
// text2: 'jours après leurs publication',
// };

type StateType = {
delayedDays: number;
hasVisibilitySetToClass: boolean;
};

//TODO: ouvrir un nouvel onglet pour les activités
//TODO: factoriser le code méthode SOLID
//TODO: traiter les erreurs de react forward avec les activityCard

function reducer(
state: InitialStateOptionsProps,
action: {
Expand Down Expand Up @@ -100,12 +88,6 @@ const ClassroomParamStep1 = () => {
ownClassTimeDelay: { delayedDays: classroom?.delayedDays || 1, hasVisibilitySetToClass: true },
});

// const [isDisabled, setIsDisabled] = React.useState({
// default: false,
// timeDelay: false,
// ownClass: false,
// ownClassTimeDelay: false,
// });
const [radioValue, setRadioValue] = React.useState('default');
const [modalStep, setModalStep] = React.useState(0);
const modalStepTimeout = React.useRef<number | undefined>(undefined);
Expand All @@ -132,24 +114,18 @@ const ClassroomParamStep1 = () => {
user !== null &&
(user.type === UserType.MEDIATOR || user.type === UserType.ADMIN || user.type === UserType.SUPER_ADMIN || user.type === UserType.FAMILY);

const filterCountries = React.useMemo(
() =>
!village || (selectedPhase === 1 && !isMediatorOrFamily)
? user && user.country !== null
? [user.country?.isoCode.toUpperCase()]
: []
: village.countries.map((c) => c.isoCode),
const filterCountries: Country[] = React.useMemo(
() => (!village || (selectedPhase === 1 && !isMediatorOrFamily) ? (user && user.country !== null ? [user.country] : []) : village.countries),
[selectedPhase, village, user, isMediatorOrFamily],
);

//TODO: may be filterCountries should be with country form student > teacher
const [filters, setFilters] = React.useState<FilterArgs>({
selectedType: 0,
selectedPhase: 0,
types: 'all',
status: 0,
countries: filterCountries.reduce<{ [key: string]: boolean }>((acc, c) => {
acc[c] = true;
acc[c.isoCode] = true;
return acc;
}, {}),
pelico: true,
Expand All @@ -172,26 +148,6 @@ const ClassroomParamStep1 = () => {
}, {}),
[users],
);
// const handleDaysDelay = (key: string, event: React.ChangeEvent<HTMLInputElement>) => {
// const value = Number(event.target.value);
// const days = value >= 1 ? value : 1;

// switch (key) {
// case 'timeDelay':
// dispatch({ type: 'timeDelay', data: days });
// break;
// case 'ownClassTimeDelay':
// dispatch({ type: 'ownClassTimeDelay', data: days });
// break;
// }
// };

// TODO handle delayed days with this debounce function
// const debouncedhandleRadioSelect = React.useRef(
// debounce((value: any) => {
// updateClassroomParameters(value);
// }, 1500),
// ).current;

const handleRadioSelect = (key: string): void => {
setRadioValue(key);
Expand Down Expand Up @@ -266,9 +222,6 @@ const ClassroomParamStep1 = () => {
refetch();
});
};
// const toggleInput = (key: string, bool: boolean) => {
// setIsDisabled({ ...isDisabled, [key]: bool });
// };

const onNext = () => {
router.push('/familles/2');
Expand Down Expand Up @@ -325,59 +278,14 @@ const ClassroomParamStep1 = () => {
// onFocus={() => handleSelectionVisibility('default')}
style={radioValue !== 'default' ? { color: '#CCC' } : {}}
/>
{/* <FormControlLabel
value="timeDelay"
name="timeDelay"
control={<Radio />}
label={
<TextnInputContainer
{...content1}
onClick={(event) => {
handleDaysDelay('timeDelay', event);
debouncedhandleRadioSelect('timeDelay');
}}
onChange={(event) => {
handleDaysDelay('timeDelay', event);
debouncedhandleRadioSelect('timeDelay');
}}
onBlur={() => handleSelectionVisibility('timeDelay')}
value={state.timeDelay.delayedDays}
disabled={radioValue !== 'timeDelay'}
/>
}
onFocus={(event: unknown) => {
handleDaysDelay('timeDelay', event as unknown as React.ChangeEvent<HTMLInputElement>);
debouncedhandleSelVisibility('timeDelay')
}}
onClick={() => toggleInput('timeDelay', false)}
disabled={isDisabled?.timeDelay}
style={radioValue !== 'timeDelay' ? { color: '#CCC' } : {}}
/> */}

<FormControlLabel
value="ownClass"
name="ownClass"
control={<Radio />}
label="les familles peuvent voir toutes les activités publiées sur 1Village, dès leur publication, mais seulement celles publiées par notre classe"
// onFocus={() => handleSelectionVisibility('ownClass')}
style={radioValue !== 'ownClass' ? { color: '#CCC' } : {}}
/>
{/* <FormControlLabel
value="ownClassTimeDelay"
name="ownClassTimeDelay"
control={<Radio />}
label={
<TextnInputContainer
{...content2}
onChange={(event) => handleDaysDelay('ownClassTimeDelay', event)}
onBlur={() => handleSelectionVisibility('ownClassTimeDelay')}
value={state.ownClassTimeDelay.delayedDays}
disabled={radioValue !== 'ownClassTimeDelay'}
/>
}
onClick={() => toggleInput('ownClassTimeDelay', false)}
disabled={isDisabled?.ownClassTimeDelay}
style={radioValue !== 'ownClassTimeDelay' ? { color: '#CCC' } : {}}
/> */}
</RadioGroup>
</FormControl>
<div style={{ margin: '-1rem 0' }}>
Expand Down Expand Up @@ -460,45 +368,4 @@ const ClassroomParamStep1 = () => {
);
};

// type TextInputContainerProps = {
// text1: string;
// text2?: string;
// onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
// onBlur: () => void;
// value: number;
// disabled?: boolean;
// };

/**
* Container to display text and input inline
* @param onChange function to handle changes
* @param value value of the input
* @param object text object containing text to display
*/
// const TextnInputContainer = ({ onChange, onBlur, value, disabled, ...props }: TextInputContainerProps) => {
// const { text1, text2 } = props;
// const spanStyle = { flexShrink: 0, marginRight: '0.5rem' };
// return (
// <div className="textnInputContainer__line" style={{ display: 'flex', alignItems: 'flex-start', flexWrap: 'wrap' }}>
// <span style={spanStyle}>{text1}</span>
// <TextField
// className="textnInputContainer__textfield"
// variant="standard"
// type="number"
// inputProps={{ min: 0 }}
// size="small"
// value={value}
// onChange={onChange}
// onBlur={onBlur}
// disabled={disabled}
// sx={{
// width: '2rem',
// marginRight: '5px',
// }}
// />
// <span style={spanStyle}>{text2}</span>
// </div>
// );
// };

export default ClassroomParamStep1;
2 changes: 1 addition & 1 deletion src/services/useActivities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { UserType } from 'types/user.type';
export type Args = {
limit?: number;
page?: number;
countries?: number[];
countries?: string[];
pelico?: boolean;
type?: number | number[];
userId?: number;
Expand Down

0 comments on commit 8412ff5

Please sign in to comment.