@@ -15,6 +15,13 @@ export const virtualChallengeSchema = function() {
1515 return new schema . Entity ( 'virtualChallenges' )
1616}
1717
18+ /*
19+ * Time, in hours, until new virtual challenges expire. Defaults to 36 hours if
20+ * nothing is specified in the .env file.
21+ */
22+ export const DEFAULT_EXPIRATION_DURATION =
23+ parseInt ( _get ( process . env , 'REACT_APP_VIRTUAL_CHALLENGE_DURATION' , 36 ) , 10 )
24+
1825// redux actions
1926const RECEIVE_VIRTUAL_CHALLENGES = 'RECEIVE_VIRTUAL_CHALLENGES'
2027
@@ -59,39 +66,78 @@ export const fetchVirtualChallenge = function(virtualChallengeId) {
5966}
6067
6168/**
62- * Creates a new virtual challenge with the given name and tasks.
69+ * Creates a new virtual challenge with the given name and tasks. If an
70+ * explicit expiration timestamp is given, it'll be used; otherwise the virtual
71+ * challenge will be set to expire after the default configured duration.
6372 */
6473export const createVirtualChallenge = function ( name , taskIds , expiration ) {
6574 return function ( dispatch ) {
6675 const challengeData = {
6776 name,
6877 taskIdList : taskIds ,
69- expiry : expiration ? expiration : addHours ( new Date ( ) , 36 ) . getTime ( )
78+ expiry : expiration ? expiration :
79+ addHours ( new Date ( ) , DEFAULT_EXPIRATION_DURATION ) . getTime ( )
7080 }
7181
72- return new Endpoint ( api . virtualChallenge . create , {
73- schema : virtualChallengeSchema ( ) ,
74- json : challengeData ,
75- } ) . execute ( ) . then ( normalizedResults => {
76- dispatch ( receiveVirtualChallenges ( normalizedResults . entities ) )
77- return _get ( normalizedResults ,
78- `entities.virtualChallenges.${ normalizedResults . result } ` )
79- } ) . catch ( ( serverError ) => {
80- if ( serverError . response && serverError . response . status === 401 ) {
81- // If we get an unauthorized, we assume the user is not logged
82- // in (or no longer logged in with the server).
83- dispatch ( logoutUser ( ) )
84- dispatch ( addError ( AppErrors . user . unauthorized ) )
85- }
86- else {
87- console . log ( serverError . response || serverError )
88- dispatch ( addServerError ( AppErrors . virtualChallenge . createFailure ,
89- serverError ) )
90- }
91- } )
82+ return saveVirtualChallenge (
83+ dispatch ,
84+ new Endpoint ( api . virtualChallenge . create , {
85+ schema : virtualChallengeSchema ( ) ,
86+ json : challengeData ,
87+ } )
88+ )
89+ }
90+ }
91+
92+ /**
93+ * Renews the expiration time of the virtual challenge, either setting it to
94+ * the explicit expiration timestamp given or resetting it to the default
95+ * configured duration if no expiration is specified.
96+ */
97+ export const renewVirtualChallenge = function ( virtualChallengeId , expiration ) {
98+ return function ( dispatch ) {
99+ const challengeData = {
100+ expiry : expiration ? expiration :
101+ addHours ( new Date ( ) , DEFAULT_EXPIRATION_DURATION ) . getTime ( )
102+ }
103+
104+ return saveVirtualChallenge (
105+ dispatch ,
106+ new Endpoint ( api . virtualChallenge . edit , {
107+ variables : { id : virtualChallengeId } ,
108+ schema : virtualChallengeSchema ( ) ,
109+ json : challengeData ,
110+ } )
111+ )
92112 }
93113}
94114
115+ /**
116+ * Executes the given endpoint, saving the virtual challenge, and
117+ * processes the response.
118+ *
119+ * @private
120+ */
121+ export const saveVirtualChallenge = function ( dispatch , endpoint ) {
122+ return endpoint . execute ( ) . then ( normalizedResults => {
123+ dispatch ( receiveVirtualChallenges ( normalizedResults . entities ) )
124+ return _get ( normalizedResults ,
125+ `entities.virtualChallenges.${ normalizedResults . result } ` )
126+ } ) . catch ( ( serverError ) => {
127+ if ( serverError . response && serverError . response . status === 401 ) {
128+ // If we get an unauthorized, we assume the user is not logged
129+ // in (or no longer logged in with the server).
130+ dispatch ( logoutUser ( ) )
131+ dispatch ( addError ( AppErrors . user . unauthorized ) )
132+ }
133+ else {
134+ console . log ( serverError . response || serverError )
135+ dispatch ( addServerError ( AppErrors . virtualChallenge . createFailure ,
136+ serverError ) )
137+ }
138+ } )
139+ }
140+
95141// redux reducers
96142//
97143/**
0 commit comments