64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
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<UpdateClientResponse> | null;
|
|
updateClient: (
|
|
clientId: string,
|
|
payload: UpdateClientRequest,
|
|
) => Promise<void>;
|
|
}
|
|
|
|
export const useUpdateClient = (): UseUpdateClient => {
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [error, setError] = useState<ApiError | null>(null);
|
|
const [data, setData] = useState<APIResponse<UpdateClientResponse> | null>(
|
|
null,
|
|
);
|
|
|
|
const updateClient = useCallback(
|
|
(clientId: string, payload: UpdateClientRequest): Promise<void> => {
|
|
setIsLoading(true);
|
|
setError(null);
|
|
setData(null);
|
|
|
|
const url = `v1/client/${clientId}`;
|
|
|
|
return apiClient()
|
|
.patch<UpdateClientResponse>(url, payload)
|
|
.then(res => {
|
|
if (isApiResponse<UpdateClientResponse>(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 };
|
|
};
|