Skip to content

Commit

Permalink
feat: navigate to new form after create form
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Sep 19, 2024
1 parent fb88262 commit 66b0528
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { Input } from "$lib/components/ui/input"
import { hasPermission } from "$lib/store/space-member.store"
import { getNextName } from "@undb/utils"
import { formId } from "$lib/store/tab.store"
const table = getTable()
Expand All @@ -19,9 +20,10 @@
const createFormMutation = createMutation({
mutationFn: trpc.table.form.create.mutate,
mutationKey: ["table", $table.id.value, "createForm"],
async onSuccess() {
async onSuccess(data) {
toast.success("create form successfully")
await invalidate(`table:${$table.id.value}`)
formId.set(data.formId)
onSuccess?.()
},
onError(e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { CreateTableFormCommand } from "@undb/commands"
import { CreateTableFormCommand, type ICreateTableFormCommandOutput } from "@undb/commands"
import { commandHandler } from "@undb/cqrs"
import { singleton } from "@undb/di"
import type { ICommandHandler } from "@undb/domain"
import { injectTableService, type ITableService } from "@undb/table"

@commandHandler(CreateTableFormCommand)
@singleton()
export class CreateTableFormCommandHandler implements ICommandHandler<CreateTableFormCommand, any> {
export class CreateTableFormCommandHandler
implements ICommandHandler<CreateTableFormCommand, ICreateTableFormCommandOutput>
{
constructor(
@injectTableService()
private readonly service: ITableService,
) {}

async execute(command: CreateTableFormCommand): Promise<any> {
const table = await this.service.createTableForm(command.input)
async execute(command: CreateTableFormCommand): Promise<ICreateTableFormCommandOutput> {
const { table, form } = await this.service.createTableForm(command.input)

return table.id.value
return {
tableId: table.id.value,
formId: form.id,
}
}
}
9 changes: 8 additions & 1 deletion packages/commands/src/create-table-form.command.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Command, type CommandProps } from "@undb/domain"
import { createTableFormDTO, type ICreateTableFormDTO } from "@undb/table"
import { createTableFormDTO, formId, tableId, type ICreateTableFormDTO } from "@undb/table"
import { z } from "@undb/zod"

export const createTableFormCommand = createTableFormDTO

export type ICreateFormCommand = z.infer<typeof createTableFormCommand>

export const createTableFormCommandOutput = z.object({
tableId,
formId,
})

export type ICreateTableFormCommandOutput = z.infer<typeof createTableFormCommandOutput>

export class CreateTableFormCommand extends Command {
public readonly input: ICreateTableFormDTO

Expand Down
7 changes: 5 additions & 2 deletions packages/table/src/methods/create-form.method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import type { TableComositeSpecification } from "../specifications"
import { WithNewFormSpecification } from "../specifications/table-forms.specification"
import type { TableDo } from "../table.do"

export function createFormMethod(this: TableDo, dto: ICreateFormDTO): Option<TableComositeSpecification> {
export function createFormMethod(
this: TableDo,
dto: ICreateFormDTO,
): { spec: Option<TableComositeSpecification>; form: FormVO } {
const form = FormVO.create(this, dto)
const spec = new WithNewFormSpecification(form)

Expand All @@ -20,5 +23,5 @@ export function createFormMethod(this: TableDo, dto: ICreateFormDTO): Option<Tab
// })
// this.addDomainEvent(event)

return Some(spec)
return { spec: Some(spec), form }
}
10 changes: 7 additions & 3 deletions packages/table/src/services/methods/create-table-form.method.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import type { FormVO } from "../../modules"
import type { ICreateTableFormDTO } from "../../modules/forms/dto/create-form.dto"
import { TableIdVo } from "../../table-id.vo"
import type { TableDo } from "../../table.do"
import type { TableService } from "../table.service"

export async function createTableFormMethod(this: TableService, dto: ICreateTableFormDTO): Promise<TableDo> {
export async function createTableFormMethod(
this: TableService,
dto: ICreateTableFormDTO,
): Promise<{ table: TableDo; form: FormVO }> {
const table = (await this.repository.findOneById(new TableIdVo(dto.tableId))).expect("Not found table")

const spec = table.$createForm(dto)
const { spec, form } = table.$createForm(dto)

await this.repository.updateOneById(table, spec)

return table
return { table, form }
}
3 changes: 2 additions & 1 deletion packages/table/src/services/table.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
IUpdateTableFieldDTO,
} from "../dto"
import {
FormVO,
injectRecordQueryRepository,
injectRecordRepository,
type ICreateTableViewDTO,
Expand Down Expand Up @@ -54,7 +55,7 @@ export interface ITableService {
deleteTableField(dto: IDeleteTableFieldDTO): Promise<TableDo>
duplicateTableField(dto: IDuplicateTableFieldDTO): Promise<TableDo>

createTableForm(dto: ICreateTableFormDTO): Promise<TableDo>
createTableForm(dto: ICreateTableFormDTO): Promise<{ table: TableDo; form: FormVO }>
deleteTableForm(dto: IDeleteTableFormDTO): Promise<TableDo>
createTableView(dto: ICreateTableViewDTO): Promise<{ table: TableDo; view: View }>

Expand Down
2 changes: 2 additions & 0 deletions packages/trpc/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
createTableCommand,
createTableFieldCommand,
createTableFormCommand,
createTableFormCommandOutput,
createTableViewCommand,
createTableViewCommandOutput,
createWebhookCommand,
Expand Down Expand Up @@ -141,6 +142,7 @@ const formRouter = t.router({
create: privateProcedure
.use(authz("form:create"))
.input(createTableFormCommand)
.output(createTableFormCommandOutput)
.mutation(({ input }) => commandBus.execute(new CreateTableFormCommand(input))),
set: privateProcedure
.use(authz("form:update"))
Expand Down

0 comments on commit 66b0528

Please sign in to comment.