1- function getTrainingsTable ( trainingScores , isPassedBack ) {
2- const trainingsTable = { } ;
3-
4- Object . keys ( trainingScores ) . forEach ( trainingId => {
5- if ( trainingsTable [ trainingId ] === undefined ) {
6- trainingsTable [ trainingId ] = { } ;
7- }
8- trainingsTable [ trainingId ] . score = trainingScores [ trainingId ] ;
9- } ) ;
10-
11- Object . keys ( isPassedBack ) . forEach ( trainingId => {
12- if ( trainingsTable [ trainingId ] === undefined ) {
13- trainingsTable [ trainingId ] = { } ;
14- }
15- trainingsTable [ trainingId ] . passedBackStatus = isPassedBack [ trainingId ] ;
16- } ) ;
17-
18- return trainingsTable ;
1+ const tableColomnHeaders = [
2+ "№" , "training_id" , "start_timestamp" , "training_status" , "pass_back_status" , "score"
3+ ] ;
4+
5+ function get_time_string ( timestampStr ) {
6+ const timestamp = Date . parse ( timestampStr ) ;
7+
8+ options = { weekday : 'short' , year : 'numeric' , month : 'long' , day : 'numeric' , hour : 'numeric' , minute : '2-digit' , second : '2-digit' , timeZoneName : 'short' }
9+ if ( ! isNaN ( timestamp ) ) {
10+ processing_time = new Date ( timestamp ) ;
11+ return processing_time . toLocaleString ( "ru-RU" , options ) ;
12+ } else {
13+ return "" ;
14+ }
1915}
2016
2117function createTableHeaderElement ( ) {
22- const trainingIdHeaderElement = document . createElement ( "th" ) ;
23- trainingIdHeaderElement . innerHTML = "training_id" ;
24-
25- const trainingScoreHeaderElement = document . createElement ( "th" ) ;
26- trainingScoreHeaderElement . innerHTML = "score" ;
27-
28- const trainingStatusHeaderElement = document . createElement ( "th" ) ;
29- trainingStatusHeaderElement . innerHTML = "pass_back_status" ;
30-
3118 const tableHeaderElement = document . createElement ( "tr" ) ;
3219
33- tableHeaderElement . append ( trainingIdHeaderElement , trainingScoreHeaderElement , trainingStatusHeaderElement ) ;
20+ tableColomnHeaders . forEach ( colomnHeader => {
21+ const tableColomnHeaderElement = document . createElement ( "th" ) ;
22+ tableColomnHeaderElement . innerHTML = colomnHeader ;
23+
24+ tableHeaderElement . appendChild ( tableColomnHeaderElement ) ;
25+ } ) ;
3426
3527 return tableHeaderElement ;
3628}
3729
38- function createTableRowElement ( trainingInfo , trainingId ) {
30+ function createTableRowElement ( ) {
3931 const tableRowElement = document . createElement ( "tr" ) ;
4032
41- const tableRowIdElement = document . createElement ( "td" ) ;
42- tableRowIdElement . innerHTML = `<a href="/trainings/statistics/${ trainingId } /">${ trainingId } </a>` ;
33+ const tableRow = { } ;
34+
35+ tableColomnHeaders . forEach ( columnHeader => {
36+ const tableCellElement = document . createElement ( "td" ) ;
37+
38+ tableRow [ columnHeader ] = tableCellElement ;
39+ tableRowElement . appendChild ( tableCellElement ) ;
40+ } ) ;
4341
44- const tableRowScoreElement = document . createElement ( "td" ) ;
45- tableRowScoreElement . innerHTML = trainingInfo . score ? trainingInfo . score . toFixed ( 2 ) : "none" ;
42+ return [ tableRowElement , tableRow ] ;
43+ }
4644
47- const tableRowStatusElement = document . createElement ( "td" ) ;
48- tableRowStatusElement . innerHTML = trainingInfo . passedBackStatus || "none" ;
45+ function TableRowFiller ( ) {
46+ let pos = 1 ;
4947
50- tableRowElement . append ( tableRowIdElement , tableRowScoreElement , tableRowStatusElement ) ;
48+ function fillTableRow ( tableRow , training ) {
49+ tableRow [ "№" ] . innerHTML = pos ++ ;
50+ tableRow [ "training_id" ] . innerHTML = `<a href="/trainings/statistics/${ training . id } /">${ training . id } </a>` ;
51+ tableRow [ "start_timestamp" ] . innerHTML = get_time_string ( training . training_start_timestamp ) ;
52+ tableRow [ "score" ] . innerHTML = training . score ? training . score . toFixed ( 2 ) : "none" ;
53+ tableRow [ "training_status" ] . innerHTML = training . training_status ;
54+ tableRow [ "pass_back_status" ] . innerHTML = training . passedBackStatus || "none" ;
55+ }
5156
52- return tableRowElement ;
57+ return fillTableRow ;
5358}
5459
55- function showRelatedTrainingsTable ( trainingScores , isPassedBack ) {
56- const trainingsTable = getTrainingsTable ( trainingScores , isPassedBack ) ;
60+ function getSortedTrainingsList ( trainings ) {
61+ const trainingsList = [ ] ;
62+
63+ Object . keys ( trainings ) . forEach ( trainingId => {
64+ const training = trainings [ trainingId ] ;
65+ training [ "id" ] = trainingId ;
66+
67+ trainingsList . push ( training ) ;
68+ } ) ;
69+
70+ trainingsList . sort ( ( a , b ) => {
71+ return Date . parse ( a ) - Date . parse ( b ) ;
72+ } ) ;
73+
74+ return trainingsList ;
75+ }
5776
77+ function showRelatedTrainingsTable ( trainings ) {
5878 const tableElement = document . getElementById ( "related-trainings-table" ) ;
5979
6080 tableElement . appendChild ( createTableHeaderElement ( ) ) ;
6181
62- Object . keys ( trainingsTable ) . forEach ( trainingId => {
63- tableElement . appendChild ( createTableRowElement ( trainingsTable [ trainingId ] , trainingId ) ) ;
82+ const trainingsList = getSortedTrainingsList ( trainings ) ;
83+
84+ const fillTableRow = TableRowFiller ( ) ;
85+
86+ trainingsList . forEach ( training => {
87+ const [ tableRowElement , tableRow ] = createTableRowElement ( ) ;
88+
89+ fillTableRow ( tableRow , training ) ;
90+
91+ tableElement . appendChild ( tableRowElement ) ;
6492 } ) ;
6593}
0 commit comments