Handle top-level page creation for offline
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
Garrett Mills 2020-10-25 19:07:03 -05:00
parent fe7e955875
commit 8f5ad697b3
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
2 changed files with 103 additions and 5 deletions

View File

@ -303,10 +303,9 @@ export class AppComponent implements OnInit {
{ {
text: 'Create', text: 'Create',
handler: async args => { handler: async args => {
this.api.post('/page/create', args).subscribe(res => { const page = await this.api.createPage(args.name);
this.router.navigate(['/editor', { id: res.data.UUID }]); this.reloadMenuItems().subscribe();
this.reloadMenuItems().subscribe(); await this.router.navigate(['/editor', { id: page.UUID }]);
});
} }
} }
] ]

View File

@ -10,7 +10,9 @@ import {Codium} from './db/Codium';
import {Database} from './db/Database'; import {Database} from './db/Database';
import {DatabaseColumn} from './db/DatabaseColumn'; import {DatabaseColumn} from './db/DatabaseColumn';
import {DatabaseEntry} from './db/DatabaseEntry'; import {DatabaseEntry} from './db/DatabaseEntry';
import {FileGroup} from "./db/FileGroup"; import {FileGroup} from './db/FileGroup';
import {Page} from './db/Page'
import {PageNode} from "./db/PageNode";
export class ResourceNotAvailableOfflineError extends Error { export class ResourceNotAvailableOfflineError extends Error {
constructor(msg = 'This resource is not yet available offline on this device.') { constructor(msg = 'This resource is not yet available offline on this device.') {
@ -868,4 +870,101 @@ export class ApiService {
}); });
}); });
} }
public createPage(name: string): Promise<any> {
return new Promise(async (res, rej) => {
// If we're offline, create a stub page to be saved later
if ( this.isOffline ) {
const page = new Page(
Page.getUUID(),
'New Page',
'OrgUserId',
true,
true,
'0',
[],
String(new Date()),
String(new Date()),
true,
'create user id',
'update user id',
[],
false,
false,
true
);
const firstNode = new PageNode(
PageNode.getUUID(),
'paragraph',
JSON.stringify({ Value: 'Click to edit...' }),
page.UUID,
String(new Date()),
String(new Date()),
'create user id',
'update user id',
true
);
await firstNode.save();
page.NodeIds.push(firstNode.UUID);
await page.save();
// Because we're offline, we need to manually create the menu item node
const topLevelItem = await this.db.menuItems.where({
serverId: 0,
name: 'My Info Tree',
}).first() as MenuItem;
if ( topLevelItem ) {
const newItem = new MenuItem(
page.Name,
MenuItem.getUUID(),
[],
false,
false,
false,
'page',
false,
true
);
await newItem.save();
topLevelItem.childIds.push(newItem.serverId);
await topLevelItem.save();
}
return res(page.getSaveRecord());
}
// If we're online, the server will handle all of that mess...
this.post('/page/create', { name }).subscribe({
next: async result => {
const page = new Page(
result.data.UUID,
result.data.Name,
result.data.OrgUserId,
result.data.IsPublic,
result.data.IsVisibleInMenu,
result.data.ParentId,
result.data.NodeIds,
result.data.CreatedAt,
result.data.UpdatedAt,
true,
result.data.CreatedUserId,
result.data.UpdateUserId,
result.data.ChildPageIds,
result.data.noDelete,
result.data.virtual
);
await page.save();
return res(result.data);
},
error: rej,
});
});
}
} }