Skip to content

Commit

Permalink
feat: add setModalActive and setCurrentActivity methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nd0ut committed Jun 24, 2024
1 parent 6de876f commit 60ba8a6
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 17 deletions.
3 changes: 2 additions & 1 deletion abstract/ActivityBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export class ActivityBlock extends Block {

if (!hasCurrentActivityInCtx) {
this.$['*currentActivity'] = null;
this.setOrAddState('*modalActive', false);
}
}

Expand Down Expand Up @@ -199,4 +200,4 @@ ActivityBlock.activities = Object.freeze({
DETAILS: 'details',
});

/** @typedef {(typeof ActivityBlock)['activities'][keyof (typeof ActivityBlock)['activities']] | null} ActivityType */
/** @typedef {(typeof ActivityBlock)['activities'][keyof (typeof ActivityBlock)['activities']] | (string & {}) | null} ActivityType */
9 changes: 0 additions & 9 deletions abstract/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,6 @@ export class Block extends BaseComponent {
);
}

/** @param {import('./ActivityBlock.js').ActivityType} activityType */
setActivity(activityType) {
if (this.hasBlockInCtx((b) => b.activityType === activityType)) {
this.$['*currentActivity'] = activityType;
return;
}
console.warn(`Activity type "${activityType}" not found in the context`);
}

connectedCallback() {
const styleAttrs = /** @type {typeof Block} */ (this.constructor).styleAttrs;
styleAttrs.forEach((attr) => {
Expand Down
4 changes: 2 additions & 2 deletions abstract/CTX.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ export const blockCtx = () => ({});
/** @param {import('./Block').Block} fnCtx */
export const activityBlockCtx = (fnCtx) => ({
...blockCtx(),
'*currentActivity': '',
'*currentActivity': null,
'*currentActivityParams': {},
'*history': [],
'*historyBack': null,
'*closeModal': () => {
fnCtx.set$({
'*currentActivity': null,
'*modalActive': false,
'*currentActivity': '',
});
},
});
Expand Down
8 changes: 8 additions & 0 deletions abstract/UploaderBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ export class UploaderBlock extends ActivityBlock {
if (!this.$['*secureUploadsManager']) {
this.$['*secureUploadsManager'] = new SecureUploadsManager(this);
}

if (this.has('*modalActive')) {
this.sub('*modalActive', (modalActive) => {
if (modalActive && !this.$['*currentActivity']) {
this.$['*modalActive'] = false;
}
});
}
}

/** @type {string[]} */
Expand Down
24 changes: 24 additions & 0 deletions abstract/UploaderPublicApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,28 @@ export class UploaderPublicApi {
this._ctx.setOrAddState('*modalActive', false);
}
}

/**
* @param {import('./ActivityBlock.js').ActivityType} activityType
* @param {import('../blocks/ExternalSource/ExternalSource.js').ActivityParams | {}} [params]
*/
setCurrentActivity(activityType, params = {}) {
if (this._ctx.hasBlockInCtx((b) => b.activityType === activityType)) {
this._ctx.set$({
'*currentActivityParams': params,
'*currentActivity': activityType,
});
return;
}
console.warn(`Activity type "${activityType}" not found in the context`);
}

/** @param {boolean} active */
setModalActive(active) {
if (active && !this._ctx.$['*currentActivity']) {
console.warn(`Can't open modal without current activity. Please use "setCurrentActivity" method first.`);
return;
}
this._ctx.setOrAddState('*modalActive', active);
}
}
14 changes: 14 additions & 0 deletions blocks/ExternalSource/ExternalSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export class ExternalSource extends UploaderBlock {
onActivate: () => {
let { externalSourceType } = /** @type {ActivityParams} */ (this.activityParams);

if (!externalSourceType) {
this.$['*currentActivity'] = null;
this.setOrAddState('*modalActive', false);
console.error(`Param "externalSourceType" is required for activity "${this.activityType}"`);
return;
}

this.set$({
activityCaption: `${externalSourceType?.[0].toUpperCase()}${externalSourceType?.slice(1)}`,
activityIcon: externalSourceType,
Expand All @@ -80,6 +87,13 @@ export class ExternalSource extends UploaderBlock {
this.mountIframe();
},
});
this.sub('*currentActivityParams', (val) => {

Check warning on line 90 in blocks/ExternalSource/ExternalSource.js

View workflow job for this annotation

GitHub Actions / build

'val' is defined but never used
if (!this.isActivityActive) {
return;
}
this.unmountIframe();
this.mountIframe();
});
this.sub('*currentActivity', (val) => {
if (val !== this.activityType) {
this.unmountIframe();
Expand Down
1 change: 1 addition & 0 deletions types/exported.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { complexConfigKeys } from '../blocks/Config/Config';
import type { FuncFileValidator, FuncCollectionValidator } from '../abstract/ValidationManager';

export type { FuncFileValidator, FuncCollectionValidator } from '../abstract/ValidationManager';
export type { UploaderPublicApi } from '../abstract/UploaderPublicApi';

export type UploadError = import('@uploadcare/upload-client').UploadError;
export type UploadcareFile = import('@uploadcare/upload-client').UploadcareFile;
Expand Down
13 changes: 8 additions & 5 deletions types/test/lr-upload-ctx-provider.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import { useRef } from 'react';
import { UploadcareFile, UploadcareGroup } from '@uploadcare/upload-client';

const instance = new UploadCtxProvider();

instance.api.addFileFromUrl('https://example.com/image.png');
instance.uploadCollection.size;
instance.setOrAddState('fileId', 'uploading');

const api = instance.api;
api.addFileFromUrl('https://example.com/image.png');

instance.addEventListener('change', (e) => {
expectType<EventMap['change']>(e);
});
Expand All @@ -42,7 +43,7 @@ instance.addEventListener('change', (e) => {
expectType<false>(state.isFailed);
expectType<false>(state.isUploading);
expectType<[]>(state.errors);
expectType<'success'>(state.allEntries[0].status)
expectType<'success'>(state.allEntries[0].status);
} else if (state.isFailed) {
expectType<'failed'>(state.status);
expectType<false>(state.isSuccess);
Expand All @@ -61,7 +62,7 @@ instance.addEventListener('change', (e) => {
expectType<false>(state.isFailed);
expectType<false>(state.isUploading);
expectType<[]>(state.errors);
expectType<'success' | 'idle'>(state.allEntries[0].status)
expectType<'success' | 'idle'>(state.allEntries[0].status);
}
});

Expand Down Expand Up @@ -204,7 +205,9 @@ instance.addEventListener('modal-open', (e) => {

instance.addEventListener('activity-change', (e) => {
const payload = e.detail;
expectType<(typeof ActivityBlock)['activities'][keyof (typeof ActivityBlock)['activities']] | null>(payload.activity);
expectType<(typeof ActivityBlock)['activities'][keyof (typeof ActivityBlock)['activities']] | null | (string & {})>(
payload.activity,
);
});

() => {
Expand Down

0 comments on commit 60ba8a6

Please sign in to comment.