feat(5.20): Custom Fields — plugin-defined fields in Contact UI
- Add CustomFieldDefinition to PluginManifest (text/number/date/select/multiselect/boolean)
- Add GET/PATCH /api/v1/contacts/{id}/custom-fields routes
- Merge plugin definitions with stored values in contacts.custom JSONB
- Add CustomFieldRenderer component (read + edit modes)
- Integrate into ContactDetail (read-only) and ContactEditModal (editable)
- Add custom_fields to pluginStore and active manifests API
- Add useCustomFields/useUpdateCustomFields React Query hooks
- Tests: test_custom_fields.py (9 tests) + CustomFieldRenderer.test.tsx (5 tests)
- i18n keys already present (contacts.customFields)
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Custom fields API hooks — merge plugin definitions with stored values.
|
||||
*/
|
||||
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { apiGet, apiPatch } from './client';
|
||||
|
||||
export interface CustomFieldDefinition {
|
||||
name: string;
|
||||
label: string;
|
||||
label_key: string;
|
||||
field_type: 'text' | 'number' | 'date' | 'select' | 'multiselect' | 'boolean';
|
||||
options: string[];
|
||||
default_value: any;
|
||||
required: boolean;
|
||||
entity: string;
|
||||
plugin: string;
|
||||
value: any;
|
||||
}
|
||||
|
||||
export interface CustomFieldsResponse {
|
||||
fields: CustomFieldDefinition[];
|
||||
}
|
||||
|
||||
export function useCustomFields(contactId?: string) {
|
||||
return useQuery({
|
||||
queryKey: ['custom-fields', contactId],
|
||||
queryFn: () => apiGet<CustomFieldsResponse>(`/contacts/${contactId}/custom-fields`),
|
||||
enabled: !!contactId,
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateCustomFields() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ contactId, values }: { contactId: string; values: Record<string, any> }) =>
|
||||
apiPatch<CustomFieldsResponse>(`/contacts/${contactId}/custom-fields`, { values }),
|
||||
onSuccess: (_data, variables) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['custom-fields', variables.contactId] });
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user