From bba7b9b7850d7ac509d5eb14b680e2ca60e54570 Mon Sep 17 00:00:00 2001 From: Julia Gru <56249914+julczka@users.noreply.github.com> Date: Tue, 7 Feb 2023 14:28:44 +0100 Subject: [PATCH] create repository, store and source I have no clue what i'm doing lol --- .../libs/observable-api/array-state.ts | 1 - .../workspace/data/log-search.repository.ts | 79 +++++++++++++++++++ .../workspace/data/log-search.store.ts | 47 +++++++++++ .../logviewer/workspace/data/sources/index.ts | 11 +++ .../data/sources/log-search.server.data.ts | 33 ++++++++ .../logviewer-root-workspace.element.ts | 3 +- src/Umbraco.Web.UI.Client/vite.config.ts | 3 + 7 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/log-search.repository.ts create mode 100644 src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/log-search.store.ts create mode 100644 src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/sources/index.ts create mode 100644 src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/sources/log-search.server.data.ts diff --git a/src/Umbraco.Web.UI.Client/libs/observable-api/array-state.ts b/src/Umbraco.Web.UI.Client/libs/observable-api/array-state.ts index 303e3b3874..f8b46880ba 100644 --- a/src/Umbraco.Web.UI.Client/libs/observable-api/array-state.ts +++ b/src/Umbraco.Web.UI.Client/libs/observable-api/array-state.ts @@ -10,7 +10,6 @@ import { pushToUniqueArray } from "./push-to-unique-array.method"; * * The ArrayState provides methods to append data when the data is an Object. */ - export class ArrayState extends DeepState { diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/log-search.repository.ts b/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/log-search.repository.ts new file mode 100644 index 0000000000..45d8997c7c --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/log-search.repository.ts @@ -0,0 +1,79 @@ +import { UmbLogSearchesServerDataSource } from './sources/log-search.server.data'; +import { UmbLogSearchesStore, UMB_LOG_SEARCHES_STORE_CONTEXT_TOKEN } from './log-search.store'; +import { ProblemDetails, Template } from '@umbraco-cms/backend-api'; +import { UmbContextConsumerController } from '@umbraco-cms/context-api'; +import { UmbControllerHostInterface } from '@umbraco-cms/controller'; +import { UmbNotificationService, UMB_NOTIFICATION_SERVICE_CONTEXT_TOKEN } from '@umbraco-cms/notification'; + +// Move to documentation / JSdoc +/* We need to create a new instance of the repository from within the element context. We want the notifications to be displayed in the right context. */ +// element -> context -> repository -> (store) -> data source +// All methods should be async and return a promise. Some methods might return an observable as part of the promise response. +export class UmbLogSearchRepository { + #host: UmbControllerHostInterface; + #dataSource: UmbLogSearchesServerDataSource; + #searchStore?: UmbLogSearchesStore; + #notificationService?: UmbNotificationService; + #initResolver?: () => void; + #initialized = false; + + constructor(host: UmbControllerHostInterface) { + this.#host = host; + this.#dataSource = new UmbLogSearchesServerDataSource(this.#host); + + new UmbContextConsumerController(this.#host, UMB_LOG_SEARCHES_STORE_CONTEXT_TOKEN, (instance) => { + this.#searchStore = instance; + this.#checkIfInitialized(); + }); + + new UmbContextConsumerController(this.#host, UMB_NOTIFICATION_SERVICE_CONTEXT_TOKEN, (instance) => { + this.#notificationService = instance; + this.#checkIfInitialized(); + }); + } + + #init() { + // TODO: This would only works with one user of this method. If two, the first one would be forgotten, but maybe its alright for now as I guess this is temporary. + return new Promise((resolve) => { + this.#initialized ? resolve() : (this.#initResolver = resolve); + }); + } + + #checkIfInitialized() { + if (this.#searchStore && this.#notificationService) { + this.#initialized = true; + this.#initResolver?.(); + } + } + + async getSavedSearches() { + await this.#init(); + + return this.#dataSource.getLogViewerSavedSearch(); + } + + // async insert(template: Template) { + // await this.#init(); + + // // TODO: should we show a notification if the template is missing? + // // Investigate what is best for Acceptance testing, cause in that perspective a thrown error might be the best choice? + // if (!template) { + // const error: ProblemDetails = { title: 'Template is missing' }; + // return { error }; + // } + + // const { error } = await this.#dataSource.insert(template); + + // if (!error) { + // const notification = { data: { message: `Template created` } }; + // this.#notificationService?.peek('positive', notification); + // } + + // // TODO: we currently don't use the detail store for anything. + // // Consider to look up the data before fetching from the server + // this.#searchStore?.append(template); + // // TODO: Update tree store with the new item? + + // return { error }; + // } +} diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/log-search.store.ts b/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/log-search.store.ts new file mode 100644 index 0000000000..990ec2de4f --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/log-search.store.ts @@ -0,0 +1,47 @@ +import { UmbContextToken } from '@umbraco-cms/context-api'; +import { ArrayState } from '@umbraco-cms/observable-api'; +import { UmbStoreBase } from '@umbraco-cms/store'; +import { SavedLogSearch } from '@umbraco-cms/backend-api'; +import { UmbControllerHostInterface } from '@umbraco-cms/controller'; + +/** + * @export + * @class UmbLogSearchesStore + * @extends {UmbStoreBase} + * @description - Data Store for log searches + */ +export class UmbLogSearchesStore extends UmbStoreBase { + #data = new ArrayState([], (x) => x.name); + + /** + * Creates an instance of UmbLogSearchesStore. + * @param {UmbControllerHostInterface} host + * @memberof UmbLogSearchesStore + */ + constructor(host: UmbControllerHostInterface) { + super(host, UmbLogSearchesStore.name); + } + + /** + * Append a template to the store + * @param {SavedLogSearch} search + * @memberof UmbLogSearchesStore + */ + append(search: SavedLogSearch) { + this.#data.append([search]); + } + + /** + * Removes templates in the store with the given uniques + * @param {string[]} searchNames + * @memberof UmbLogSearchesStore + */ + remove(searchNames: string[]) { + this.#data.remove(searchNames); + } +} + +export const UMB_LOG_SEARCHES_STORE_CONTEXT_TOKEN = new UmbContextToken( + UmbLogSearchesStore.name, + 'LogSearchesStore TOKEN XXX' +); diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/sources/index.ts b/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/sources/index.ts new file mode 100644 index 0000000000..6c42bd1510 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/sources/index.ts @@ -0,0 +1,11 @@ +import { PagedSavedLogSearch, Template } from '@umbraco-cms/backend-api'; +import type { DataSourceResponse } from '@umbraco-cms/models'; + +interface skipNtake { + skip?: number; + take?: number; +} + +export interface LogSearchDataSource { + getLogViewerSavedSearch(skipNtake: skipNtake): Promise>; +} diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/sources/log-search.server.data.ts b/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/sources/log-search.server.data.ts new file mode 100644 index 0000000000..a1be0cb494 --- /dev/null +++ b/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/data/sources/log-search.server.data.ts @@ -0,0 +1,33 @@ +import { LogSearchDataSource } from '.'; +import { LogViewerResource } from '@umbraco-cms/backend-api'; +import { UmbControllerHostInterface } from '@umbraco-cms/controller'; +import { tryExecuteAndNotify } from '@umbraco-cms/resources'; + +/** + * A data source for the Template detail that fetches data from the server + * @export + * @class UmbLogSearchesServerDataSource + * @implements {TemplateDetailDataSource} + */ +export class UmbLogSearchesServerDataSource implements LogSearchDataSource { + #host: UmbControllerHostInterface; + + /** + * Creates an instance of UmbLogSearchesServerDataSource. + * @param {UmbControllerHostInterface} host + * @memberof UmbLogSearchesServerDataSource + */ + constructor(host: UmbControllerHostInterface) { + this.#host = host; + } + + /** + * Gets the user's saved searches + * @param {string} key + * @return {*} + * @memberof UmbLogSearchesServerDataSource + */ + async getLogViewerSavedSearch() { + return await tryExecuteAndNotify(this.#host, LogViewerResource.getLogViewerSavedSearch({})); + } +} diff --git a/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/logviewer-root/logviewer-root-workspace.element.ts b/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/logviewer-root/logviewer-root-workspace.element.ts index daefbd2423..3c469e898b 100644 --- a/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/logviewer-root/logviewer-root-workspace.element.ts +++ b/src/Umbraco.Web.UI.Client/src/backoffice/settings/logviewer/workspace/logviewer-root/logviewer-root-workspace.element.ts @@ -19,7 +19,8 @@ export class UmbLogViewerRootWorkspaceElement extends LitElement { ]; render() { return html` - + + hello `; } diff --git a/src/Umbraco.Web.UI.Client/vite.config.ts b/src/Umbraco.Web.UI.Client/vite.config.ts index a9a8182fc9..611ff2686b 100644 --- a/src/Umbraco.Web.UI.Client/vite.config.ts +++ b/src/Umbraco.Web.UI.Client/vite.config.ts @@ -7,6 +7,9 @@ export default defineConfig({ build: { sourcemap: true, }, + server: { + open: '/backoffice', + }, plugins: [ viteStaticCopy({ targets: [