Skip to content

Commit

Permalink
feat(core): add view id to view value object
Browse files Browse the repository at this point in the history
fix #156
  • Loading branch information
nichenqin committed Jan 6, 2023
1 parent cd2bc05 commit 34a0757
Show file tree
Hide file tree
Showing 31 changed files with 125 additions and 75 deletions.
2 changes: 1 addition & 1 deletion apps/web/components/table/toolbar-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const ToolbarView: React.FC<ITableBaseProps> = ({ table }) => {
onChange={(type) => {
switchDisplayType.mutate({
tableId: table.id.value,
viewName: view.name.unpack(),
viewId: view.name.unpack(),
displayType: type as IViewDisplayType,
})
}}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/__snapshots__/table.factory.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ ResultType {
},
"fieldsOrder": undefined,
"filter": undefined,
"id": ViewId {
"props": {
"value": "table",
},
},
"kanban": undefined,
"name": ViewName {
"props": {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/__snapshots__/table.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ Table {
},
"fieldsOrder": undefined,
"filter": undefined,
"id": ViewId {
"props": {
"value": "name",
},
},
"kanban": undefined,
"name": ViewName {
"props": {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/commands/move-field/move-field.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import type { IMoveFieldCommandInput } from './move-field.command.interface'

export class MoveFieldCommand extends Command implements IMoveFieldCommandInput {
readonly tableId: string
readonly viewName?: string
readonly viewId?: string
readonly from: string
readonly to: string

constructor(props: CommandProps<IMoveFieldCommandInput>) {
super(props)
this.tableId = props.tableId
this.viewName = props.viewName
this.viewId = props.viewId
this.from = props.from
this.to = props.to
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { setCalendarFieldSchema, viewNameSchema } from '../../view'
export const setCalendarFieldCommandInput = z
.object({
tableId: tableIdSchema,
viewName: viewNameSchema.optional(),
viewId: viewNameSchema.optional(),
})
.merge(setCalendarFieldSchema)
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import type { ISetCalendarFieldCommandInput } from './set-calendar-field.command

export class SetCalendarFieldCommand extends Command implements ISetCalendarFieldCommandInput {
readonly tableId: string
readonly viewName?: string
readonly viewId?: string
readonly field: string

constructor(props: CommandProps<ISetCalendarFieldCommandInput>) {
super(props)
this.tableId = props.tableId
this.viewName = props.viewName
this.viewId = props.viewId
this.field = props.field
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import type { ISetFieldVisibilityCommandInput } from './set-field-visibility.com

export class SetFieldVisibilityCommand extends Command implements ISetFieldVisibilityCommandInput {
public readonly tableId: string
public readonly viewName?: string
public readonly viewId?: string
public readonly fieldName: string
public readonly hidden: boolean

constructor(props: CommandProps<ISetFieldVisibilityCommandInput>) {
super(props)
this.tableId = props.tableId
this.viewName = props.viewName
this.viewId = props.viewId
this.fieldName = props.fieldName
this.hidden = props.hidden
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import type { ISetFieldWidthCommandInput } from './set-field-width.command.inter

export class SetFieldWidthCommand extends Command implements ISetFieldWidthCommandInput {
public readonly tableId: string
public readonly viewName?: string
public readonly viewId?: string
public readonly fieldName: string
public readonly width: number

constructor(props: CommandProps<ISetFieldWidthCommandInput>) {
super(props)
this.tableId = props.tableId
this.viewName = props.viewName
this.viewId = props.viewId
this.fieldName = props.fieldName
this.width = props.width
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class SetFiltersCommandHandler implements ISetFilterCommandHandler {
async execute(command: SetFitlersCommand): Promise<void> {
const table = (await this.repo.findOneById(command.tableId)).unwrap()

const spec = table.setFilter(command.filter, command.viewName).unwrap()
const spec = table.setFilter(command.filter, command.viewId).unwrap()
await this.repo.updateOneById(command.tableId, spec)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { viewNameSchema } from '../../view'

export const setFiltersCommandInput = z.object({
tableId: tableIdSchema,
viewName: viewNameSchema.optional(),
viewId: viewNameSchema.optional(),
filter: rootFilter.nullable(),
})
4 changes: 2 additions & 2 deletions packages/core/commands/set-filters/set-filters.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import type { ISetFilterCommandInput } from './set-filters.command.interface'

export class SetFitlersCommand extends Command implements ISetFilterCommandInput {
readonly tableId: string
readonly viewName?: string
readonly viewId?: string
readonly filter: IRootFilter | null

constructor(props: CommandProps<ISetFilterCommandInput>) {
super(props)
this.tableId = props.tableId
this.viewName = props.viewName
this.viewId = props.viewId
this.filter = props.filter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { setKanbanFieldSchema, viewNameSchema } from '../../view'
export const setKanbanFieldCommandInput = z
.object({
tableId: tableIdSchema,
viewName: viewNameSchema.optional(),
viewId: viewNameSchema.optional(),
})
.merge(setKanbanFieldSchema)
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import type { ISetKanbanFieldCommandInput } from './set-kanban-field.command.int

export class SetKanbanFieldCommand extends Command implements ISetKanbanFieldCommandInput {
readonly tableId: string
readonly viewName?: string
readonly viewId?: string
readonly field: string

constructor(props: CommandProps<ISetKanbanFieldCommandInput>) {
super(props)
this.tableId = props.tableId
this.viewName = props.viewName
this.viewId = props.viewId
this.field = props.field
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import type { ISwitchDisplayTypeCommandInput } from './switch-display-type.comma

export class SwitchDisplayTypeCommand extends Command implements ISwitchDisplayTypeCommandInput {
public readonly tableId: string
public readonly viewName?: string
public readonly viewId?: string
public readonly displayType: IViewDisplayType

constructor(props: CommandProps<ISwitchDisplayTypeCommandInput>) {
super(props)
this.tableId = props.tableId
this.viewName = props.viewName
this.viewId = props.viewId
this.displayType = props.displayType
}
}
5 changes: 5 additions & 0 deletions packages/core/fixtures/table.fixtuer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ test('createTestTable', () => {
},
"fieldsOrder": undefined,
"filter": undefined,
"id": ViewId {
"props": {
"value": "name",
},
},
"kanban": undefined,
"name": ViewName {
"props": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class GetRecordsQueryHandler implements IQueryHandler<GetRecordsQuery, IG

async execute(query: GetRecordsQuery): Promise<IGetRecordsOutput> {
const table = (await this.tableRepo.findOneById(query.tableId)).unwrap()
const filter = table.getSpec(query.viewName)
const filter = table.getSpec(query.viewId)

const spec = WithRecordTableId.fromString(query.tableId)
.map((s) => (filter.isNone() ? s : s.and(filter.unwrap())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { viewNameSchema } from '../../view'

export const getRecordsQueryInput = z.object({
tableId: tableIdSchema,
viewName: viewNameSchema.optional(),
viewId: viewNameSchema.optional(),
})
4 changes: 2 additions & 2 deletions packages/core/queries/get-records/get-records.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type { IGetRecordsQuery } from './get-records.query.interface'

export class GetRecordsQuery extends Query implements IGetRecordsQuery {
readonly tableId: string
readonly viewName?: string
readonly viewId?: string
constructor(query: IGetRecordsQuery) {
super()
this.tableId = query.tableId
this.viewName = query.viewName
this.viewId = query.viewId
}
}
8 changes: 4 additions & 4 deletions packages/core/specifications/filters.specificaiton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import type { Table } from '../table'
import type { ITableSpecVisitor } from './interface'

export class WithFilter extends CompositeSpecification<Table, ITableSpecVisitor> {
constructor(public readonly filter: IRootFilter | null, public readonly viewName: string) {
constructor(public readonly filter: IRootFilter | null, public readonly viewId: string) {
super()
}

isSatisfiedBy(t: Table): boolean {
if (!this.filter) {
return isEmpty(t.mustGetView(this.viewName).filter)
return isEmpty(t.mustGetView(this.viewId).filter)
}
return t.mustGetView(this.viewName).filter?.equals(new RootFilter(this.filter)) ?? false
return t.mustGetView(this.viewId).filter?.equals(new RootFilter(this.filter)) ?? false
}

mutate(t: Table): Result<Table, string> {
const view = t.mustGetView(this.viewName)
const view = t.mustGetView(this.viewId)
view.setFilter(this.filter)
return Ok(t)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import type { IViewFieldOption } from '../view/view-field-options'
import { DEFAULT_WIDTH } from '../view/view-field-options'

abstract class BaseViewFieldOptionSpec extends CompositeSpecification<Table, ITableSpecVisitor> {
constructor(public readonly fieldName: string, public readonly viewName: string) {
constructor(public readonly fieldName: string, public readonly viewId: string) {
super()
}

protected getView(t: Table): View {
return t.mustGetView(this.viewName)
return t.mustGetView(this.viewId)
}

protected getFieldOption(t: Table): IViewFieldOption {
Expand All @@ -26,12 +26,12 @@ abstract class BaseViewFieldOptionSpec extends CompositeSpecification<Table, ITa
}

export class WithFieldWidth extends BaseViewFieldOptionSpec {
constructor(fieldName: string, viewName: string, public readonly width: number) {
super(fieldName, viewName)
constructor(fieldName: string, viewId: string, public readonly width: number) {
super(fieldName, viewId)
}

static default(fieldName: string, viewName: string) {
return new this(fieldName, viewName, DEFAULT_WIDTH)
static default(fieldName: string, viewId: string) {
return new this(fieldName, viewId, DEFAULT_WIDTH)
}

isSatisfiedBy(t: Table): boolean {
Expand All @@ -50,8 +50,8 @@ export class WithFieldWidth extends BaseViewFieldOptionSpec {
}

export class WithFieldVisibility extends BaseViewFieldOptionSpec {
constructor(fieldName: string, viewName: string, public readonly hidden: boolean) {
super(fieldName, viewName)
constructor(fieldName: string, viewId: string, public readonly hidden: boolean) {
super(fieldName, viewId)
}
isSatisfiedBy(t: Table): boolean {
return this.getView(t).getFieldHidden(this.fieldName) === this.hidden
Expand Down
45 changes: 22 additions & 23 deletions packages/core/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,41 +69,40 @@ export class Table {
}

private createDefaultView(viewName?: string): View {
return View.create({ name: viewName ?? this.name.value, displayType: defaultViewDiaplyType })
// TODO: move to views value object
return View.create({
id: viewName ?? this.name.value,
name: viewName ?? this.name.value,
displayType: defaultViewDiaplyType,
})
}

public createDefaultViews(): Views {
return new Views([this.createDefaultView()])
}

public getSpec(viewName?: string) {
return this.mustGetView(viewName).spec
public getSpec(viewId?: string) {
return this.mustGetView(viewId).spec
}

public getView(viewName?: string): Option<View> {
if (!viewName) {
public getView(viewId?: string): Option<View> {
if (!viewId) {
return Some(this.defaultView)
}

return this.views.getByName(viewName)
return this.views.getById(viewId)
}

public getOrCreateView(viewName?: string): [View, Option<TableCompositeSpecificaiton>] {
const view = this.views.getByName(viewName)
if (view.isSome()) return [view.unwrap(), None]
return this.getOrCreateDefaultView(viewName)
}

public mustGetView(viewName?: string): View {
if (!viewName) {
public mustGetView(viewId?: string): View {
if (!viewId) {
return this.defaultView
}

return this.views.getByName(viewName).unwrapOrElse(() => this.defaultView)
return this.views.getById(viewId).unwrapOrElse(() => this.defaultView)
}

public setFilter(filters: IRootFilter | null, viewName?: string): Result<TableCompositeSpecificaiton, string> {
const vn = this.mustGetView(viewName).name.unpack()
public setFilter(filters: IRootFilter | null, viewId?: string): Result<TableCompositeSpecificaiton, string> {
const vn = this.mustGetView(viewId).name.unpack()
const spec = new WithFilter(filters, vn)
spec.mutate(this).unwrap()
return Ok(spec)
Expand Down Expand Up @@ -147,44 +146,44 @@ export class Table {
}

public setFieldWidth(input: ISetFieldWidthSchema): TableCompositeSpecificaiton {
const view = this.mustGetView(input.viewName)
const view = this.mustGetView(input.viewId)
const spec = view.setFieldWidth(input.fieldName, input.width)
spec.mutate(this)
return spec
}

public switchDisplayType(input: ISwitchDisplayTypeSchema): TableCompositeSpecificaiton {
const view = this.mustGetView(input.viewName)
const view = this.mustGetView(input.viewId)
const spec = view.switchDisplayType(input.displayType)
spec.mutate(this)
return spec
}

public setFieldVisibility(input: ISetFieldVisibilitySchema): TableCompositeSpecificaiton {
const view = this.mustGetView(input.viewName)
const view = this.mustGetView(input.viewId)
const spec = view.setFieldVisibility(input.fieldName, input.hidden)
spec.mutate(this)
return spec
}

public setKanbanField(input: ISetKanbanFieldSchema): TableCompositeSpecificaiton {
const view = this.mustGetView(input.viewName)
const view = this.mustGetView(input.viewId)
const field = this.schema.getFieldById(input.field).unwrap()
const spec = view.setKanbanFieldSpec(field.id)
spec.mutate(this)
return spec
}

public setCalendarField(input: ISetCalendarFieldSchema): TableCompositeSpecificaiton {
const view = this.mustGetView(input.viewName)
const view = this.mustGetView(input.viewId)
const field = this.schema.getFieldById(input.field).unwrap()
const spec = view.setCalendarFieldSpec(field.id)
spec.mutate(this)
return spec
}

public moveField(input: IMoveFieldSchema): TableCompositeSpecificaiton {
const [view, viewSpec] = this.getOrCreateDefaultView(input.viewName)
const [view, viewSpec] = this.getOrCreateDefaultView(input.viewId)
const viewFieldsOrder = this.getFieldsOrder(view).move(input.from, input.to)

const spec = new WithViewFieldsOrder(viewFieldsOrder, view)
Expand Down
Loading

0 comments on commit 34a0757

Please sign in to comment.