@@ -2,8 +2,6 @@ import React, { useMemo, useEffect } from 'react'
22import { useTranslation } from 'react-i18next'
33import { useParams } from 'react-router'
44import { useSearchParams } from 'react-router-dom'
5- import { orderBy } from 'lodash-es'
6- import { useQuery } from '@tanstack/react-query'
75import { writeFileXLSX , utils } from 'xlsx'
86import { format } from 'date-fns'
97import {
@@ -30,8 +28,8 @@ import { LoadingProgress } from '../../components/common/LoadingProgress'
3028import { YearSemesterPeriodSelector } from '../../components/common/YearSemesterPeriodSelector'
3129import useHistoryState from '../../hooks/useHistoryState'
3230import { getYearRange } from '../../util/yearUtils'
33- import apiClient from '../../util/apiClient'
3431import { NorButton } from '../../components/common/NorButton'
32+ import { useOrganisationFeedbackTargets , getCourseRealisationName , generateTeacherStats } from './responsiblesUtils'
3533
3634const styles = {
3735 filtersHead : {
@@ -42,26 +40,6 @@ const styles = {
4240 } ,
4341}
4442
45- const useOrganisationFeedbackTargets = ( { code, startDate, endDate, enabled } ) => {
46- const queryKey = [ 'organisationFeedbackTargets' , code , startDate , endDate ]
47-
48- const queryFn = async ( ) => {
49- const { data : feedbackTargets } = await apiClient . get ( `/feedback-targets/for-organisation/${ code } ` , {
50- params : { startDate, endDate } ,
51- } )
52-
53- return feedbackTargets
54- }
55-
56- return useQuery ( {
57- queryKey,
58- queryFn,
59- enabled,
60- refetchOnWindowFocus : false ,
61- refetchOnMount : false ,
62- } )
63- }
64-
6543const Filters = React . memo ( ( { startDate, endDate, onChange, timeOption, setTimeOption } ) => {
6644 const { t } = useTranslation ( )
6745 const [ open , setOpen ] = React . useState ( false )
@@ -131,12 +109,6 @@ const Responsibles = () => {
131109 setSearchParams ( params , { replace : true } )
132110 } , [ startDate , endDate , timeOption ] )
133111
134- const getCourseRealisationName = fbt =>
135- fbt . courseRealisation . name [ i18n . language ] ||
136- fbt . courseRealisation . name . fi ||
137- fbt . courseRealisation . name . en ||
138- fbt . courseRealisation . name . sv
139-
140112 const handleDateChange = ( newStart , newEnd ) => {
141113 setStartDate ( newStart )
142114 setEndDate ( newEnd )
@@ -161,45 +133,7 @@ const Responsibles = () => {
161133 enabled : startDate !== null ,
162134 } )
163135
164- const teacherStats = useMemo ( ( ) => {
165- if ( ! feedbackTargets || ! Array . isArray ( feedbackTargets ) ) return [ ]
166-
167- const stats = new Map ( )
168-
169- feedbackTargets . forEach ( ( [ _year , months ] ) => {
170- if ( ! Array . isArray ( months ) ) return
171-
172- months . forEach ( ( [ _month , days ] ) => {
173- if ( ! Array . isArray ( days ) ) return
174-
175- days . forEach ( ( [ _date , fbts ] ) => {
176- if ( ! Array . isArray ( fbts ) ) return
177-
178- fbts . forEach ( fbt => {
179- if ( fbt . responsibleTeachers && Array . isArray ( fbt . responsibleTeachers ) ) {
180- fbt . responsibleTeachers . forEach ( teacher => {
181- if ( stats . has ( teacher . id ) ) {
182- stats . get ( teacher . id ) . count ++
183- stats . get ( teacher . id ) . feedbackTargets . push ( fbt )
184- } else {
185- stats . set ( teacher . id , {
186- id : teacher . id ,
187- firstName : teacher . firstName ,
188- lastName : teacher . lastName ,
189- email : teacher . email ,
190- count : 1 ,
191- feedbackTargets : [ fbt ] ,
192- } )
193- }
194- } )
195- }
196- } )
197- } )
198- } )
199- } )
200-
201- return orderBy ( Array . from ( stats . values ( ) ) , [ 'lastName' , 'firstName' ] , [ 'asc' , 'asc' ] )
202- } , [ feedbackTargets ] )
136+ const teacherStats = useMemo ( ( ) => generateTeacherStats ( feedbackTargets ) , [ feedbackTargets ] )
203137
204138 const exportSummary = ( ) => {
205139 const headers = [ t ( 'common:lastName' ) , t ( 'common:firstName' ) , t ( 'organisationSettings:feedbackTargetCount' ) ]
@@ -234,7 +168,7 @@ const Responsibles = () => {
234168 teacher . lastName ,
235169 teacher . firstName ,
236170 fbt . courseUnit . courseCode ,
237- getCourseRealisationName ( fbt ) ,
171+ getCourseRealisationName ( fbt , i18n ) ,
238172 new Date ( fbt . courseRealisation . startDate ) . toLocaleDateString ( i18n . language ) ,
239173 new Date ( fbt . courseRealisation . endDate ) . toLocaleDateString ( i18n . language ) ,
240174 ] )
@@ -328,7 +262,7 @@ const Responsibles = () => {
328262 { teacher . feedbackTargets . map ( fbt => (
329263 < TableRow key = { fbt . id } >
330264 < TableCell > { fbt . courseUnit . courseCode } </ TableCell >
331- < TableCell > { getCourseRealisationName ( fbt ) } </ TableCell >
265+ < TableCell > { getCourseRealisationName ( fbt , i18n ) } </ TableCell >
332266 < TableCell >
333267 { new Date ( fbt . courseRealisation . startDate ) . toLocaleDateString ( i18n . language ) }
334268 </ TableCell >
0 commit comments