create repository, store and source

I have no clue what i'm doing lol
This commit is contained in:
Julia Gru
2023-02-07 14:28:44 +01:00
parent e0cd8f421a
commit bba7b9b785
7 changed files with 175 additions and 2 deletions

View File

@@ -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<T> extends DeepState<T[]> {

View File

@@ -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<void>((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 };
// }
}

View File

@@ -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<SavedLogSearch>([], (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>(
UmbLogSearchesStore.name,
'LogSearchesStore TOKEN XXX'
);

View File

@@ -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<DataSourceResponse<PagedSavedLogSearch>>;
}

View File

@@ -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({}));
}
}

View File

@@ -19,7 +19,8 @@ export class UmbLogViewerRootWorkspaceElement extends LitElement {
];
render() {
return html`
<umb-workspace-layout headline="Log Overview for Today" alias="Umb.Workspace.LogviewerRoot">
<umb-workspace-layout headline="Log Overview for today" alias="Umb.Workspace.LogviewerRoot">
hello
</umb-workspace-layout>
`;
}

View File

@@ -7,6 +7,9 @@ export default defineConfig({
build: {
sourcemap: true,
},
server: {
open: '/backoffice',
},
plugins: [
viteStaticCopy({
targets: [