1
1
import * as React from 'react' ;
2
+ import { Route , Switch } from 'react-router-dom' ;
2
3
3
4
import ContestObject from 'contests/ContestObject' ;
4
5
import DataSetObject from 'contests/DataSetObject' ;
@@ -19,9 +20,11 @@ import SubmitResults from 'contests/SubmitResults';
19
20
20
21
export interface ContestAppProps extends React . Props < ContestApp > {
21
22
children : React . ReactElement < any > ;
22
- params : {
23
- contestId : string ;
24
- problemId : string ;
23
+ match : {
24
+ params : {
25
+ contestId : string ;
26
+ problemId : string ;
27
+ } ;
25
28
} ;
26
29
}
27
30
@@ -65,7 +68,7 @@ export default class ContestApp extends React.Component<ContestAppProps, Contest
65
68
}
66
69
67
70
public async fetchContest ( ) : Promise < void > {
68
- const responseContest : Response = await fetch ( `/api/contests/${ this . props . params . contestId } ${ t ( 'locale' ) } ` , {
71
+ const responseContest : Response = await fetch ( `/api/contests/${ this . props . match . params . contestId } ${ t ( 'locale' ) } ` , {
69
72
credentials : 'same-origin' ,
70
73
} ) ;
71
74
@@ -119,7 +122,7 @@ export default class ContestApp extends React.Component<ContestAppProps, Contest
119
122
let submissions : SubmissionObject [ ] ;
120
123
if ( contest . problems ) {
121
124
const responseSubmissions : Response = await fetch (
122
- `/api/contests/${ this . props . params . contestId } /submissions${ t ( 'locale' ) } ` ,
125
+ `/api/contests/${ this . props . match . params . contestId } /submissions${ t ( 'locale' ) } ` ,
123
126
{
124
127
credentials : 'same-origin' ,
125
128
}
@@ -162,7 +165,7 @@ export default class ContestApp extends React.Component<ContestAppProps, Contest
162
165
const formData : FormData = new FormData ( ) ;
163
166
formData . append ( this . csrfParam , this . csrfToken ) ;
164
167
165
- const response : Response = await fetch ( `/api/contests/${ this . props . params . contestId } /entry` , {
168
+ const response : Response = await fetch ( `/api/contests/${ this . props . match . params . contestId } /entry` , {
166
169
method : 'post' ,
167
170
credentials : 'same-origin' ,
168
171
body : formData ,
@@ -200,7 +203,7 @@ export default class ContestApp extends React.Component<ContestAppProps, Contest
200
203
201
204
this . changeAnswerForm ( problemId , dataSetId , '' ) ;
202
205
203
- const response : Response = await fetch ( `/api/contests/${ this . props . params . contestId } /submissions` , {
206
+ const response : Response = await fetch ( `/api/contests/${ this . props . match . params . contestId } /submissions` , {
204
207
method : 'post' ,
205
208
credentials : 'same-origin' ,
206
209
body : formData ,
@@ -265,7 +268,7 @@ export default class ContestApp extends React.Component<ContestAppProps, Contest
265
268
}
266
269
267
270
public async fetchRanking ( ) : Promise < void > {
268
- const response : Response = await fetch ( `/api/contests/${ this . props . params . contestId } /ranking${ t ( 'locale' ) } ` , {
271
+ const response : Response = await fetch ( `/api/contests/${ this . props . match . params . contestId } /ranking${ t ( 'locale' ) } ` , {
269
272
credentials : 'same-origin' ,
270
273
} ) ;
271
274
@@ -337,7 +340,7 @@ export default class ContestApp extends React.Component<ContestAppProps, Contest
337
340
338
341
public async fetchTime ( ) : Promise < TimerObject > {
339
342
let fetchedTime : TimerObject ;
340
- const responseTime : Response = await fetch ( `/api/contests/${ this . props . params . contestId } ${ t ( 'locale' ) } ` , {
343
+ const responseTime : Response = await fetch ( `/api/contests/${ this . props . match . params . contestId } ${ t ( 'locale' ) } ` , {
341
344
credentials : 'same-origin' ,
342
345
} ) ;
343
346
@@ -401,38 +404,55 @@ export default class ContestApp extends React.Component<ContestAppProps, Contest
401
404
return (
402
405
< div className = "container" >
403
406
< Navigation contest = { this . state . contest } />
404
- { this . props . children &&
405
- this . props . children . type === ContestHome && (
406
- < ContestHome contest = { this . state . contest } join = { this . join . bind ( this ) } />
407
- ) }
408
- { this . props . children &&
409
- this . props . children . type === Problem &&
410
- this . state . contest . problems && (
411
- < Problem
412
- contest = { this . state . contest }
413
- problem = { this . state . contest . problems . find (
414
- ( problem : ProblemObject ) => problem . id === + this . props . params . problemId
415
- ) }
416
- changeAnswerForm = { this . changeAnswerForm . bind ( this ) }
417
- submit = { this . submit . bind ( this ) }
418
- />
419
- ) }
420
- { this . props . children &&
421
- this . props . children . type === Submissions && (
422
- < Submissions contest = { this . state . contest } submissions = { this . state . submissions } />
423
- ) }
424
- { this . props . children &&
425
- this . props . children . type === Ranking &&
426
- this . state . users && < Ranking contest = { this . state . contest } users = { this . state . users } /> }
407
+ < Switch >
408
+ < Route
409
+ exact = { true }
410
+ path = "/contests/:contestId"
411
+ render = { ( ) : JSX . Element => < ContestHome contest = { this . state . contest } join = { this . join . bind ( this ) } /> }
412
+ /> ;
413
+ < Route
414
+ exact = { true }
415
+ path = "/contests/:contestId/problems/:problemId"
416
+ render = { ( props : any ) : JSX . Element => {
417
+ return ! this . state . contest . problems ? null : (
418
+ < Problem
419
+ contest = { this . state . contest }
420
+ problem = { this . state . contest . problems . find (
421
+ ( problem : ProblemObject ) => problem . id === + props . match . params . problemId
422
+ ) }
423
+ changeAnswerForm = { this . changeAnswerForm . bind ( this ) }
424
+ submit = { this . submit . bind ( this ) }
425
+ />
426
+ ) ;
427
+ } }
428
+ />
429
+ < Route
430
+ exact = { true }
431
+ path = "/contests/:contestId/ranking"
432
+ render = { ( ) : JSX . Element => {
433
+ return ! this . state . users ? null : < Ranking contest = { this . state . contest } users = { this . state . users } /> ;
434
+ } }
435
+ />
436
+ < Route
437
+ exact = { true }
438
+ path = "/contests/:contestId/submissions"
439
+ render = { ( ) : JSX . Element => (
440
+ < Submissions contest = { this . state . contest } submissions = { this . state . submissions } />
441
+ ) }
442
+ />
443
+ < Route
444
+ exact = { true }
445
+ path = "/contests/:contestId/editorials/:editorialId"
446
+ render = { ( ) : JSX . Element => < Editorial editorial = { this . state . contest . editorial } /> }
447
+ />
448
+ </ Switch >
427
449
{ this . state . submissions && (
428
450
< SubmitResults
429
451
submissions = { this . state . submissions }
430
452
submitResults = { this . state . submitResults }
431
453
closeSubmitResults = { this . closeSubmitResults . bind ( this ) }
432
454
/>
433
455
) }
434
- { this . props . children &&
435
- this . props . children . type === Editorial && < Editorial editorial = { this . state . contest . editorial } /> }
436
456
</ div >
437
457
) ;
438
458
}
0 commit comments