diff --git a/example/src/Termination.tsx b/example/src/Termination.tsx index 0365e208..f93cb9cd 100644 --- a/example/src/Termination.tsx +++ b/example/src/Termination.tsx @@ -1,4 +1,8 @@ -import { TerminationFlow, zendeskArticles } from '@remoteoss/remote-flows'; +import { + TerminationFlow, + useGetOffboardings, + zendeskArticles, +} from '@remoteoss/remote-flows'; import type { TerminationRenderProps, TerminationFormValues, @@ -184,14 +188,31 @@ const TerminationRender = ({ ); }; -export const TerminationWithProps = ({ - employmentId, -}: { - employmentId: string; -}) => { - const proxyURL = window.location.origin; +const Termination = ({ employmentId }: { employmentId: string }) => { + const { data: totalOffboardings, isLoading: isLoadingOffboardings } = + useGetOffboardings({ + params: { + employmentId: employmentId, + includeConfidential: true, + }, + options: { + enabled: !!employmentId, + select: (data) => { + return data?.total_count; + }, + }, + }); + + if (isLoadingOffboardings) { + return
Loading offboardings...
; + } + + if (totalOffboardings && totalOffboardings > 0) { + return
You have already submitted a termination requests
; + } + return ( - + <> + + ); +}; + +export const TerminationWithProps = ({ + employmentId, +}: { + employmentId: string; +}) => { + const proxyURL = window.location.origin; + return ( + + ); }; diff --git a/src/flows/Termination/api.ts b/src/flows/Termination/api.ts index d5e44b89..781f6e67 100644 --- a/src/flows/Termination/api.ts +++ b/src/flows/Termination/api.ts @@ -1,5 +1,7 @@ import { CreateOffboardingParams, + getIndexOffboarding, + ListOffboardingResponse, postCreateOffboarding, Timeoff, } from '@/src/client'; @@ -316,3 +318,47 @@ export const useTerminationSchema = ({ }, }); }; + +/** + * Hook to retrieve list of offboardings or filter offboardings by employmentId. + * + * @param {Object} params - The parameters for the query. + * @param {string} [params.employmentId] - The ID of the employment to fetch offboardings for. + * @param {boolean} [params.includeConfidential] - Whether to include confidential offboardings in the response. + * @returns {TData} - The offboardings data. + */ +export const useGetOffboardings = ({ + params, + options, +}: { + params: Partial<{ + employmentId?: string; + includeConfidential?: boolean; + }>; + options?: Partial<{ + enabled?: boolean; + select?: (data: ListOffboardingResponse['data']) => TData; + }>; +}) => { + const { client } = useClient(); + return useQuery({ + queryKey: [ + 'rmt-flows-get-offboardings', + params.employmentId, + params.includeConfidential, + ], + queryFn: () => { + return getIndexOffboarding({ + client: client as Client, + query: { + employment_id: params.employmentId, + include_confidential: params.includeConfidential ? 'true' : 'false', + }, + }); + }, + select: ({ data }) => { + return (options?.select?.(data?.data) ?? data?.data) as TData; + }, + enabled: options?.enabled, + }); +}; diff --git a/src/index.tsx b/src/index.tsx index a96e1bfd..ffda905f 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -33,6 +33,8 @@ export { TerminationDialogInfoContent, } from '@/src/flows/Termination'; +export { useGetOffboardings } from '@/src/flows/Termination/api'; + export type { TerminationFormValues, TerminationFlowProps,