Skip to content

Commit

Permalink
Undo Fix eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
corentin703 committed Nov 6, 2021
1 parent 166de89 commit b0eee11
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 85 deletions.
43 changes: 20 additions & 23 deletions app/components/hub/average/AverageModule.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, useContext, useEffect, useState } from 'react'
import React, { createContext, useEffect, useRef, useState } from 'react'
import { InputLabel, MenuItem, Select } from '@mui/material'

import AverageData from 'constants/modules/average/AverageData'
Expand Down Expand Up @@ -48,17 +48,15 @@ export const AverageModuleContext = createContext<AverageModuleType>({
function AverageModule() {
const savedData = importData()

const [averageFormState, setAverageFormState] = useState<AverageModuleType>({
currentYear: savedData.currentYear,
currentSector: savedData.currentSector,
})
const currentYearIndexRef = useRef<number>(savedData.currentYear)
const currentSectorIndexRef = useRef<number | undefined>(savedData.currentSector)

const [averageDataState, setAverageDataState] = useState<YearData[]>(savedData.averageData)

const [mounted, setMounted] = useState(false)

useEffect(() => {
saveData(averageDataState, averageFormState.currentYear, averageFormState.currentSector)
saveData(averageDataState, currentYearIndexRef.current, currentSectorIndexRef.current)
}, [averageDataState])

useEffect(() => {
Expand All @@ -69,31 +67,25 @@ function AverageModule() {
let sector =
averageDataState[event.target.value].semesters[0].sectors.length > 1 ? 1 : undefined

if (averageFormState.currentSector !== undefined && sector !== undefined) {
sector = averageFormState.currentSector
if (sector === undefined) {
currentSectorIndexRef.current = undefined
}

setAverageFormState({
currentSector: sector,
currentYear: event.target.value,
})
currentYearIndexRef.current = event.target.value

setMounted(false)
}

const handleSectorChange = (event) => {
setAverageFormState({
...averageFormState,
currentSector: event.target.value,
})
currentSectorIndexRef.current = event.target.value

setMounted(false)
}

const yearInput = (
<FormControl className={'w-1/4 pr-2'}>
<InputLabel shrink>Année</InputLabel>
<Select value={averageFormState.currentYear} onChange={handleYearChange}>
<Select value={currentYearIndexRef.current} onChange={handleYearChange}>
{averageDataState.map((year, index) => {
return (
<MenuItem key={'avg-year-' + index} value={index}>
Expand All @@ -105,10 +97,10 @@ function AverageModule() {
</FormControl>
)

const currentYear = averageDataState[averageFormState.currentYear]
const currentYear = averageDataState[currentYearIndexRef.current]

const sectorInput = (() => {
const sectors = averageDataState[averageFormState.currentYear].semesters[0].sectors
const sectors = averageDataState[currentYearIndexRef.current].semesters[0].sectors

if (sectors.length === 1) {
return <></>
Expand All @@ -118,11 +110,11 @@ function AverageModule() {
<FormControl className={'w-1/4 pl-2'}>
<InputLabel shrink>Filière</InputLabel>
<Select
value={averageFormState.currentSector}
value={currentSectorIndexRef.current}
onChange={handleSectorChange}
disabled={!(sectors.length > 1)}
>
{averageDataState[averageFormState.currentYear].semesters[0].sectors
{averageDataState[currentYearIndexRef.current].semesters[0].sectors
.slice(1)
.map((sector, index) => {
return (
Expand All @@ -141,7 +133,12 @@ function AverageModule() {
}

return (
<AverageModuleContext.Provider value={averageFormState}>
<AverageModuleContext.Provider
value={{
currentYear: currentYearIndexRef.current,
currentSector: currentSectorIndexRef.current,
}}
>
<div>
{yearInput}
{sectorInput}
Expand All @@ -151,7 +148,7 @@ function AverageModule() {
setYearData={(yearData) => {
const newAverageState = [...averageDataState]

newAverageState[averageFormState.currentYear] = yearData
newAverageState[currentYearIndexRef.current] = yearData

setAverageDataState(newAverageState)
}}
Expand Down
17 changes: 8 additions & 9 deletions app/components/hub/average/SectorTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import Table from '@mui/material/Table'
import TableBody from '@mui/material/TableBody'
import TableCell from '@mui/material/TableCell'
Expand All @@ -18,7 +18,8 @@ interface SectorTablePropsType {

export default function SectorTable({ sectorData, setSectorData }: SectorTablePropsType) {
const [sectorState, setSectorState] = useState(sectorData)
const [averageState, setAverageState] = useState(sectorState.average)

const averageRef = useRef(sectorState.average)

const computeSectorAverage = (ues: UEData[]): number | undefined => {
let sectorAverage = 0
Expand All @@ -45,15 +46,13 @@ export default function SectorTable({ sectorData, setSectorData }: SectorTablePr
}

useEffect(() => {
averageRef.current = computeSectorAverage(sectorState.ues)

setSectorData({
...sectorState,
average: averageState,
average: averageRef.current,
})
}, [averageState])

useEffect(() => {
setAverageState(computeSectorAverage(sectorState.ues))
}, [sectorState])
}, [sectorState, setSectorData])

return (
<>
Expand Down Expand Up @@ -93,7 +92,7 @@ export default function SectorTable({ sectorData, setSectorData }: SectorTablePr
Moyenne
</TableCell>
<TableCell component="th" scope="row" align={'center'}>
{averageState ?? 'N/A'}
{averageRef.current ?? 'N/A'}
</TableCell>
</TableRow>
</TableBody>
Expand Down
30 changes: 16 additions & 14 deletions app/components/hub/average/Semester.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useState } from 'react'
import React, { useContext, useEffect, useRef, useState } from 'react'
import Typography from '@mui/material/Typography'
import SectorTable from './SectorTable'
import { SectorData, SemesterData } from 'constants/modules/average/types'
Expand All @@ -14,20 +14,13 @@ const Semester = ({ semesterIndex, semesterData, setSemesterData }: SemesterProp
const { currentSector } = useContext(AverageModuleContext)

const [semesterState, setSemesterState] = useState<SemesterData>(semesterData)
const [averageState, setAverageState] = useState(semesterData.average)

useEffect(() => {
setSemesterData({
...semesterState,
average: averageState,
})
}, [averageState])

useEffect(() => {
setAverageState(computeSemesterAverage(semesterState.sectors))
}, [semesterState])
const averageRef = useRef(semesterData.average)

const computeSemesterAverage = (sectors: SectorData[]): number | undefined => {
const computeSemesterAverage = (
sectors: SectorData[],
currentSector?: number
): number | undefined => {
let semesterAverage = 0
let count = 0

Expand Down Expand Up @@ -55,6 +48,15 @@ const Semester = ({ semesterIndex, semesterData, setSemesterData }: SemesterProp
return Math.round(semesterAverage * 1000) / 1000
}

useEffect(() => {
averageRef.current = computeSemesterAverage(semesterState.sectors, currentSector)

setSemesterData({
...semesterState,
average: averageRef.current,
})
}, [currentSector, semesterState, setSemesterData])

return (
<>
<br />
Expand Down Expand Up @@ -100,7 +102,7 @@ const Semester = ({ semesterIndex, semesterData, setSemesterData }: SemesterProp

<br />
<Typography variant="h6" color="textPrimary">
Moyenne du semestre {semesterIndex + 1} : {averageState ?? 'N/A'}
Moyenne du semestre {semesterIndex + 1} : {averageRef.current ?? 'N/A'}
</Typography>
</>
)
Expand Down
7 changes: 2 additions & 5 deletions app/components/hub/average/Subject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ function Subject({ subjectData, setSubjectData }: SubjectPropsType) {
subjectState.mark ? subjectState.mark.toString() : ''
)

useEffect(() => {
setSubjectData(subjectState)
}, [subjectState])

useEffect(() => {
const newSubjectState = {
...subjectState,
Expand All @@ -39,7 +35,8 @@ function Subject({ subjectData, setSubjectData }: SubjectPropsType) {
}

setSubjectState(newSubjectState)
}, [markState])
setSubjectData(newSubjectState)
}, [markState, setSubjectData, subjectState])

return (
<TableRow>
Expand Down
28 changes: 13 additions & 15 deletions app/components/hub/average/UeTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SubjectData, UEData } from '../../../../constants/modules/average/types'
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import TableRow from '@mui/material/TableRow'
import TableCell from '@mui/material/TableCell'
import IconButton from '@mui/material/IconButton'
Expand All @@ -19,21 +19,10 @@ interface UeTablePropsType {

const UeTable = ({ ueData, setUeData }: UeTablePropsType) => {
const [ueState, setUeState] = useState(ueData)
const [averageState, setAverageState] = useState(ueState.average)
const averageRef = useRef(ueData.average)

const [open, setOpen] = React.useState(false)

useEffect(() => {
setUeData({
...ueState,
average: averageState,
})
}, [averageState])

useEffect(() => {
setAverageState(computeAverage(ueState.subjects))
}, [ueState])

const computeAverage = (subjects: SubjectData[]): number | undefined => {
let average = 0
let coefSum = 0
Expand All @@ -58,6 +47,15 @@ const UeTable = ({ ueData, setUeData }: UeTablePropsType) => {
return Math.round(average * 1000) / 1000
}

useEffect(() => {
averageRef.current = computeAverage(ueState.subjects)

setUeData({
...ueState,
average: averageRef.current,
})
}, [setUeData, ueState])

return (
<>
<TableRow>
Expand All @@ -69,7 +67,7 @@ const UeTable = ({ ueData, setUeData }: UeTablePropsType) => {
<TableCell component="th" scope="row">
{ueState.name}
</TableCell>
<TableCell align={'center'}>{averageState ?? 'N/A'}</TableCell>
<TableCell align={'center'}>{averageRef.current ?? 'N/A'}</TableCell>
<TableCell align="right">{ueState.ects}</TableCell>
</TableRow>
<TableRow>
Expand Down Expand Up @@ -105,7 +103,7 @@ const UeTable = ({ ueData, setUeData }: UeTablePropsType) => {
Moyenne
</TableCell>
<TableCell align="center" colSpan={2}>
{averageState ?? 'N/A'}
{averageRef.current ?? 'N/A'}
</TableCell>
</TableRow>
</TableBody>
Expand Down
36 changes: 17 additions & 19 deletions app/components/hub/average/Year.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { YearData } from 'constants/modules/average/types'
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import Semester from './Semester'

interface SemestersPropsType {
Expand All @@ -9,24 +9,7 @@ interface SemestersPropsType {

const Year = ({ yearData, setYearData }: SemestersPropsType) => {
const [yearState, setYearState] = useState(yearData)
const [averageState, setAverageState] = useState(yearData.average)

useEffect(() => {
if (yearData !== yearState) {
setYearState(yearData)
}
}, [yearData])

useEffect(() => {
setYearData({
...yearState,
average: averageState,
})
}, [averageState])

useEffect(() => {
setAverageState(computeYearAverage(yearState))
}, [yearState])
const averageRef = useRef(yearData.average)

const computeYearAverage = ({ semesters }: YearData): number | undefined => {
let yearAverage = 0
Expand All @@ -52,6 +35,21 @@ const Year = ({ yearData, setYearData }: SemestersPropsType) => {
return Math.round(yearAverage * 1000) / 1000
}

useEffect(() => {
if (yearData !== yearState) {
setYearState(yearData)
}
}, [yearData, yearState])

useEffect(() => {
averageRef.current = computeYearAverage(yearState)

setYearData({
...yearState,
average: averageRef.current,
})
}, [setYearData, yearState])

return (
<>
{yearState.semesters.map((semester, semesterIndex) => (
Expand Down

0 comments on commit b0eee11

Please sign in to comment.