Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add message banner API hooks #5078

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { IInternalMessageBanner } from 'interfaces/messageBanner';
import useAPI from '../useApi/useApi';

const ENDPOINT = 'api/admin/message-banners';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, should we just call them "banners" instead of "message-banners"? I feel we might regret in the future for not giving it a more general name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see "banner" as the type of component itself. Right now we have:

  • DraftBanner (CR)
  • MaintenanceBanner
  • DemoBanner

So I think MessageBanner (or in this case, message-banners) is a good representation of what it does. Do you think it's worth making this rename now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you know more than I do, so I trust your judgment. It was just an observation because message-banner seems weird... a banner is by definition a message you want to deliver... the fact that it's a "message banner" type is an implementation detail IMO. But again, take it for what it is: opinions without a lot of context :D


type AddOrUpdateMessageBanner = Omit<
IInternalMessageBanner,
'id' | 'createdAt'
>;

export const useMessageBannersApi = () => {
const { loading, makeRequest, createRequest, errors } = useAPI({
propagateErrors: true,
});

const addMessageBanner = async (
messageBanner: AddOrUpdateMessageBanner,
) => {
const requestId = 'addMessageBanner';
const req = createRequest(
ENDPOINT,
{
method: 'POST',
body: JSON.stringify(messageBanner),
},
requestId,
);

const response = await makeRequest(req.caller, req.id);
return response.json();
};

const updateMessageBanner = async (
messageBannerId: number,
messageBanner: AddOrUpdateMessageBanner,
) => {
const requestId = 'updateMessageBanner';
const req = createRequest(
`${ENDPOINT}/${messageBannerId}`,
{
method: 'PUT',
body: JSON.stringify(messageBanner),
},
requestId,
);

await makeRequest(req.caller, req.id);
};

const removeMessageBanner = async (messageBannerId: number) => {
const requestId = 'removeMessageBanner';
const req = createRequest(
`${ENDPOINT}/${messageBannerId}`,
{ method: 'DELETE' },
requestId,
);

await makeRequest(req.caller, req.id);
};

return {
addMessageBanner,
updateMessageBanner,
removeMessageBanner,
errors,
loading,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useMemo } from 'react';
import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler';
import { useConditionalSWR } from '../useConditionalSWR/useConditionalSWR';
import useUiConfig from '../useUiConfig/useUiConfig';
import { useUiFlag } from 'hooks/useUiFlag';
import { IInternalMessageBanner } from 'interfaces/messageBanner';

const ENDPOINT = 'api/admin/message-banners';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is re-defined here, I believe you can just export this const in the API definition


export const useMessageBanners = () => {
const { isEnterprise } = useUiConfig();
const internalMessageBanners = useUiFlag('internalMessageBanners');

const { data, error, mutate } = useConditionalSWR(
isEnterprise() && internalMessageBanners,
{ messageBanners: [] },
formatApiPath(ENDPOINT),
fetcher,
);

return useMemo(
() => ({
messageBanners: (data?.messageBanners ??
[]) as IInternalMessageBanner[],
loading: !error && !data,
refetch: () => mutate(),
error,
}),
[data, error, mutate],
);
};

const fetcher = (path: string) => {
return fetch(path)
.then(handleErrorResponses('Message Banners'))
.then((res) => res.json());
};