@@ -21,136 +21,106 @@ import worklogInput from "./workLogInput";
2121import voucherLogInput from "./voucherLogInput" ;
2222
2323const WORK_TABLE_HEADERS = [
24- { id : "workedAt_label" , type : "date" , name : "work date" , flex : 2 , sortBy : "workedAt_num" } ,
25- { id : "duration" , type : "number" , name : "duration" , flex : 1 } ,
26- { id : "description" , type : "string" , name : "description" , flex : 3 } ,
27- { id : "loggedBy" , type : "string" , name : "log by" , flex : 2 } ,
28- { id : "loggedFor" , type : "string" , name : "log for" , flex : 2 } ,
24+ { id : "workedAt_label" , type : "date" , name : "work date" , flex : 2 , sortBy : "workedAt_num" } ,
25+ { id : "duration" , type : "number" , name : "duration" , flex : 1 } ,
26+ { id : "description" , type : "string" , name : "description" , flex : 3 } ,
27+ { id : "loggedBy" , type : "string" , name : "log by" , flex : 2 } ,
28+ { id : "loggedFor" , type : "string" , name : "log for" , flex : 2 } ,
2929] ;
3030
3131const VOUCHER_TABLE_HEADERS = [
32- { id : "usedAt_label " , type : "date" , name : "usage date" , flex : 2 , sortBy : "usedAt_num" } ,
33- { id : "amount" , type : "number" , name : "vouchers" , flex : 2 } ,
34- { id : "description" , type : "string" , name : "description" , flex : 4 } ,
35- { id : "loggedFor" , type : "string" , name : "log for" , flex : 2 } ,
32+ { id : "usedAt " , type : "date" , name : "usage date" , flex : 2 , sortBy : "usedAt_num" } ,
33+ { id : "amount" , type : "number" , name : "vouchers" , flex : 2 } ,
34+ { id : "description" , type : "string" , name : "description" , flex : 4 } ,
35+ { id : "loggedFor" , type : "string" , name : "log for" , flex : 2 } ,
3636] ;
3737
3838function LogsPage ( ) {
3939 const [ users , setUsers ] = useState ( [ ] ) ;
4040 const [ workGroups , setWorkGroups ] = useState ( [ ] ) ;
4141 const [ workLogs , setWorkLogs ] = useState ( [ ] ) ;
4242 const [ voucherLogs , setVoucherLogs ] = useState ( [ ] ) ;
43- const [ lastSemVoucherLogs , setLastSemVoucherLogs ] = useState ( [ ] ) ;
43+ const [ voucherAmount , setVoucherAmount ] = useState ( 0 ) ;
4444
4545 const [ mode , setMode ] = useState ( false ) ;
46- const [ vouchersEarned , setVouchersEarned ] = useState ( 0 ) ;
47- const [ vouchersEarnedLastSemester , setVouchersEarnedLastSemester ] = useState ( 0 ) ;
48- const [ vouchersUsed , setVouchersUsed ] = useState ( 0 ) ;
49- const [ vouchersUsedLastSemester , setVouchersUsedLastSemester ] = useState ( 0 ) ;
50-
5146 const [ refresh , setRefresh ] = useState ( false ) ;
5247
5348 const session = useSession ( ) ;
5449
55- let startOfSemester = false ;
56- let lastSemester ;
57-
58- const dateTime = new Date ( ) ;
59- const deadline = new Date ( ) ;
60- deadline . setDate ( 16 ) ;
61- deadline . setMonth ( 1 ) ;
62-
63- if ( dateTime < deadline ) {
64- startOfSemester = true ;
65- lastSemester = {
66- id : session . data . semester . id - 1 ,
67- vouchersEarned : 0 ,
68- vouchersUsed : 0 ,
69- }
70- }
71-
7250 useEffect ( ( ) => {
7351 fetch ( "/api/v2/users" )
74- . then ( res => res . json ( ) )
75- . then ( data => {
76- setUsers (
77- data . users . map ( ( e ) => ( {
78- ...e ,
79- name : `${ e . firstName } ${ e . lastName } `
80- } ) )
81- )
82- } )
52+ . then ( res => res . json ( ) )
53+ . then ( data => {
54+ setUsers (
55+ data . users . map ( ( e ) => ( {
56+ ...e ,
57+ name : `${ e . firstName } ${ e . lastName } `
58+ } ) )
59+ )
60+ } )
8361
8462 fetch ( "/api/v2/workGroups" )
85- . then ( res => res . json ( ) )
86- . then ( groups => {
87- setWorkGroups ( groups . groups )
88- } )
63+ . then ( res => res . json ( ) )
64+ . then ( groups => {
65+ setWorkGroups ( groups . groups )
66+ } )
8967 } , [ ] ) ;
9068
9169 useEffect ( ( ) => {
92- fetch ( "/api/v2/workLogs" )
93- . then ( res => res . json ( ) )
94- . then ( resData => {
95- handleWorkLogs ( resData . workLogs , session , setWorkLogs , setVouchersEarned )
96- } )
97-
98- fetch ( "/api/v2/voucherLogs" )
99- . then ( res => res . json ( ) )
100- . then ( voucherLog => {
101- handleVoucherLogs ( voucherLog . voucherLogs , session , setVoucherLogs , setVouchersUsed )
102- } )
103-
104- if ( startOfSemester ) {
105- fetch ( "/api/v2/workLogs?semesterId=" + lastSemester . id )
70+ fetch ( "/api/v2/work" )
10671 . then ( res => res . json ( ) )
10772 . then ( resData => {
108- handleLastSemesterWorkLogs ( resData . workLogs , session , setVouchersEarnedLastSemester )
73+ handleWorkLogs ( resData . workLogs , session , setWorkLogs )
10974 } )
11075
111- fetch ( "/api/v2/voucherLogs?semesterId=" + lastSemester . id )
76+ fetch ( "/api/v2/vouchers?action=logs" )
11277 . then ( res => res . json ( ) )
113- . then ( voucherLog => {
114- handleLastSemesterVoucherLogs ( voucherLog . voucherLogs , session , setLastSemVoucherLogs , setVouchersUsedLastSemester )
78+ . then ( res => {
79+ res = res . map ( log => ( {
80+ ...log ,
81+ usedAt : format ( parseISO ( log . usedAt ) , "dd.MM HH:mm" ) . toLowerCase ( ) ,
82+ usedAt_num : parseISO ( log . usedAt ) . getTime ( )
83+ } ) )
84+ setVoucherLogs ( res )
11585 } )
116- }
86+
87+ fetch ( "/api/v2/vouchers?action=amount" )
88+ . then ( res => res . json ( ) )
89+ . then ( res => {
90+ setVoucherAmount ( res . voucherAmount )
91+ } )
92+
11793 } , [ refresh ] )
118-
94+
11995 const worklogInputLayout = worklogInput (
12096 session ,
12197 users ,
12298 workGroups ,
12399 setRefresh
124100 ) ;
125-
101+
126102 const voucherLogInputLayout = voucherLogInput (
127103 session ,
128- vouchersEarned ,
129- vouchersUsed ,
130- setRefresh ,
131- vouchersEarnedLastSemester ,
132- vouchersUsedLastSemester ,
133- startOfSemester
104+ voucherAmount ,
105+ setRefresh
134106 ) ;
135-
107+
136108 const inputLayout = mode ? worklogInputLayout : voucherLogInputLayout ;
137109 const tableLayout = (
138110 < CustomTable
139111 key = { mode ? "workLogTable" : "voucherLogTable" }
140112 headers = { mode ? WORK_TABLE_HEADERS : VOUCHER_TABLE_HEADERS }
141- data = { mode ? workLogs : voucherLogs . concat ( lastSemVoucherLogs ) }
113+ data = { mode ? workLogs : voucherLogs }
142114 defaultFilterBy = "loggedFor"
143115 />
144116 ) ;
145-
146- const registerButtonText = `Register ${
147- useMediaQuery ( cybTheme . breakpoints . down ( "md" ) ) ? "" : "Work"
148- } `;
149-
150- const useButtonText = `Use ${
151- useMediaQuery ( cybTheme . breakpoints . down ( "md" ) ) ? "" : "Voucher"
152- } `;
153-
117+
118+ const registerButtonText = `Register ${ useMediaQuery ( cybTheme . breakpoints . down ( "md" ) ) ? "" : "Work"
119+ } `;
120+
121+ const useButtonText = `Use ${ useMediaQuery ( cybTheme . breakpoints . down ( "md" ) ) ? "" : "Voucher"
122+ } `;
123+
154124 return (
155125 < Box >
156126 < PageHeader text = "Logs" />
@@ -189,8 +159,8 @@ function LogsPage() {
189159 ) ;
190160}
191161
192- function handleWorkLogs ( logs , session , setWorkLogs , setVouchersEarned ) {
193-
162+ function handleWorkLogs ( logs , session , setWorkLogs ) {
163+
194164 const newWorkLogs = logs . map ( ( log ) => ( {
195165 ...log ,
196166 loggedBy : getUserName ( log . LoggedByUser ) ,
@@ -199,81 +169,7 @@ function handleWorkLogs(logs, session, setWorkLogs, setVouchersEarned) {
199169 workedAt_label : format ( parseISO ( log . workedAt ) , "dd.MM HH:mm" ) . toLowerCase ( ) ,
200170 } ) ) ;
201171
202- const newVouchersEarned = logs
203- . filter ( ( e ) => {
204- const person = e . LoggedForUser ;
205- return person && person . id == session . data . user . id ;
206- } )
207- . reduce ( ( total , e ) => {
208- return ( total += e . duration * 0.5 ) ;
209- } , 0.0 ) ;
210-
211172 setWorkLogs ( newWorkLogs ) ;
212- setVouchersEarned ( newVouchersEarned ) ;
213- }
214-
215- function handleLastSemesterWorkLogs ( logs , session , setVouchersEarnedLastSemester ) {
216-
217- const newVouchersEarned = logs
218- . filter ( ( e ) => {
219- const person = e . LoggedForUser ;
220- return person && person . id == session . data . user . id ;
221- } )
222- . reduce ( ( total , e ) => {
223- return ( total += e . duration * 0.5 ) ;
224- } , 0.0 ) ;
225-
226- setVouchersEarnedLastSemester ( newVouchersEarned ) ;
227- }
228-
229- function handleVoucherLogs ( logs , session , setVoucherLogs , setVouchersUsed ) {
230-
231- const newVoucherLogs = logs . map ( ( log ) => ( {
232- ...log ,
233- loggedFor : getUserName ( log . LoggedForUser ) ,
234- usedAt_num : parseISO ( log . usedAt ) . getTime ( ) ,
235- usedAt_label : format ( parseISO ( log . usedAt ) , "dd.MM HH:mm" ) . toLowerCase ( ) ,
236- } ) ) ;
237-
238- const newVouchersUsed = logs
239- . filter ( ( e ) => {
240- const person = e . LoggedForUser ;
241- const personId = person . id ;
242- return personId == session . data . user . id ;
243- } )
244- . reduce ( ( total , e ) => {
245- return ( total += e . amount ) ;
246- } , 0.0 ) ;
247-
248- setVoucherLogs ( newVoucherLogs ) ;
249- setVouchersUsed ( parseFloat ( newVouchersUsed ) ) ;
250- }
251-
252- function handleLastSemesterVoucherLogs ( logs , session , setLastSemVoucherLogs , setVouchersUsedLastSemester ) {
253-
254- let newVoucherLogs = logs . map ( ( log ) => ( {
255- ...log ,
256- loggedFor : getUserName ( log . LoggedForUser ) ,
257- usedAt_num : parseISO ( log . usedAt ) . getTime ( ) ,
258- usedAt_label : format ( parseISO ( log . usedAt ) , "dd.MM HH:mm" ) . toLowerCase ( ) ,
259- } ) )
260- newVoucherLogs = newVoucherLogs . filter ( ( e ) => {
261- const year = new Date ( e . usedAt ) . getFullYear ( ) ;
262- return year == new Date ( ) . getFullYear ( ) ;
263- } ) ;
264-
265- const newVouchersUsed = logs
266- . filter ( ( e ) => {
267- const person = e . LoggedForUser ;
268- const personId = person . id ;
269- return personId == session . data . user . id ;
270- } )
271- . reduce ( ( total , e ) => {
272- return ( total += e . amount ) ;
273- } , 0.0 ) ;
274-
275- setLastSemVoucherLogs ( newVoucherLogs ) ;
276- setVouchersUsedLastSemester ( parseFloat ( newVouchersUsed ) ) ;
277173}
278174
279175export default authWrapper ( LogsPage ) ;
0 commit comments