testing setup
This commit is contained in:
58
src/lib/hooks.ts
Normal file
58
src/lib/hooks.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
'use client'
|
||||
|
||||
import type { UseTRPCQueryResult } from "node_modules/@trpc/react-query/dist/getQueryKey.d-CruH3ncI.mjs"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
function useRelationShipSuccess<T extends Record<string,any> & {id:string},K extends keyof T>(
|
||||
relationShipData: T[] | undefined,
|
||||
relationShipNameIndex: K,
|
||||
setRelationShipId: (arg0: string | undefined) => void,
|
||||
setRelationShipName: (arg0: string | undefined) => void,
|
||||
realtionShipDataSuccess: boolean,
|
||||
id: string|undefined,
|
||||
nameCallBack?: (arg0:T[K]) => string,
|
||||
){
|
||||
useEffect(() => {
|
||||
if (id === undefined) {
|
||||
return
|
||||
}
|
||||
setRelationShipId(relationShipData?.at(0)?.id)
|
||||
if (relationShipData !== undefined && relationShipData[0] !==undefined) {
|
||||
|
||||
if (relationShipData[0][relationShipNameIndex] !== null) {
|
||||
if (nameCallBack !== undefined) {
|
||||
setRelationShipName(nameCallBack(relationShipData[0][relationShipNameIndex]))
|
||||
return
|
||||
}
|
||||
setRelationShipName(relationShipData[0][relationShipNameIndex])
|
||||
}
|
||||
}
|
||||
}, [realtionShipDataSuccess])
|
||||
}
|
||||
|
||||
export function useRelationShip<T extends Record<string,any> & {id: string},TE>(
|
||||
queryResult: UseTRPCQueryResult<T[],TE>,
|
||||
relationShipNameIndex: keyof T,
|
||||
dependsOnId:string|undefined,
|
||||
) {
|
||||
const [id, setRelationShipId] = useState<string | undefined>()
|
||||
const [name, setRealtionShipName] = useState<string | undefined>()
|
||||
const { data: data, isSuccess: success } = queryResult
|
||||
useRelationShipSuccess(data,relationShipNameIndex,setRelationShipId,setRealtionShipName,success,dependsOnId)
|
||||
return {data,id,name,success}
|
||||
}
|
||||
|
||||
export function makeUseRelationShipWithNameIndex<K extends string>(key:K) {
|
||||
return function useRelationShip<T extends Record<K, any> & { id: string },TE>(
|
||||
queryResult: UseTRPCQueryResult<T[],TE>,
|
||||
dependsOnId:string|undefined,
|
||||
nameCallBack: (arg0:T[K]) => string
|
||||
) {
|
||||
const [id, setRelationShipId] = useState<string | undefined>()
|
||||
const [name, setRealtionShipName] = useState<string | undefined>()
|
||||
const { data: data, isSuccess: success, error } = queryResult
|
||||
useRelationShipSuccess(data,key,setRelationShipId,setRealtionShipName,success,dependsOnId,nameCallBack)
|
||||
return {data,error,id,name,success}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user