1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/frontend/src/hooks/api/actions/useAuthFeedbackApi/useAuthFeedbackApi.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

import { formatApiPath } from 'utils/formatPath';
import { useCallback } from 'react';
import { useAuthFeedback } from 'hooks/api/getters/useAuth/useAuthFeedback';
import { IAuthFeedback } from 'hooks/api/getters/useAuth/useAuthEndpoint';
interface IUseAuthFeedbackApi {
createFeedback: (feedback: IAuthFeedback) => Promise<void>;
updateFeedback: (feedback: IAuthFeedback) => Promise<void>;
}
export const useAuthFeedbackApi = (): IUseAuthFeedbackApi => {
const { refetchFeedback } = useAuthFeedback();
const path = formatApiPath('api/admin/feedback');
const createFeedback = useCallback(
async (feedback: IAuthFeedback): Promise<void> => {
await sendFeedback('POST', path, feedback);
await refetchFeedback();
},
[path, refetchFeedback]
);
const updateFeedback = useCallback(
async (feedback: IAuthFeedback): Promise<void> => {
const pathWithId = `${path}/${feedback.feedbackId}`;
await sendFeedback('PUT', pathWithId, feedback);
await refetchFeedback();
},
[path, refetchFeedback]
);
return {
createFeedback,
updateFeedback,
};
};
const sendFeedback = async (
method: 'PUT' | 'POST',
path: string,
feedback: IAuthFeedback
): Promise<void> => {
await fetch(path, {
method,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(feedback),
});
};