1+ import type { UseMutationOptions } from '@tanstack/react-query'
12import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query'
2- import { noop } from '@helpwave/hightide'
3- import {
4- CreatePropertyRequest ,
5- GetPropertiesRequest ,
6- GetPropertyRequest ,
7- UpdatePropertyRequest
8- } from '@helpwave/proto-ts/services/property_svc/v1/property_svc_pb'
9- import { FieldType } from '@helpwave/proto-ts/services/property_svc/v1/types_pb'
10- import { APIServices } from '../../services'
11- import { getAuthenticatedGrpcMetadata } from '../../authentication/grpc_metadata'
123import { QueryKeys } from '../query_keys'
13- import type { Property , SelectData , SelectOption , SubjectType } from '../../types/properties/property'
14- import { GRPCConverter } from '../../util/util '
4+ import type { Property , SelectOption , PropertySubjectType } from '../../types/properties/property'
5+ import { PropertyService } from '../../service/properties/PropertyService '
156
16- export const usePropertyListQuery = ( subjectType ?: SubjectType ) => {
7+ export const usePropertyListQuery = ( subjectType ?: PropertySubjectType ) => {
178 return useQuery ( {
189 queryKey : [ QueryKeys . properties , subjectType ?? 'all' ] ,
1910 queryFn : async ( ) : Promise < Property [ ] > => {
20- const req = new GetPropertiesRequest ( )
21- if ( subjectType ) {
22- req . setSubjectType ( GRPCConverter . subjectTypeMapperToGRPC ( subjectType ) )
23- }
24- const result = await APIServices . property . getProperties ( req , getAuthenticatedGrpcMetadata ( ) )
25- return result . getPropertiesList ( ) . filter ( value => value . getFieldType ( ) !== FieldType . FIELD_TYPE_UNSPECIFIED ) . map ( property => {
26- const fieldType = GRPCConverter . fieldTypeMapperFromGRPC ( property . getFieldType ( ) )
27- const selectData = property . getSelectData ( )
28- const mustHaveSelectData = fieldType === 'singleSelect' || fieldType === 'multiSelect'
29- if ( ! selectData && mustHaveSelectData ) {
30- throw Error ( 'usePropertyListQuery could not find selectData' )
31- }
32- return {
33- id : property . getId ( ) ,
34- name : property . getName ( ) ,
35- description : property . getDescription ( ) ,
36- subjectType : GRPCConverter . subjectTypeMapperFromGRPC ( property . getSubjectType ( ) ) ,
37- fieldType,
38- isArchived : property . getIsArchived ( ) ,
39- setId : property . getSetId ( ) ,
40- selectData : mustHaveSelectData ? {
41- isAllowingFreetext : selectData ! . getAllowFreetext ( ) ,
42- options : selectData ! . getOptionsList ( ) . map ( option => ( {
43- id : option . getId ( ) ,
44- name : option . getName ( ) ,
45- description : option . getDescription ( ) ,
46- isCustom : option . getIsCustom ( )
47- } ) )
48- } : undefined
49- }
50- } )
11+ return await PropertyService . getList ( subjectType )
5112 } ,
5213 } )
5314}
@@ -57,89 +18,22 @@ export const usePropertyQuery = (id?: string) => {
5718 queryKey : [ QueryKeys . properties , id ] ,
5819 enabled : ! ! id ,
5920 queryFn : async ( ) : Promise < Property > => {
60- if ( ! id ) {
61- throw Error ( 'usePropertyQuery no id in mutate' )
62- }
63- const req = new GetPropertyRequest ( )
64- req . setId ( id )
65-
66- const result = await APIServices . property . getProperty ( req , getAuthenticatedGrpcMetadata ( ) )
67-
68- const fieldType = GRPCConverter . fieldTypeMapperFromGRPC ( result . getFieldType ( ) )
69- let selectData : SelectData | undefined
70- if ( fieldType === 'singleSelect' || fieldType === 'multiSelect' ) {
71- const responseSelectData = result . getSelectData ( )
72- if ( ! responseSelectData ) {
73- throw Error ( 'usePropertyQuery could not find selectData' )
74- }
75- selectData = {
76- isAllowingFreetext : responseSelectData . getAllowFreetext ( ) ,
77- options : responseSelectData . getOptionsList ( ) . map ( option => ( {
78- id : option . getId ( ) ,
79- name : option . getName ( ) ,
80- description : option . getDescription ( ) ,
81- isCustom : option . getIsCustom ( )
82- } ) )
83- }
84- }
85- return {
86- id : result . getId ( ) ,
87- name : result . getName ( ) ,
88- description : result . getDescription ( ) ,
89- subjectType : GRPCConverter . subjectTypeMapperFromGRPC ( result . getSubjectType ( ) ) ,
90- fieldType,
91- isArchived : result . getIsArchived ( ) ,
92- setId : result . getSetId ( ) ,
93- alwaysIncludeForViewSource : result . getAlwaysIncludeForViewSource ( ) ,
94- selectData,
95- }
21+ return await PropertyService . get ( id ! )
9622 } ,
9723 } )
9824}
9925
100- export const usePropertyCreateMutation = ( callback : ( property : Property ) => void = noop ) => {
26+ export const usePropertyCreateMutation = ( options ?: UseMutationOptions < Property , unknown , Property > ) => {
10127 const queryClient = useQueryClient ( )
10228 return useMutation ( {
29+ ...options ,
10330 mutationFn : async ( property : Property ) => {
104- const req = new CreatePropertyRequest ( )
105- req . setName ( property . name )
106- if ( property . description ) {
107- req . setDescription ( property . description )
108- }
109- req . setSubjectType ( GRPCConverter . subjectTypeMapperToGRPC ( property . subjectType ) )
110- req . setFieldType ( GRPCConverter . fieldTypeMapperToGRPC ( property . fieldType ) )
111- if ( property . setId ) {
112- req . setSetId ( property . setId )
113- }
114- if ( property . fieldType === 'singleSelect' || property . fieldType === 'multiSelect' ) {
115- if ( ! property . selectData ) {
116- throw Error ( 'Select FieldType, but select data not set' )
117- }
118- const selectDataVal = new CreatePropertyRequest . SelectData ( )
119- selectDataVal . setAllowFreetext ( property . selectData . isAllowingFreetext )
120- selectDataVal . setOptionsList ( property . selectData . options . map ( option => {
121- const optionVal = new CreatePropertyRequest . SelectData . SelectOption ( )
122- optionVal . setName ( option . name )
123- if ( option . description ) {
124- optionVal . setDescription ( option . description )
125- }
126- return optionVal
127- } ) )
128- req . setSelectData ( selectDataVal )
129- }
130-
131- const result = await APIServices . property . createProperty ( req , getAuthenticatedGrpcMetadata ( ) )
132-
133- const id = result . getPropertyId ( )
134-
135- const newValue = {
136- ...property ,
137- id
138- }
139- callback ( newValue )
140- return newValue
31+ return await PropertyService . create ( property )
14132 } ,
142- onSuccess : ( ) => {
33+ onSuccess : ( data , variables , context ) => {
34+ if ( options ?. onSuccess ) {
35+ options . onSuccess ( data , variables , context )
36+ }
14337 queryClient . invalidateQueries ( [ QueryKeys . properties ] ) . catch ( console . error )
14438 }
14539 } )
@@ -156,62 +50,17 @@ export type PropertyUpdateType = {
15650 selectUpdate ?: PropertySelectDataUpdate ,
15751}
15852
159- export const usePropertyUpdateMutation = ( callback : ( property : Property ) => void = noop ) => {
53+ export const usePropertyUpdateMutation = ( options ?: UseMutationOptions < boolean , unknown , PropertyUpdateType > ) => {
16054 const queryClient = useQueryClient ( )
16155 return useMutation ( {
162- mutationFn : async ( { property, selectUpdate } : PropertyUpdateType ) => {
163- const req = new UpdatePropertyRequest ( )
164- req . setId ( property . id )
165- req . setName ( property . name )
166- req . setIsArchived ( property . isArchived )
167- req . setSubjectType ( GRPCConverter . subjectTypeMapperToGRPC ( property . subjectType ) )
168- if ( property . description ) {
169- req . setDescription ( property . description )
170- }
171- if ( property . setId ) {
172- req . setSetId ( property . setId )
173- }
174- if ( property . fieldType === 'singleSelect' || property . fieldType === 'multiSelect' ) {
175- if ( ! property . selectData ) {
176- throw Error ( 'Select FieldType, but select data not set' )
177- }
178- const selectDataVal = new UpdatePropertyRequest . SelectData ( )
179- selectDataVal . setAllowFreetext ( property . selectData . isAllowingFreetext )
180- if ( selectUpdate ) {
181- const createList = selectUpdate . add . map ( option => {
182- const optionVal = new UpdatePropertyRequest . SelectData . SelectOption ( )
183- optionVal . setId ( '' )
184- optionVal . setName ( option . name )
185- if ( option . description ) {
186- optionVal . setDescription ( option . description )
187- }
188- optionVal . setIsCustom ( option . isCustom )
189- return optionVal
190- } )
191- const updateList = selectUpdate . update . map ( option => {
192- const optionVal = new UpdatePropertyRequest . SelectData . SelectOption ( )
193- optionVal . setId ( option . id )
194- optionVal . setName ( option . name )
195- if ( option . description ) {
196- optionVal . setDescription ( option . description )
197- }
198- optionVal . setIsCustom ( option . isCustom )
199- return optionVal
200- } )
201- selectDataVal . setUpsertOptionsList ( [ ...updateList , ...createList ] )
202- selectDataVal . setRemoveOptionsList ( selectUpdate . remove )
203- }
204- req . setSelectData ( selectDataVal )
205- }
206-
207- const result = await APIServices . property . updateProperty ( req , getAuthenticatedGrpcMetadata ( ) )
208- if ( ! result . toObject ( ) ) {
209- throw Error ( 'usePropertyUpdateMutation: error in result' )
210- }
211- callback ( property )
212- return property
56+ ...options ,
57+ mutationFn : async ( props ) => {
58+ return await PropertyService . update ( props )
21359 } ,
214- onSuccess : ( ) => {
60+ onSuccess : ( data , variables , context ) => {
61+ if ( options ?. onSuccess ) {
62+ options . onSuccess ( data , variables , context )
63+ }
21564 queryClient . invalidateQueries ( [ QueryKeys . properties ] ) . catch ( console . error )
21665 }
21766 } )
0 commit comments