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
olav dbb62631a6 feat: add FeedbackCES component (#826)
* refactor: add screen-reader-only util class

* refactor: move FeedbackNPS component

* feat: add FeedbackCES component

* refactor: improve hidden checkbox styles

* refactor: fix IFeedbackEndpointRequestBody source type

* refactor: remove unnecessary event.persist() calls

* refactor: remove disableEscapeKeyDown from FeedbackCES modal

* refactor: make textarea label customizable

* refactor: store feedback state on the backend

* refactor: add FeedbackCESForm snapshot test

* refactor: use extant IAuthFeedback type

* refactor: fix showNPSFeedback logic for multiple feedback types
2022-03-31 09:23:46 +02:00

49 lines
1.4 KiB
TypeScript

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),
});
};