Skip to content

Commit

Permalink
feat: add response interceptor on form definition
Browse files Browse the repository at this point in the history
  • Loading branch information
cafadev committed Dec 19, 2023
1 parent 667f11c commit d9f3c1f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
6 changes: 6 additions & 0 deletions demo-element-plus/src/viewer/FTableViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const form = useForm({
url: 'artists/',
title: '{{ Crear artista | Actualizar artista }}',
},
responseInterceptor: {
// 201: (formId, response) => {
// console.log('🚀 ~ file: FTableViewer.vue:65 ~ response:', response)
// console.log(response)
// },
},
})
const table = useTable({
Expand Down
16 changes: 14 additions & 2 deletions packages/core/src/forms/axioma/commands/create-form.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { meta } from '@fancy-crud/bus'
import type { BaseCommand } from '@fancy-crud/bus'
import type { ResponseInterceptorState } from '@packages/core/common/response-interceptor/axioma'
import type { BaseObjectWithRawFields, Form, RawFormButtons, RawSetting } from '../typing'

/**
* A class that provides functionality to create a form from raw fields and settings.
*/
export class CreateFormCommand<T extends BaseObjectWithRawFields, U extends RawSetting, V extends RawFormButtons> implements BaseCommand {
export class CreateFormCommand<
T extends BaseObjectWithRawFields,
U extends RawSetting,
V extends RawFormButtons,
TypeResponseInterceptor extends ResponseInterceptorState,
> implements BaseCommand {
public readonly meta = meta(ICreateFormHandler)

constructor(
public readonly id: string,
public readonly rawFields: T,
public readonly rawButtons?: V,
public readonly rawSettings?: U,
public readonly responseInterceptor?: TypeResponseInterceptor,
) {}
}

Expand All @@ -25,5 +32,10 @@ export abstract class ICreateFormHandler {
* @param rawSettings - An optional `RawSettings` object containing the raw settings to be normalized.
* @returns A `Form` object containing the normalized fields and settings.
*/
abstract execute<T extends BaseObjectWithRawFields, U extends RawSetting, V extends RawFormButtons>({ id, rawFields, rawSettings, rawButtons }: CreateFormCommand<T, U, V>): Form<T, V>
abstract execute<
T extends BaseObjectWithRawFields,
U extends RawSetting,
V extends RawFormButtons,
TypeResponseInterceptor extends ResponseInterceptorState,
>({ id, rawFields, rawSettings, rawButtons }: CreateFormCommand<T, U, V, TypeResponseInterceptor>): Form<T, V>
}
13 changes: 11 additions & 2 deletions packages/core/src/forms/capabilities/create-form.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Bus, inject } from '@fancy-crud/bus'
import { INotificationStore } from '@packages/core/common/notifications/axioma'
import type { ResponseInterceptorState } from '@packages/core/common/response-interceptor/axioma'
import { IResponseInterceptorStore } from '@packages/core/common/response-interceptor/axioma'

import type {
Expand All @@ -25,7 +26,12 @@ export class CreateFormHandler implements ICreateFormHandler {
private ruleConfigStore: IRuleConfigStore = inject(IRuleConfigStore),
) {}

execute<T extends BaseObjectWithRawFields, U extends RawSetting, V extends RawFormButtons>({ id, rawFields, rawSettings, rawButtons }: CreateFormCommand<T, U, V>): Form<T, V> {
execute<
T extends BaseObjectWithRawFields,
U extends RawSetting,
V extends RawFormButtons,
TypeResponseInterceptor extends ResponseInterceptorState,
>({ id, rawFields, rawSettings, rawButtons, responseInterceptor }: CreateFormCommand<T, U, V, TypeResponseInterceptor>): Form<T, V> {
const formId = Symbol(id)
const bus = new Bus()

Expand All @@ -48,7 +54,10 @@ export class CreateFormHandler implements ICreateFormHandler {
})

this.notificationStore.save(formId, getDefaultNotificationHandler())
this.responseInterceptorStore.save(formId, getDefaultInterceptors())
this.responseInterceptorStore.save(formId, {
...getDefaultInterceptors(),
...(responseInterceptor || {}),
})
this.ruleConfigStore.save(formId, {})

return {
Expand Down
7 changes: 5 additions & 2 deletions packages/vue/src/forms/composables/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
NormalizedSettings,
RawFormButtons,
RawSetting,
ResponseInterceptorState,
} from '@fancy-crud/core'
import { Bus, CreateFormCommand, IFormStore, IRuleConfigStore, rulesConfig } from '@fancy-crud/core'
import type { Args, UseForm } from '../typing'
Expand All @@ -24,13 +25,15 @@ export function useForm<
TypeFields extends BaseObjectWithRawFields,
TypeButtons extends RawFormButtons,
TypeSettings extends RawSetting,
>(args: Args<TypeFields, TypeButtons, TypeSettings>): UseForm<TypeFields, TypeButtons, TypeSettings> {
TypeResponseInterceptor extends ResponseInterceptorState,
>(args: Args<TypeFields, TypeButtons, TypeSettings, TypeResponseInterceptor>): UseForm<TypeFields, TypeButtons, TypeSettings> {
const {
id: _id = '',
fields: rawFields,
buttons: rawButtons,
settings: rawSettings,
rulesConfig: customRulesConfig,
responseInterceptor = {},
} = args

const formStore: IFormStore = inject(IFormStore.name)!
Expand All @@ -44,7 +47,7 @@ export function useForm<
normalizedButtons,
normalizedSettings,
} = bus.execute(
new CreateFormCommand(_id, rawFields, rawButtons, rawSettings),
new CreateFormCommand(_id, rawFields, rawButtons, rawSettings, responseInterceptor),
)

const fields = reactive(clonedNormalizedFields) as NormalizedFields<TypeFields>
Expand Down
5 changes: 3 additions & 2 deletions packages/vue/src/forms/typing/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
NormalizedFields,
NormalizedSettings,
RuleConfig,
} from '@fancy-crud/core';
} from '@fancy-crud/core'

export interface UseForm<T, U, S> {
id: symbol
Expand All @@ -16,12 +16,13 @@ export interface UseForm<T, U, S> {
bus: Bus
}

export interface Args<T, U, S> {
export interface Args<T, U, S, TypeResponseInterceptor> {
fields: T
id?: string
buttons?: U
settings?: S
rulesConfig?: RuleConfig
responseInterceptor?: TypeResponseInterceptor
}

export interface DefaultProps {
Expand Down

0 comments on commit d9f3c1f

Please sign in to comment.