Skip to content

Commit

Permalink
Model should handle all comms with JSONBlob Service
Browse files Browse the repository at this point in the history
  • Loading branch information
lbwexler committed Nov 30, 2024
1 parent 511c57a commit d82faa9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
19 changes: 4 additions & 15 deletions cmp/viewmanager/SaveAsDialogModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
*/

import {FormModel} from '@xh/hoist/cmp/form';
import {ViewInfo} from '@xh/hoist/cmp/viewmanager/ViewInfo';
import {HoistModel, managed, XH} from '@xh/hoist/core';
import {lengthIs, required} from '@xh/hoist/data';
import {makeObservable, action, observable} from '@xh/hoist/mobx';
import {View} from './View';
import {ViewManagerModel} from './ViewManagerModel';
Expand Down Expand Up @@ -68,33 +66,24 @@ export class SaveAsDialogModel extends HoistModel {
fields: [
{
name: 'name',
rules: [
required,
lengthIs({max: ViewInfo.NAME_MAX_LENGTH}),
({value}) => this.parent.validateViewNameAsync(value)
]
rules: [({value}) => this.parent.validateViewNameAsync(value)]
},
{name: 'description'}
]
});
}

private async doSaveAsAsync() {
const {formModel, parent, type} = this,
const {formModel, parent} = this,
{name, description} = formModel.getData(),
isValid = await formModel.validateAsync();

if (!isValid) return;

try {
const blob = await XH.jsonBlobService.createAsync({
type,
name,
description,
value: parent.getValue()
});
const ret = await this.parent.createViewAsync(name, description, parent.getValue());
this.close();
this.resolveOpen(View.fromBlob(blob, this.parent));
this.resolveOpen(ret);
} catch (e) {
XH.handleException(e);
}
Expand Down
2 changes: 0 additions & 2 deletions cmp/viewmanager/ViewInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {JsonBlob} from '@xh/hoist/svc';
* Metadata describing {@link View} managed by {@link ViewManagerModel}.
*/
export class ViewInfo {
static NAME_MAX_LENGTH = 50;

/** Unique Id */
readonly token: string;

Expand Down
52 changes: 45 additions & 7 deletions cmp/viewmanager/ViewManagerModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,55 @@ export class ViewManagerModel<T = PlainObject>
this.manageDialogOpen = false;
}

async validateViewNameAsync(name: string): Promise<string> {
// TODO: Consider going to server.
if (this.views.some(view => view.name === name?.trim())) {
async validateViewNameAsync(name: string, token: string = null): Promise<string> {
const maxLength = 50;
name = name?.trim();
if (!name) return 'Name is required';
if (name.length > maxLength) {
return `Name cannot be longer than ${maxLength} characters`;
}
if (this.views.some(view => view.name === name && view.token != token)) {
return `A ${this.typeDisplayName} with name '${name}' already exists`;
}

return null;
}

async deleteViewAsync(token: string) {
try {
await XH.jsonBlobService.archiveAsync(token);
this.removeFavorite(token);
} catch (e) {
throw XH.exception({message: `Unable to delete ${this.typeDisplayName}`, cause: e});
}
}

async updateViewAsync(token: string, name: string, description: string, isGlobal: boolean) {
try {
await XH.jsonBlobService.updateAsync(token, {
name: name.trim(),
description: description?.trim(),
acl: isGlobal ? '*' : null
});
} catch (e) {
throw XH.exception({message: `Unable to update ${this.typeDisplayName}`, cause: e});
}
}

async createViewAsync(name: string, description: string, value: PlainObject): Promise<View> {
try {
const blob = await XH.jsonBlobService.createAsync({
type: this.viewType,
name: name.trim(),
description: description?.trim(),
value
});
return View.fromBlob(blob, this);
} catch (e) {
throw XH.exception({message: `Unable to create ${this.typeDisplayName}`, cause: e});
}
}

//------------------
// Implementation
//------------------
Expand All @@ -434,10 +475,7 @@ export class ViewManagerModel<T = PlainObject>
const blob = await XH.jsonBlobService.getAsync(info.token);
return View.fromBlob(blob, this);
} catch (e) {
throw XH.exception({
message: `Unable to fetch ${this.typeDisplayName} '${info.name}'`,
cause: e
});
throw XH.exception({message: `Unable to fetch ${info.typedName}`, cause: e});
}
}

Expand Down
4 changes: 2 additions & 2 deletions desktop/cmp/viewmanager/ViewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export interface ViewManagerProps extends HoistProps<ViewManagerModel> {
showRevertButton?: ViewManagerStateButtonMode;
/** Side the save and revert buttons should appear on (default 'right') */
buttonSide?: 'left' | 'right';
/** True to render private views in sub-menu (Default false)*/
/** True to render private views in sub-menu (Default false) */
showPrivateViewsInSubMenu?: boolean;
/** True to render global views in sub-menu (Default false)*/
/** True to render global views in sub-menu (Default false) */
showGlobalViewsInSubMenu?: boolean;
}

Expand Down
19 changes: 4 additions & 15 deletions desktop/cmp/viewmanager/dialog/ManageDialogModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {FormModel} from '@xh/hoist/cmp/form';
import {GridAutosizeMode, GridModel} from '@xh/hoist/cmp/grid';
import {fragment, p} from '@xh/hoist/cmp/layout';
import {HoistModel, lookup, managed, TaskObserver, XH} from '@xh/hoist/core';
import {lengthIs, required} from '@xh/hoist/data';
import {Icon} from '@xh/hoist/icon';
import {makeObservable} from '@xh/hoist/mobx';
import {pluralize, throwIf} from '@xh/hoist/utils/js';
import {ViewInfo, ViewManagerModel} from '@xh/hoist/cmp/viewmanager';
import {ViewManagerModel} from '@xh/hoist/cmp/viewmanager';
import {startCase} from 'lodash';

/**
Expand Down Expand Up @@ -192,12 +191,7 @@ export class ManageDialogModel extends HoistModel {
if (!confirmed) return;
}

await XH.jsonBlobService.updateAsync(token, {
name,
description,
acl: isGlobal ? '*' : null
});

await parent.updateViewAsync(token, name, description, isGlobal);
await parent.refreshAsync();
}

Expand Down Expand Up @@ -231,8 +225,7 @@ export class ManageDialogModel extends HoistModel {
if (!confirmed) return;

for (const token of selectedIds) {
parent.removeFavorite(token);
await XH.jsonBlobService.archiveAsync(token);
await parent.deleteViewAsync(token);
}

await parent.refreshAsync();
Expand Down Expand Up @@ -306,11 +299,7 @@ export class ManageDialogModel extends HoistModel {
fields: [
{
name: 'name',
rules: [
required,
lengthIs({max: ViewInfo.NAME_MAX_LENGTH}),
({value}) => this.parent.validateViewNameAsync(value)
]
rules: [({value}) => this.parent.validateViewNameAsync(value, this.selectedId)]
},
{name: 'description'},
{name: 'isGlobal', displayName: 'Global'},
Expand Down

0 comments on commit d82faa9

Please sign in to comment.