Skip to content

Commit

Permalink
ux(option-form): fix code climate issues
Browse files Browse the repository at this point in the history
  • Loading branch information
caio-felipee committed Jan 15, 2024
1 parent 779ffd3 commit a673034
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,29 @@ function Form(props: FormPropsType) {
function getYearAndPeriod(text: string) {
const year = text.split('/')[0] || defaultFormData.year;
const period = text.split('/')[1] || defaultFormData.period;

return { year, period };
}

function handleChangeYearAndPeriod(text: string, currentYearPeriod: string, selectedClasses: any, handleSetYearPeriod: () => void) {
if (currentYearPeriod && selectedClasses && currentYearPeriod != text) {
errorToast('Há disciplinas selecionadas de outro período, não pode haver mistura!');
} else handleSetYearPeriod();
}

export default function DisciplineOptionForm(props: DisciplineOptionFormPropsType) {
const { selectedClasses, currentYearPeriod, setCurrentYearPeriod } = useSelectedClasses();
const [disableDefault, setDisableDefault] = useState(false);
const [formData, setFormData] = useState(defaultFormData);

useEffect(() => {
if (disableDefault || formData != defaultFormData)
return;

if (selectedClasses.size) {
if (!disableDefault && formData == defaultFormData && selectedClasses.size) {
const { year, period } = getYearAndPeriod(currentYearPeriod);
setFormData({ ...formData, year: year, period: period });
setDisableDefault(true);
}
}, [disableDefault, currentYearPeriod, formData, selectedClasses]);

function handleChangeYearAndPeriod(text: string, handleSetYearPeriod: () => void) {
if (currentYearPeriod && currentYearPeriod != text && selectedClasses.size) {
errorToast('Há disciplinas selecionadas de outro período, não pode haver mistura!');
} else handleSetYearPeriod();
}

function handleYearAndPeriodChange(event: ChangeEvent<HTMLSelectElement>) {
const text = event.target.value.trim();
Expand All @@ -83,7 +81,7 @@ export default function DisciplineOptionForm(props: DisciplineOptionFormPropsTyp
setCurrentYearPeriod(text);
setDisableDefault(true);
};
handleChangeYearAndPeriod(text, handleSetYearPeriod);
handleChangeYearAndPeriod(text, currentYearPeriod, selectedClasses.size, handleSetYearPeriod);
}
}

Expand Down

0 comments on commit a673034

Please sign in to comment.