@@ -8,7 +8,7 @@ function get_time_string(timestamp){
88 }
99}
1010
11- function buildCurrentTrainingRow ( trainingId , trainingJson , is_Admin = false ) {
11+ function buildCurrentTrainingRow ( trainingId , trainingJson , isAdmin = false ) {
1212
1313 const currentTrainingRowElement = document . createElement ( "tr" ) ;
1414
@@ -102,7 +102,7 @@ function buildCurrentTrainingRow(trainingId, trainingJson, is_Admin=false) {
102102 }
103103 currentTrainingRowElement . appendChild ( recordingElement ) ;
104104
105- if ( is_Admin ) {
105+ if ( isAdmin ) {
106106 const recheckingElement = document . createElement ( "td" ) ;
107107 const button = document . createElement ( "input" )
108108 button . type = "button"
@@ -112,22 +112,11 @@ function buildCurrentTrainingRow(trainingId, trainingJson, is_Admin=false) {
112112 recheckingElement . appendChild ( button ) ;
113113 currentTrainingRowElement . appendChild ( recheckingElement ) ;
114114 }
115- return currentTrainingRowElement ;
116- }
117115
118- function recheck ( trainingId ) {
119- console . log ( `Start recheck for ${ trainingId } ` )
120- fetch ( `/api/trainings/${ trainingId } /` , { method : "POST" } )
121- . then ( response => response . json ( ) )
122- . then ( innerResponseJson => {
123- if ( innerResponseJson [ "message" ] === "OK" ) {
124- window . open ( `/trainings/statistics/${ trainingId } /` ) ;
125- //location.href = `/trainings/statistics/${trainingId}/`;
126- }
127- } ) ;
116+ return currentTrainingRowElement ;
128117}
129118
130- function buildAllTrainingsTable ( trainingsJson , is_Admin = false ) {
119+ function buildAllTrainingsTable ( trainingsJson ) {
131120 if ( trainingsJson [ "message" ] !== "OK" ) return ;
132121
133122 const allTrainingsTable = document . getElementById ( "all-trainings-table" ) ;
@@ -148,16 +137,31 @@ function buildAllTrainingsTable(trainingsJson, is_Admin=false) {
148137 "Презентация" ,
149138 "Аудиозапись"
150139 ]
151- if ( is_Admin ) { titles . push ( 'Запустить перепроверку' ) }
152- const titleRow = buildTitleRow ( titles ) ;
153- allTrainingsTable . appendChild ( titleRow ) ;
154140
155- const arrayTrainings = trainingsJson . trainings ; // Array of train from FETCH
141+ fetch ( '/api/sessions/admin' )
142+ . then ( response => response . json ( ) )
143+ . then ( responseJson => {
144+ const isAdmin = responseJson . admin ;
145+ const arrayTrainings = trainingsJson . trainings ; // Array of train from FETCH
146+
147+ if ( isAdmin ) {
148+ titles . push ( 'Запустить перепроверку' ) ;
149+ }
156150
157- Object . keys ( arrayTrainings ) . forEach ( trainingId => {
158- const currentTrainingRowElement = buildCurrentTrainingRow ( trainingId , arrayTrainings [ trainingId ] , is_Admin ) ;
159- allTrainingsTable . appendChild ( currentTrainingRowElement ) ;
160- } ) ;
151+ const titleRow = buildTitleRow ( titles ) ;
152+ allTrainingsTable . appendChild ( titleRow ) ;
153+
154+ Object . keys ( arrayTrainings ) . forEach ( trainingId => {
155+ const currentTrainingRowElement = buildCurrentTrainingRow (
156+ trainingId ,
157+ arrayTrainings [ trainingId ] ,
158+ isAdmin
159+ ) ;
160+
161+ allTrainingsTable . appendChild ( currentTrainingRowElement ) ;
162+ } ) ;
163+ } )
164+ . catch ( err => console . log ( err ) ) ;
161165}
162166
163167const REF_PAGE_COUNT = document . getElementById ( 'ref-page-count' ) ;
@@ -262,7 +266,7 @@ async function updateCountPage() {
262266 REF_BUTTON_TO_END . innerText = pageTotal ;
263267}
264268
265- function call_get_all_trainings ( { filters, admin = false , page = 0 , count} ) {
269+ function call_get_all_trainings ( { filters, page = 0 , count} ) {
266270 const query = new URLSearchParams ( {
267271 filters,
268272 count
@@ -272,5 +276,5 @@ function call_get_all_trainings({filters, admin=false, page = 0, count}) {
272276
273277 return fetch ( `/api/trainings?${ query . toString ( ) } ` )
274278 . then ( response => response . json ( ) )
275- . then ( responseJson => buildAllTrainingsTable ( responseJson , admin ) ) ;
279+ . then ( responseJson => buildAllTrainingsTable ( responseJson ) ) ;
276280}
0 commit comments