From f4f4dea6014275faf7d6e50b6cf8c7dbdad449aa Mon Sep 17 00:00:00 2001 From: Aleksandr Grenishin Date: Fri, 6 Oct 2023 15:06:04 +0300 Subject: [PATCH] fix(init-flow): fix sync `initFlow` calls right after upload collection (#532) --- abstract/ActivityBlock.js | 2 +- abstract/Block.js | 12 +++++++----- abstract/UploaderBlock.js | 12 ++++++------ blocks/DropArea/DropArea.js | 2 +- blocks/Modal/Modal.js | 2 +- blocks/UploadList/UploadList.js | 12 ++++++++---- 6 files changed, 24 insertions(+), 18 deletions(-) diff --git a/abstract/ActivityBlock.js b/abstract/ActivityBlock.js index 66cf8a0eb..24ef37903 100644 --- a/abstract/ActivityBlock.js +++ b/abstract/ActivityBlock.js @@ -161,7 +161,7 @@ export class ActivityBlock extends Block { this.$['*currentActivity'] = nextActivity; this.$['*history'] = history; if (!nextActivity) { - this.setForCtxTarget(Modal.StateConsumerScope, '*modalActive', false); + this.setOrAddState('*modalActive', false); } } } diff --git a/abstract/Block.js b/abstract/Block.js index 1bb339369..c475ffaa3 100644 --- a/abstract/Block.js +++ b/abstract/Block.js @@ -102,14 +102,16 @@ export class Block extends BaseComponent { } /** - * @param {String} consumerScope * @param {String} prop * @param {any} newVal */ - setForCtxTarget(consumerScope, prop, newVal) { - if (this.hasBlockInCtx((b) => /** @type {typeof Block} */ (b.constructor).StateConsumerScope === consumerScope)) { - this.$[prop] = newVal; - } + setOrAddState(prop, newVal) { + this.add$( + { + [prop]: newVal, + }, + true + ); } /** @param {String} activityType */ diff --git a/abstract/UploaderBlock.js b/abstract/UploaderBlock.js index 8810d3f97..26ff97e65 100644 --- a/abstract/UploaderBlock.js +++ b/abstract/UploaderBlock.js @@ -229,7 +229,7 @@ export class UploaderBlock extends ActivityBlock { [...this.fileInput['files']].forEach((file) => this.addFileFromObject(file, { source: UploadSource.LOCAL })); // To call uploadTrigger UploadList should draw file items first: this.$['*currentActivity'] = ActivityBlock.activities.UPLOAD_LIST; - this.setForCtxTarget(Modal.StateConsumerScope, '*modalActive', true); + this.setOrAddState('*modalActive', true); // @ts-ignore TODO: fix this this.fileInput['value'] = ''; this.fileInput = null; @@ -249,11 +249,11 @@ export class UploaderBlock extends ActivityBlock { /** @param {Boolean} [force] */ initFlow(force = false) { - if (this.$['*uploadList']?.length && !force) { + if (this.uploadCollection.size > 0 && !force) { this.set$({ '*currentActivity': ActivityBlock.activities.UPLOAD_LIST, }); - this.setForCtxTarget(Modal.StateConsumerScope, '*modalActive', true); + this.setOrAddState('*modalActive', true); } else { if (this.sourceList?.length === 1) { let srcKey = this.sourceList[0]; @@ -272,14 +272,14 @@ export class UploaderBlock extends ActivityBlock { } else { this.$['*currentActivity'] = srcKey; } - this.setForCtxTarget(Modal.StateConsumerScope, '*modalActive', true); + this.setOrAddState('*modalActive', true); } } else { // Multiple sources case: this.set$({ '*currentActivity': ActivityBlock.activities.START_FROM, }); - this.setForCtxTarget(Modal.StateConsumerScope, '*modalActive', true); + this.setOrAddState('*modalActive', true); } } EventManager.emit( @@ -298,7 +298,7 @@ export class UploaderBlock extends ActivityBlock { '*history': this.doneActivity ? [this.doneActivity] : [], }); if (!this.$['*currentActivity']) { - this.setForCtxTarget(Modal.StateConsumerScope, '*modalActive', false); + this.setOrAddState('*modalActive', false); } EventManager.emit( new EventData({ diff --git a/blocks/DropArea/DropArea.js b/blocks/DropArea/DropArea.js index dcc57d4da..a6e509fa8 100644 --- a/blocks/DropArea/DropArea.js +++ b/blocks/DropArea/DropArea.js @@ -89,7 +89,7 @@ export class DropArea extends UploaderBlock { this.set$({ '*currentActivity': ActivityBlock.activities.UPLOAD_LIST, }); - this.setForCtxTarget(Modal.StateConsumerScope, '*modalActive', true); + this.setOrAddState('*modalActive', true); } }, }); diff --git a/blocks/Modal/Modal.js b/blocks/Modal/Modal.js index 81d7f173e..c8ff20134 100644 --- a/blocks/Modal/Modal.js +++ b/blocks/Modal/Modal.js @@ -19,7 +19,7 @@ export class Modal extends Block { }; _closeDialog = () => { - this.setForCtxTarget(Modal.StateConsumerScope, '*modalActive', false); + this.setOrAddState('*modalActive', false); }; _handleDialogClose = () => { diff --git a/blocks/UploadList/UploadList.js b/blocks/UploadList/UploadList.js index 1a9680f3a..ff4a8512f 100644 --- a/blocks/UploadList/UploadList.js +++ b/blocks/UploadList/UploadList.js @@ -65,6 +65,14 @@ export class UploadList extends UploaderBlock { } this._updateUploadsState(); this._updateCountLimitMessage(); + + if ( + this.$['*uploadList']?.length === 0 && + !this.cfg.showEmptyList && + this.$['*currentActivity'] === this.activityType + ) { + this.historyBack(); + } }, 0); /** @@ -223,10 +231,6 @@ export class UploadList extends UploaderBlock { hasFiles: list.length > 0, }); - if (list?.length === 0 && !this.cfg.showEmptyList) { - this.historyBack(); - } - if (!this.cfg.confirmUpload) { this.add$( {