1212 */
1313import { Observable , RemoteData } from '/js/src/index.js' ;
1414import { jsonFetch } from '../../../utilities/fetch/jsonFetch.js' ;
15- import { tagToOption } from '../../../components/tag/tagToOption.js' ;
16- import { getRemoteData } from '../../../utilities/fetch/getRemoteData.js' ;
1715import { TagPickerModel } from '../../../components/tag/TagPickerModel.js' ;
1816
1917const DATA_DISPLAY_DELIMITER = ',' ;
2018
19+ /**
20+ * @typedef LogCreationRelations
21+ * @property {number[] } [runNumbers] run numbers associated with log being created
22+ * @property {number[] } [lhcFillNumbers] lhc fill numbers associated with log being created
23+ * @property {number[] } [environmentIds] environment ids associated with log being created
24+ */
25+
2126/**
2227 * Model to cary log creation state
2328 */
@@ -26,8 +31,9 @@ export class LogCreationModel extends Observable {
2631 * Constructor
2732 *
2833 * @param {function } [onCreation] function called when log is created, with the id of the created log
34+ * @param {LogCreationRelations } relations the relations of the log
2935 */
30- constructor ( onCreation ) {
36+ constructor ( onCreation , relations ) {
3137 super ( ) ;
3238
3339 this . _onCreation = onCreation ;
@@ -38,47 +44,20 @@ export class LogCreationModel extends Observable {
3844 this . _creationTagsPickerModel . bubbleTo ( this ) ;
3945 this . _creationTagsPickerModel . visualChange$ . bubbleTo ( this ) ;
4046
41- this . _parentLogId = null ;
47+ const { runNumbers = [ ] , environmentIds = [ ] , lhcFillNumbers = [ ] } = relations ;
48+ this . _runNumbers = runNumbers . join ( DATA_DISPLAY_DELIMITER ) ;
49+ this . _lhcFills = lhcFillNumbers . join ( DATA_DISPLAY_DELIMITER ) ;
50+ this . _environments = environmentIds . join ( DATA_DISPLAY_DELIMITER ) ;
51+ this . _isRunNumbersReadonly = Boolean ( this . _runNumbers ) ;
4252
43- this . _isParentLogCollapsed = true ;
53+ this . _parentLogId = null ;
4454
4555 this . _attachments = [ ] ;
4656
4757 this . _createdLog = RemoteData . notAsked ( ) ;
4858 this . _isRunNumbersReadonly = false ;
4959 }
5060
51- /**
52- * Provide initial data for creation
53- * @param {Object } [data] data
54- * @param {number } [data.parentLogId] id of parent log
55- * @param {number[] } [data.runNumbers] run numbers associated with log being created
56- * @param {number[] } [data.lhcFillNumbers] lhc fill numbers associated with log being created
57- * @param {number[] } [data.environmentIds] environment ids associated with log being created
58- * @return {void }
59- */
60- setLogCreationInitialData ( { parentLogId, runNumbers, lhcFillNumbers, environmentIds } ) {
61- this . _runNumbers = runNumbers . join ( DATA_DISPLAY_DELIMITER ) ;
62- this . _lhcFills = lhcFillNumbers . join ( DATA_DISPLAY_DELIMITER ) ;
63- this . _environments = environmentIds . join ( DATA_DISPLAY_DELIMITER ) ;
64- this . _isRunNumbersReadonly = Boolean ( this . _runNumbers ) ;
65-
66- if ( parentLogId ) {
67- this . _parentLogId = Number ( parentLogId ) ;
68- this . _parentLog = RemoteData . loading ( ) ;
69- getRemoteData ( `/api/logs/${ this . _parentLogId } ` ) . then (
70- ( { data : parentLog } ) => {
71- this . parentLog = RemoteData . success ( parentLog ) ;
72- } ,
73- ( errors ) => {
74- this . parentLog = RemoteData . failure ( errors ) ;
75- } ,
76- ) ;
77- } else {
78- this . _parentLog = RemoteData . success ( null ) ;
79- }
80- }
81-
8261 /**
8362 * Returns whether parent log is collapsed
8463 *
@@ -103,10 +82,6 @@ export class LogCreationModel extends Observable {
10382 * @returns {undefined }
10483 */
10584 async submit ( ) {
106- if ( ! this . isReady ) {
107- throw new Error ( 'Log creation is not ready, please wait' ) ;
108- }
109-
11085 if ( ! this . isValid ) {
11186 throw new Error ( 'Created log is not valid' ) ;
11287 }
@@ -126,7 +101,7 @@ export class LogCreationModel extends Observable {
126101 const body = {
127102 title,
128103 text,
129- ...parentLogId > 0 && { parentLogId } ,
104+ ...parentLogId !== null && { parentLogId } ,
130105 ...runNumbers && { runNumbers } ,
131106 } ;
132107
@@ -283,38 +258,6 @@ export class LogCreationModel extends Observable {
283258 return this . _parentLogId ;
284259 }
285260
286- /**
287- * Returns the current parent log as a remote data
288- * If the log do not have a parent log, the value will be success(null)
289- *
290- * @return {RemoteData } the parent log
291- */
292- get parentLog ( ) {
293- return this . _parentLog ;
294- }
295-
296- /**
297- * Set the parent log and use its values to define default values for the created log
298- *
299- * @param {RemoteData } parentLog the new parent log as a remote data
300- */
301- set parentLog ( parentLog ) {
302- this . _parentLog = parentLog ;
303- if ( parentLog . isSuccess ( ) ) {
304- const { title, tags, runs } = parentLog . payload ;
305-
306- this . title = `${ title } ` ;
307-
308- if ( this . tagsPickerModel . hasOnlyDefaultSelection ( ) ) {
309- this . tagsPickerModel . selectedOptions = tags . map ( tagToOption ) ;
310- }
311- if ( ! this . runNumbers ) {
312- this . runNumbers = runs . map ( ( { runNumber } ) => runNumber ) . join ( ', ' ) ;
313- }
314- }
315- this . notify ( ) ;
316- }
317-
318261 /**
319262 * Returns the list of files attachments linked to the log being created
320263 *
@@ -343,15 +286,6 @@ export class LogCreationModel extends Observable {
343286 return this . _createdLog ;
344287 }
345288
346- /**
347- * States if the log creation model is ready
348- *
349- * @return {boolean } true if the model is ready
350- */
351- get isReady ( ) {
352- return this . _parentLog . isSuccess ( ) ;
353- }
354-
355289 /**
356290 * States if the currently created log is valid
357291 *
0 commit comments