From 96b9d6a5d230772de1e3a0e66cb8d9e4bc4732a1 Mon Sep 17 00:00:00 2001 From: Chen-I Lim Date: Wed, 14 Oct 2020 13:44:35 -0700 Subject: [PATCH] Add generic fields to IBlock --- server/main/octoDatabase.go | 8 ++++---- src/client/block.ts | 25 +++++++++++++++++------- src/client/board.ts | 23 +++++++++++++++++----- src/client/boardView.ts | 39 +++++++++++++++++++++++++++---------- src/client/octoTypes.ts | 2 ++ 5 files changed, 71 insertions(+), 26 deletions(-) diff --git a/server/main/octoDatabase.go b/server/main/octoDatabase.go index 4d8bf5e76..dfae0d2f9 100644 --- a/server/main/octoDatabase.go +++ b/server/main/octoDatabase.go @@ -50,9 +50,11 @@ func NewSQLStore(dbType, connectionString string) (*SQLStore, error) { type Block struct { ID string `json:"id"` ParentID string `json:"parentId"` + Schema int64 `json:"schema"` Type string `json:"type"` Title string `json:"title"` Properties map[string]interface{} `json:"properties"` + Fields map[string]interface{} `json:"fields"` CreateAt int64 `json:"createAt"` UpdateAt int64 `json:"updateAt"` DeleteAt int64 `json:"deleteAt"` @@ -68,8 +70,7 @@ func (s *SQLStore) createTablesIfNotExists() error { insert_at DATETIME NOT NULL DEFAULT current_timestamp, parent_id VARCHAR(36), type TEXT, - title TEXT, - properties TEXT, + json TEXT, create_at BIGINT, update_at BIGINT, delete_at BIGINT, @@ -81,8 +82,7 @@ func (s *SQLStore) createTablesIfNotExists() error { insert_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), parent_id VARCHAR(36), type TEXT, - title TEXT, - properties TEXT, + json TEXT, create_at BIGINT, update_at BIGINT, delete_at BIGINT, diff --git a/src/client/block.ts b/src/client/block.ts index 10258cfa6..08aba81e3 100644 --- a/src/client/block.ts +++ b/src/client/block.ts @@ -3,6 +3,7 @@ import { Utils } from "./utils" class Block implements IBlock { id: string = Utils.createGuid() + schema: number parentId: string type: string title: string @@ -10,6 +11,7 @@ class Block implements IBlock { url?: string order: number properties: Record = {} + fields: Record = {} createAt: number = Date.now() updateAt: number = 0 deleteAt: number = 0 @@ -31,26 +33,35 @@ class Block implements IBlock { const now = Date.now() this.id = block.id || Utils.createGuid() + this.schema = 1 this.parentId = block.parentId this.type = block.type + + this.fields = block.fields ? { ...block.fields } : {} + this.title = block.title this.icon = block.icon this.url = block.url this.order = block.order + this.createAt = block.createAt || now this.updateAt = block.updateAt || now this.deleteAt = block.deleteAt || 0 - if (Array.isArray(block.properties)) { - // HACKHACK: Port from old schema - this.properties = {} - for (const property of block.properties) { - if (property.id) { - this.properties[property.id] = property.value + if (block.schema !== 1) { + if (Array.isArray(block.properties)) { + // HACKHACK: Port from old schema + this.properties = {} + for (const property of block.properties) { + if (property.id) { + this.properties[property.id] = property.value + } } + } else { + this.properties = { ...block.properties || {} } } } else { - this.properties = { ...block.properties || {} } + this.properties = { ...block.properties } // Shallow copy here. Derived classes must make deep copies of their known properties in their constructors. } } } diff --git a/src/client/board.ts b/src/client/board.ts index b1387d8a9..f01ba7f61 100644 --- a/src/client/board.ts +++ b/src/client/board.ts @@ -16,24 +16,37 @@ interface IPropertyTemplate { } class Board extends Block { - cardProperties: IPropertyTemplate[] = [] + get cardProperties(): IPropertyTemplate[] { return this.fields.cardProperties as IPropertyTemplate[] } + set cardProperties(value: IPropertyTemplate[]) { this.fields.cardProperties = value } constructor(block: any = {}) { super(block) this.type = "board" - if (block.cardProperties) { - // Deep clone of properties and their options - this.cardProperties = block.cardProperties.map((o: IPropertyTemplate) => { + + if (block.fields?.cardProperties) { + // Deep clone of card properties and their options + this.cardProperties = block.fields?.cardProperties.map((o: IPropertyTemplate) => { return { id: o.id, name: o.name, type: o.type, - options: o.options ? o.options.map(option => ({...option})): [] + options: o.options ? o.options.map(option => ({ ...option })) : [] } }) } else { this.cardProperties = [] } + + if (block.schema !== 1) { + this.cardProperties = block.cardProperties?.map((o: IPropertyTemplate) => { + return { + id: o.id, + name: o.name, + type: o.type, + options: o.options ? o.options.map(option => ({ ...option })) : [] + } + }) || [] + } } } diff --git a/src/client/boardView.ts b/src/client/boardView.ts index 95e4e74d2..c1aed1b23 100644 --- a/src/client/boardView.ts +++ b/src/client/boardView.ts @@ -5,21 +5,40 @@ type IViewType = "board" | "table" | "calendar" | "list" | "gallery" type ISortOption = { propertyId: "__name" | string, reversed: boolean } class BoardView extends Block { - viewType: IViewType - groupById?: string - sortOptions: ISortOption[] - visiblePropertyIds: string[] - filter?: FilterGroup + get viewType(): IViewType { return this.fields.viewType } + set viewType(value: IViewType) { this.fields.viewType = value } + + get groupById(): string | undefined { return this.fields.groupById } + set groupById(value: string | undefined) { this.fields.groupById = value } + + get sortOptions(): ISortOption[] { return this.fields.sortOptions } + set sortOptions(value: ISortOption[]) { this.fields.sortOptions = value } + + get visiblePropertyIds(): string[] { return this.fields.visiblePropertyIds } + set visiblePropertyIds(value: string[]) { this.fields.visiblePropertyIds = value } + + get filter(): FilterGroup | undefined { return this.fields.filter } + set filter(value: FilterGroup | undefined) { this.fields.filter = value } constructor(block: any = {}) { super(block) this.type = "view" - this.viewType = block.viewType || "board" - this.groupById = block.groupById - this.sortOptions = block.sortOptions ? block.sortOptions.map((o: ISortOption) => ({...o})) : [] // Deep clone - this.visiblePropertyIds = block.visiblePropertyIds ? block.visiblePropertyIds.slice() : [] - this.filter = new FilterGroup(block.filter) + + this.sortOptions = block.properties?.sortOptions?.map((o: ISortOption) => ({ ...o })) || [] // Deep clone + this.visiblePropertyIds = block.properties?.visiblePropertyIds?.slice() || [] + this.filter = new FilterGroup(block.properties?.filter) + + // TODO: Remove this fixup code + if (block.schema !== 1) { + this.viewType = block.viewType || "board" + this.groupById = block.groupById + this.sortOptions = block.sortOptions ? block.sortOptions.map((o: ISortOption) => ({ ...o })) : [] // Deep clone + this.visiblePropertyIds = block.visiblePropertyIds ? block.visiblePropertyIds.slice() : [] + this.filter = new FilterGroup(block.filter) + } + + if (!this.viewType) { this.viewType = "board" } } } diff --git a/src/client/octoTypes.ts b/src/client/octoTypes.ts index d02c379e5..7047c87c6 100644 --- a/src/client/octoTypes.ts +++ b/src/client/octoTypes.ts @@ -3,12 +3,14 @@ interface IBlock { id: string parentId: string + schema: number type: string title?: string url?: string // TODO: Move to properties (_url) icon?: string order: number properties: Record + fields: Record createAt: number updateAt: number