Skip to content

Commit

Permalink
refactor(context): modulariza seleção disciplina
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusvrs committed Nov 30, 2023
1 parent 335ba55 commit 4d930fe
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions web/app/contexts/SelectedClassesContext/SelectedClassesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,31 @@ import { errorToast } from '../../utils/errorToast';
import { ProviderJSXPropsType, SelectedClassesContextProviderPropsType, SelectedClassesContextType, SelectedClassesType } from './types';


let hasError = false;
const MAX_CLASSES = 4;
const MAX_DISCIPLINES = 11;

export const SelectedClassesContext = createContext({} as SelectedClassesContextType);

const handleError = (message: string) => {
errorToast(message);
hasError = true;
};

function handleMaxDisciplinesError(value: SelectedClassesType) {
if (value.size > MAX_DISCIPLINES) {
handleError(`Você só pode escolher até ${MAX_DISCIPLINES} disciplinas!`);
}
}

function handleMaxClassesError(value: SelectedClassesType) {
value.forEach(classes => {
if (classes.size > MAX_CLASSES) {
handleError(`Você só pode escolher até ${MAX_CLASSES} aulas da mesma disciplina!`);
}
});
}

function ProviderJSX({ children, ...props }: ProviderJSXPropsType) {
return (
<SelectedClassesContext.Provider value={props}>
Expand All @@ -31,22 +51,11 @@ export default function SelectedClassesContextProvider({ children }: SelectedCla
setClassesChange(!classesChange);
}

function handleMaxDisciplinesError(value: SelectedClassesType, handleError: (message: string) => void) {
if (value.size > MAX_DISCIPLINES) handleError(`Você só pode escolher até ${MAX_DISCIPLINES} disciplinas!`);
}

const setSelectedClasses = (value: SelectedClassesType) => {
let hasError = false;
const handleError = (message: string) => {
errorToast(message);
hasError = true;
};

value.forEach(classes => {
if (classes.size > MAX_CLASSES) handleError(`Você só pode escolher até ${MAX_CLASSES} aulas da mesma disciplina!`);
});
handleMaxDisciplinesError(value, handleError);
hasError = false;

handleMaxClassesError(value);
handleMaxDisciplinesError(value);
if (!hasError) handleNoError(value);
};

Expand Down

0 comments on commit 4d930fe

Please sign in to comment.