@@ -6,7 +6,7 @@ import { TeamEventsAPI } from '../../../API/TeamEventsAPI';
6
6
import TeamEventCreditDashboard from './TeamEventsCreditDashboard' ;
7
7
import styles from './TeamEventCreditsForm.module.css' ;
8
8
import ImagesAPI from '../../../API/ImagesAPI' ;
9
- import { INITIATIVE_EVENTS } from '../../../consts' ;
9
+ import { INITIATIVE_EVENTS , TEC_DEADLINES } from '../../../consts' ;
10
10
11
11
const TeamEventCreditForm : React . FC = ( ) => {
12
12
// When the user is logged in, `useSelf` always return non-null data.
@@ -31,6 +31,53 @@ const TeamEventCreditForm: React.FC = () => {
31
31
} ) ;
32
32
} , [ ] ) ;
33
33
34
+ const approvedTECDates = approvedAttendance
35
+ . map ( ( attendance ) => {
36
+ const matchingEvent = teamEventInfoList . find ( ( event ) => event . uuid === attendance . eventUuid ) ;
37
+ return matchingEvent
38
+ ? {
39
+ date : new Date ( matchingEvent . date ) ,
40
+ credits : parseFloat ( matchingEvent . numCredits )
41
+ }
42
+ : null ;
43
+ } )
44
+ . filter ( ( entry ) : entry is { date : Date ; credits : number } => entry !== null ) ;
45
+
46
+ const getTECPeriod = ( submissionDate : Date ) => {
47
+ const currentPeriodIndex = TEC_DEADLINES . findIndex ( ( date ) => submissionDate <= date ) ;
48
+ if ( currentPeriodIndex === - 1 ) {
49
+ return TEC_DEADLINES . length ;
50
+ }
51
+ return currentPeriodIndex ;
52
+ } ;
53
+
54
+ const tecCounts : number [ ] = Array . from ( { length : TEC_DEADLINES . length } , ( ) => 0 ) ;
55
+ approvedTECDates . forEach ( ( { date, credits } ) => {
56
+ const period = getTECPeriod ( date ) ;
57
+ if ( period < tecCounts . length ) tecCounts [ period ] += credits ;
58
+ } ) ;
59
+
60
+ const calculateCredits = ( ) => {
61
+ const currentPeriod = getTECPeriod ( new Date ( ) ) ;
62
+ const previousPeriod = currentPeriod > 0 ? currentPeriod - 1 : null ;
63
+
64
+ const periodCredits = tecCounts [ currentPeriod ] || 0 ;
65
+ const previousPeriodCredits = previousPeriod !== null ? tecCounts [ currentPeriod - 1 ] : 1 ;
66
+
67
+ if ( currentPeriod === 0 ) {
68
+ return periodCredits < 1 ? 1 - periodCredits : 0 ;
69
+ }
70
+ if ( previousPeriodCredits < 1 ) {
71
+ return periodCredits + previousPeriodCredits < 2
72
+ ? 2 - previousPeriodCredits - periodCredits
73
+ : 0 ;
74
+ }
75
+
76
+ return periodCredits < 1 ? 1 - periodCredits : 0 ;
77
+ } ;
78
+
79
+ const requiredCredits = calculateCredits ( ) ;
80
+
34
81
const handleAddIconClick = ( ) => {
35
82
setImages ( ( images ) => [ ...images , '' ] ) ;
36
83
} ;
@@ -142,12 +189,32 @@ const TeamEventCreditForm: React.FC = () => {
142
189
< h1 > Submit Team Event Credits</ h1 >
143
190
< p >
144
191
Earn team event credits for participating in DTI events! Fill out this form every time and
145
- attach a picture of yourself at the event to receive credit.
192
+ attach a picture of yourself at the event to receive credit. The current 5-week TEC period
193
+ ends on{ ' ' }
194
+ { getTECPeriod ( new Date ( ) ) < TEC_DEADLINES . length
195
+ ? TEC_DEADLINES [ getTECPeriod ( new Date ( ) ) ] . toDateString ( )
196
+ : 'No upcoming period' }
197
+ .
146
198
</ p >
147
199
< div className = { styles . inline } >
148
200
< label className = { styles . bold } >
149
201
Select a Team Event: < span className = { styles . red_color } > *</ span >
150
202
</ label >
203
+ < div className = { styles . bold } >
204
+ { requiredCredits > 1 && (
205
+ < span className = { styles . red_color } >
206
+ You submitted { 2 - requiredCredits } TEC last period so you must submit at least{ ' ' }
207
+ { requiredCredits } TEC for this 5-week period.
208
+ </ span >
209
+ ) }
210
+ </ div >
211
+ < div className = { styles . bold } >
212
+ { requiredCredits === 0 && (
213
+ < span className = { styles . red_color } >
214
+ You have already submitted a TEC for this 5-week period.
215
+ </ span >
216
+ ) }
217
+ </ div >
151
218
< div className = { styles . center_and_flex } >
152
219
{ teamEventInfoList ? (
153
220
< Dropdown
@@ -264,6 +331,8 @@ const TeamEventCreditForm: React.FC = () => {
264
331
rejectedAttendance = { rejectedAttendance }
265
332
isAttendanceLoading = { isAttendanceLoading }
266
333
setPendingAttendance = { setPendingAttendance }
334
+ requiredPeriodCredits = { requiredCredits }
335
+ tecCounts = { tecCounts }
267
336
/>
268
337
</ Form >
269
338
</ div >
0 commit comments