Skip to content

Commit

Permalink
implement filter on entry type
Browse files Browse the repository at this point in the history
  • Loading branch information
madsrasmussen committed Dec 16, 2024
1 parent 6365c2f commit 892f44c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export class UmbClipboardEntryPickerModalElement extends UmbModalBaseElement<
}

override async firstUpdated() {
const { data } = await this.#collectionRepository.requestCollection({});
const entryType = this.data?.entry?.type;

const { data } = await this.#collectionRepository.requestCollection({ entry: { type: entryType } });
this._items = data?.items ?? [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import type { UmbClipboardEntryDetailModel } from '../types.js';
import { UMB_CLIPBOARD_ENTRY_PICKER_MODAL_ALIAS } from './constants.js';
import { UmbModalToken, type UmbPickerModalData, type UmbPickerModalValue } from '@umbraco-cms/backoffice/modal';

export interface UmbClipboardEntryPickerModalData extends UmbPickerModalData<UmbClipboardEntryDetailModel> {}
export interface UmbClipboardEntryPickerModalData extends UmbPickerModalData<UmbClipboardEntryDetailModel> {
entry: {
type: string;
};
}

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface UmbClipboardEntryPickerModalValue extends UmbPickerModalValue {}

export const UMB_CLIPBOARD_ENTRY_PICKER_MODAL = new UmbModalToken<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import type { UmbClipboardEntryDetailModel } from './clipboard-entry/index.js';

const UMB_CLIPBOARD_LOCALSTORAGE_KEY = 'umb:clipboard';

interface UmbClipboardLocalStorageFilterModel {
entry?: {
type?: string;
};
skip?: number;
take?: number;
}

// keep internal
export class UmbClipboardLocalStorageManager {
// Gets all entries from local storage
Expand Down Expand Up @@ -29,4 +37,24 @@ export class UmbClipboardLocalStorageManager {
setEntries(entries: Array<UmbClipboardEntryDetailModel>) {
localStorage.setItem(UMB_CLIPBOARD_LOCALSTORAGE_KEY, JSON.stringify(entries));
}

// gets a filtered list of entries
filter(filter: UmbClipboardLocalStorageFilterModel) {
const { entries } = this.getEntries();
const filteredEntries = this.#filterEntries(entries, filter);
const total = filteredEntries.length;
const skip = filter.skip || 0;
const take = filter.take || total;
const pagedEntries = filteredEntries.slice(skip, skip + take);
return { entries: pagedEntries, total };
}

#filterEntries(entries: Array<UmbClipboardEntryDetailModel>, filter: UmbClipboardLocalStorageFilterModel) {
return entries.filter((entry) => {
if (filter.entry?.type && entry.type !== filter.entry.type) {
return false;
}
return true;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class UmbClipboardCollectionLocalStorageDataSource
#localStorageManager = new UmbClipboardLocalStorageManager();

async getCollection(filter: UmbClipboardCollectionFilterModel) {
const { entries, total } = this.#localStorageManager.getEntries();
const { entries, total } = this.#localStorageManager.filter(filter);
return { data: { items: entries, total } };
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export interface UmbClipboardCollectionFilterModel {
entry?: {
type?: string;
};
skip?: number;
take?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { UmbPropertyActionBase, type UmbPropertyActionArgs } from '@umbraco-cms/
import { UMB_MODAL_MANAGER_CONTEXT } from '@umbraco-cms/backoffice/modal';

export class UmbColorPickerPasteFromClipboardPropertyAction extends UmbPropertyActionBase<MetaPropertyActionPasteFromClipboardKind> {
#detailRepository = new UmbClipboardEntryDetailRepository(this);
#init: Promise<unknown>;
#entryType: string;
#propertyContext?: typeof UMB_PROPERTY_CONTEXT.TYPE;
#modalManagerContext?: typeof UMB_MODAL_MANAGER_CONTEXT.TYPE;
#clipboardEntryDetailRepository = new UmbClipboardEntryDetailRepository(this);
#init?: Promise<unknown>;
#entryType?: string;

constructor(host: UmbControllerHost, args: UmbPropertyActionArgs<MetaPropertyActionPasteFromClipboardKind>) {
super(host, args);
Expand All @@ -19,6 +19,8 @@ export class UmbColorPickerPasteFromClipboardPropertyAction extends UmbPropertyA
throw new Error('The "entry.type" meta property is required');
}

this.#entryType = args.meta.entry.type;

this.#init = Promise.all([
this.consumeContext(UMB_PROPERTY_CONTEXT, (context) => {
this.#propertyContext = context;
Expand All @@ -33,22 +35,26 @@ export class UmbColorPickerPasteFromClipboardPropertyAction extends UmbPropertyA
override async execute() {
await this.#init;

try {
const modalContext = this.#modalManagerContext?.open(this, UMB_CLIPBOARD_ENTRY_PICKER_MODAL);
const value = await modalContext?.onSubmit();
const clipboardEntryUnique = value?.selection?.[0];
console.log(this.#entryType);
if (clipboardEntryUnique) {
const { data: clipboardEntry, error } =
await this.#clipboardEntryDetailRepository.requestByUnique(clipboardEntryUnique);
const modalContext = this.#modalManagerContext?.open(this, UMB_CLIPBOARD_ENTRY_PICKER_MODAL, {
data: {
entry: {
type: this.#entryType,
},
},
});

const value = await modalContext?.onSubmit();
const clipboardEntryUnique = value?.selection?.[0];

const clipboardEntryData = clipboardEntry?.data[0];
if (clipboardEntryUnique) {
const { data: entry } = await this.#detailRepository.requestByUnique(clipboardEntryUnique);

if (clipboardEntryData) {
this.#propertyContext?.setValue(clipboardEntryData);
}
const entryValue = entry?.data[0];

if (entryValue) {
this.#propertyContext?.setValue(entryValue);
}
} catch (error) {}
}
}

Check warning on line 58 in src/Umbraco.Web.UI.Client/src/packages/core/clipboard/property-actions/paste/paste-from-clipboard.property-action.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

❌ New issue: Complex Method

UmbColorPickerPasteFromClipboardPropertyAction.execute has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
}
export { UmbColorPickerPasteFromClipboardPropertyAction as api };

0 comments on commit 892f44c

Please sign in to comment.