-
-
Notifications
You must be signed in to change notification settings - Fork 740
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
frontend/src/hooks/api/actions/useMessageBannersApi/useMessageBannersApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
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, | ||
}; | ||
}; |
38 changes: 38 additions & 0 deletions
38
frontend/src/hooks/api/getters/useMessageBanners/useMessageBanners.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
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?There was a problem hiding this comment.
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