@@ -72,91 +72,66 @@ try {
7272 // storage.set("created", Date.now());
7373 // }
7474
75- await fetch ( domain + '/bulk_load?ta=true' , {
76- method : "POST" ,
77- headers : {
78- "Content-Type" : "application/json" ,
79- } ,
80- body : JSON . stringify ( {
81- usr : storage . get ( "code" ) ,
82- pwd : storage . get ( "pwd" ) ,
83- fields : [ "courses" , "segments" , "questions" , "answers" , "responses" ]
84- } ) ,
85- } )
86- . then ( async ( r ) => {
87- if ( ! r . ok ) {
88- try {
89- var re = await r . json ( ) ;
90- if ( re . error || re . message ) {
91- ui . toast ( re . error || re . message , 5000 , "error" , "bi bi-exclamation-triangle-fill" ) ;
92- throw new Error ( re . error || re . message ) ;
93- } else {
94- throw new Error ( "API error" ) ;
95- }
96- } catch ( e ) {
97- throw new Error ( e . message || "API error" ) ;
98- }
99- }
100- return await r . json ( ) ;
101- } )
102- . then ( async bulkLoad => {
103- courses = bulkLoad . courses ;
104- segments = bulkLoad . segments ;
105- questions = bulkLoad . questions ;
106- answers = bulkLoad . answers ;
107- responses = bulkLoad . responses ;
108- if ( courses . length === 0 ) return ui . modal ( {
109- title : 'Enter Password' ,
110- body : `<p>Enter the assigned password for TA seat code <code>${ storage . get ( "code" ) } </code>.</p>` ,
111- input : {
112- type : 'password'
113- } ,
114- buttons : [
115- {
116- text : 'Verify' ,
117- class : 'submit-button' ,
118- onclick : ( inputValue ) => {
119- storage . set ( "pwd" , inputValue ) ;
120- init ( ) ;
121- } ,
122- close : true ,
123- } ,
124- ] ,
125- } ) ;
126- await auth . loadAdminSettings ( courses ) ;
127- if ( ! noReloadCourse ) {
128- document . getElementById ( "course-period-input" ) . innerHTML = "" ;
129- courses . sort ( ( a , b ) => a . id - b . id ) . forEach ( course => {
130- var coursePeriods = JSON . parse ( course . periods ) ;
131- const option = document . createElement ( "option" ) ;
132- option . value = course . id ;
133- option . innerHTML = document . getElementById ( "course-input" ) ? course . name : `${ course . name } ${ ( coursePeriods . length > 0 ) ? ` (Period${ ( coursePeriods . length > 1 ) ? 's' : '' } ${ coursePeriods . join ( ', ' ) } )` : '' } ` ;
134- document . getElementById ( "course-period-input" ) . appendChild ( option ) ;
135- } ) ;
136- }
137- document . getElementById ( "course-period-input" ) . addEventListener ( "input" , updateCourses ) ;
138- document . getElementById ( "course-period-input" ) . addEventListener ( "change" , updateResponses ) ;
139- document . getElementById ( "filter-segment-input" ) . addEventListener ( "change" , updateResponses ) ;
140- document . getElementById ( "sort-question-input" ) . addEventListener ( "input" , updateResponses ) ;
141- document . getElementById ( "sort-seat-input" ) . addEventListener ( "input" , updateResponses ) ;
142- if ( document . getElementById ( "filter-segment-input" ) ) updateCourses ( ) ;
143- if ( responses . find ( response => String ( response . seatCode ) . includes ( 'xx' ) ) ) document . getElementById ( "checker" ) . classList . add ( "anonymous" ) ;
144- if ( ! noReloadCourse ) document . getElementById ( "course-period-input" ) . value = ( storage . get ( 'period' ) && courses . find ( c => String ( c . id ) === String ( storage . get ( 'period' ) ) ) ) ? storage . get ( 'period' ) : ( ( ( ui . defaultCourse !== null ) && courses . find ( c => String ( c . id ) === String ( ui . defaultCourse ) ) ) ? ui . defaultCourse : courses . find ( c => JSON . parse ( c . periods ) . includes ( Number ( String ( responses . sort ( ( a , b ) => String ( a . seatCode ) [ 0 ] - String ( b . seatCode ) [ 0 ] ) [ 0 ] ?. seatCode ) [ 0 ] ) ) ) ? courses . find ( c => JSON . parse ( c . periods ) . includes ( Number ( String ( responses . sort ( ( a , b ) => String ( a . seatCode ) [ 0 ] - String ( b . seatCode ) [ 0 ] ) [ 0 ] ?. seatCode ) [ 0 ] ) ) ) . id : 0 ) ;
145- await updateResponses ( ) ;
146- active = true ;
147- ui . stopLoader ( ) ;
148- ui . toast ( "Data restored." , 1000 , "info" , "bi bi-cloud-arrow-down" ) ;
149- document . getElementById ( "filter-segment-input" ) . addEventListener ( "change" , ( ) => {
150- document . getElementById ( "sort-question-input" ) . value = "" ;
151- const event = new Event ( 'input' , { bubbles : true } ) ;
152- document . getElementById ( "sort-question-input" ) . dispatchEvent ( event ) ;
153- } ) ;
154- ui . reloadUnsavedInputs ( ) ;
155- } )
75+ await auth . bulkLoad ( [ "courses" , "segments" , "questions" , "answers" , "responses" ] , storage . get ( "code" ) , storage . get ( "pwd" ) , false , true )
15676 . catch ( ( e ) => {
15777 console . error ( e ) ;
158- if ( ( e . error === "Access denied." ) || ( e . message === "Access denied." ) ) return auth . ta ( init ) ;
78+ if ( ! e . message || ( e . message && ! e . message . includes ( "." ) ) ) ui . view ( "api-fail" ) ;
79+ if ( ( e . error === "Access denied." ) || ( e . message === "Access denied." ) ) return auth . admin ( init ) ;
15980 } ) ;
81+ await storage . idbReady ;
82+ const bulkLoad = ( await storage . idbGet ( 'adminCache' ) ) || storage . get ( "adminCache" ) || { } ;
83+ courses = bulkLoad . courses ;
84+ segments = bulkLoad . segments ;
85+ questions = bulkLoad . questions ;
86+ answers = bulkLoad . answers ;
87+ responses = bulkLoad . responses ;
88+ if ( courses . length === 0 ) return ui . modal ( {
89+ title : 'Enter Password' ,
90+ body : `<p>Enter the assigned password for TA seat code <code>${ storage . get ( "code" ) } </code>.</p>` ,
91+ input : {
92+ type : 'password'
93+ } ,
94+ buttons : [
95+ {
96+ text : 'Verify' ,
97+ class : 'submit-button' ,
98+ onclick : ( inputValue ) => {
99+ storage . set ( "pwd" , inputValue ) ;
100+ init ( ) ;
101+ } ,
102+ close : true ,
103+ } ,
104+ ] ,
105+ } ) ;
106+ await auth . loadAdminSettings ( courses ) ;
107+ if ( ! noReloadCourse ) {
108+ document . getElementById ( "course-period-input" ) . innerHTML = "" ;
109+ courses . sort ( ( a , b ) => a . id - b . id ) . forEach ( course => {
110+ var coursePeriods = JSON . parse ( course . periods ) ;
111+ const option = document . createElement ( "option" ) ;
112+ option . value = course . id ;
113+ option . innerHTML = document . getElementById ( "course-input" ) ? course . name : `${ course . name } ${ ( coursePeriods . length > 0 ) ? ` (Period${ ( coursePeriods . length > 1 ) ? 's' : '' } ${ coursePeriods . join ( ', ' ) } )` : '' } ` ;
114+ document . getElementById ( "course-period-input" ) . appendChild ( option ) ;
115+ } ) ;
116+ }
117+ document . getElementById ( "course-period-input" ) . addEventListener ( "input" , updateCourses ) ;
118+ document . getElementById ( "course-period-input" ) . addEventListener ( "change" , updateResponses ) ;
119+ document . getElementById ( "filter-segment-input" ) . addEventListener ( "change" , updateResponses ) ;
120+ document . getElementById ( "sort-question-input" ) . addEventListener ( "input" , updateResponses ) ;
121+ document . getElementById ( "sort-seat-input" ) . addEventListener ( "input" , updateResponses ) ;
122+ if ( document . getElementById ( "filter-segment-input" ) ) updateCourses ( ) ;
123+ if ( responses . find ( response => String ( response . seatCode ) . includes ( 'xx' ) ) ) document . getElementById ( "checker" ) . classList . add ( "anonymous" ) ;
124+ if ( ! noReloadCourse ) document . getElementById ( "course-period-input" ) . value = ( storage . get ( 'period' ) && courses . find ( c => String ( c . id ) === String ( storage . get ( 'period' ) ) ) ) ? storage . get ( 'period' ) : ( ( ( ui . defaultCourse !== null ) && courses . find ( c => String ( c . id ) === String ( ui . defaultCourse ) ) ) ? ui . defaultCourse : courses . find ( c => JSON . parse ( c . periods ) . includes ( Number ( String ( responses . sort ( ( a , b ) => String ( a . seatCode ) [ 0 ] - String ( b . seatCode ) [ 0 ] ) [ 0 ] ?. seatCode ) [ 0 ] ) ) ) ? courses . find ( c => JSON . parse ( c . periods ) . includes ( Number ( String ( responses . sort ( ( a , b ) => String ( a . seatCode ) [ 0 ] - String ( b . seatCode ) [ 0 ] ) [ 0 ] ?. seatCode ) [ 0 ] ) ) ) . id : 0 ) ;
125+ await updateResponses ( ) ;
126+ active = true ;
127+ ui . stopLoader ( ) ;
128+ ui . toast ( "Data restored." , 1000 , "info" , "bi bi-cloud-arrow-down" ) ;
129+ document . getElementById ( "filter-segment-input" ) . addEventListener ( "change" , ( ) => {
130+ document . getElementById ( "sort-question-input" ) . value = "" ;
131+ const event = new Event ( 'input' , { bubbles : true } ) ;
132+ document . getElementById ( "sort-question-input" ) . dispatchEvent ( event ) ;
133+ } ) ;
134+ ui . reloadUnsavedInputs ( ) ;
160135 }
161136
162137 init ( ) ;
0 commit comments