import { useState, useCallback } from 'react'; import { ApiError, APIResponse, apiClient, isApiResponse, isApiError, } from '..'; export interface UpdateClientRequest { client_name: string; redirect_uri: string; regenerate_secret: boolean; } export interface UpdateClientResponse { client_secret?: string; } interface UseUpdateClient { isLoading: boolean; error: ApiError | null; data: APIResponse | null; updateClient: ( clientId: string, payload: UpdateClientRequest, ) => Promise; } export const useUpdateClient = (): UseUpdateClient => { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [data, setData] = useState | null>( null, ); const updateClient = useCallback( (clientId: string, payload: UpdateClientRequest): Promise => { setIsLoading(true); setError(null); setData(null); const url = `v1/client/${clientId}`; return apiClient() .patch(url, payload) .then(res => { if (isApiResponse(res)) { setData(res); } else if (isApiError(res)) { setError(res); } else { setError({ message: 'Received unknown response structure.' }); } }) .catch(() => setError({ message: 'Network or unexpected error' })) .finally(() => setIsLoading(false)); }, [], ); return { isLoading, error, data, updateClient }; };