Repository navigation
DINO-1082: add discord announcement route and admin ui button #1060
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
Merged
Changes from all commits
Commits
Show all changes
3 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
This file contains hidden or 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
This file contains hidden or 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
117 changes: 117 additions & 0 deletions
117
js/src/app/admin/_components/announcements/NewDiscordAnnouncementModal.test.tsx
This file contains hidden or 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,117 @@ | ||
| import { | ||
| CREATE_DISCORD_ANNOUNCEMENT_FAILURE_MESSAGE, | ||
| CREATE_DISCORD_ANNOUNCEMENT_SUCCESS_MESSAGE, | ||
| CREATE_DISCORD_ANNOUNCEMENT_SERVER_ERROR_MESSAGE, | ||
| createDiscordAnnouncementPartialFailureHandler, | ||
| createDiscordAnnouncementServerErrorHandler, | ||
| createDiscordAnnouncementSuccessHandler, | ||
| } from "@/__mock__/admin"; | ||
| import NewDiscordAnnouncementModal from "@/app/admin/_components/announcements/NewDiscordAnnouncementModal"; | ||
| import { TestUtils, TestUtilTypes } from "@/lib/test"; | ||
| import { screen, waitFor } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { setupServer } from "msw/node"; | ||
|
|
||
| describe("NewDiscordAnnouncementModal", () => { | ||
| const server = setupServer(createDiscordAnnouncementSuccessHandler); | ||
| let requestCount = 0; | ||
| let requestBodies: unknown[] = []; | ||
|
|
||
| beforeAll(() => server.listen()); | ||
| afterEach(() => server.resetHandlers()); | ||
| afterAll(() => server.close()); | ||
|
|
||
| let renderProviderFn: TestUtilTypes.RenderWithAllProvidersFn | null = null; | ||
| beforeEach(() => { | ||
| renderProviderFn = TestUtils.getRenderWithAllProvidersFn(); | ||
| requestCount = 0; | ||
| requestBodies = []; | ||
| server.events.removeAllListeners(); | ||
| server.events.on("request:start", async ({ request }) => { | ||
| requestCount++; | ||
| requestBodies.push(await request.clone().json()); | ||
| }); | ||
| }); | ||
|
|
||
| const openModal = async (user: ReturnType<typeof userEvent.setup>) => { | ||
| await user.click( | ||
| screen.getByRole("button", { name: "New Discord Announcement" }), | ||
| ); | ||
| return screen.findByRole("textbox", { name: /Announcement message/ }); | ||
| }; | ||
|
|
||
| it("should be closed until the button is clicked", async () => { | ||
| renderProviderFn?.(<NewDiscordAnnouncementModal />); | ||
| const user = userEvent.setup(); | ||
|
|
||
| expect(screen.queryByRole("textbox")).not.toBeInTheDocument(); | ||
|
|
||
| expect(await openModal(user)).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByText("Create new discord announcement"), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("should not call the API when the message is only whitespace", async () => { | ||
| renderProviderFn?.(<NewDiscordAnnouncementModal />); | ||
| const user = userEvent.setup(); | ||
|
|
||
| const input = await openModal(user); | ||
| await user.type(input, " "); | ||
| await user.click(screen.getByRole("button", { name: "Submit" })); | ||
|
|
||
| expect(requestCount).toBe(0); | ||
| expect(input).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("should send the trimmed message, notify, close, and clear the field on success", async () => { | ||
| renderProviderFn?.(<NewDiscordAnnouncementModal />); | ||
| const user = userEvent.setup(); | ||
|
|
||
| const input = await openModal(user); | ||
| await user.type(input, " big news "); | ||
| await user.click(screen.getByRole("button", { name: "Submit" })); | ||
|
|
||
| expect( | ||
| await screen.findByText(CREATE_DISCORD_ANNOUNCEMENT_SUCCESS_MESSAGE), | ||
| ).toBeInTheDocument(); | ||
| expect(requestCount).toBe(1); | ||
| expect(requestBodies[0]).toEqual({ message: "big news" }); | ||
| await waitFor(() => { | ||
| expect(screen.queryByRole("textbox")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| expect(await openModal(user)).toHaveValue(""); | ||
| }); | ||
|
|
||
| it("should notify and keep the modal open when the API reports failure", async () => { | ||
| server.use(createDiscordAnnouncementPartialFailureHandler); | ||
| renderProviderFn?.(<NewDiscordAnnouncementModal />); | ||
| const user = userEvent.setup(); | ||
|
|
||
| const input = await openModal(user); | ||
| await user.type(input, "big news"); | ||
| await user.click(screen.getByRole("button", { name: "Submit" })); | ||
|
|
||
| expect( | ||
| await screen.findByText(CREATE_DISCORD_ANNOUNCEMENT_FAILURE_MESSAGE), | ||
| ).toBeInTheDocument(); | ||
| expect(input).toBeInTheDocument(); | ||
| expect(input).toHaveValue("big news"); | ||
| }); | ||
|
|
||
| it("should show the error message when the request fails", async () => { | ||
| server.use(createDiscordAnnouncementServerErrorHandler); | ||
| renderProviderFn?.(<NewDiscordAnnouncementModal />); | ||
| const user = userEvent.setup(); | ||
|
|
||
| const input = await openModal(user); | ||
| await user.type(input, "big news"); | ||
| await user.click(screen.getByRole("button", { name: "Submit" })); | ||
|
|
||
| expect( | ||
| await screen.findByText(CREATE_DISCORD_ANNOUNCEMENT_SERVER_ERROR_MESSAGE), | ||
| ).toBeInTheDocument(); | ||
| expect(input).toBeInTheDocument(); | ||
| }); | ||
| }); |
80 changes: 80 additions & 0 deletions
80
js/src/app/admin/_components/announcements/NewDiscordAnnouncementModal.tsx
This file contains hidden or 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,80 @@ | ||
| import { useCreateDiscordAnnouncementMutation } from "@/lib/api/queries/admin"; | ||
| import { newDiscordAnnouncementSchema } from "@/lib/api/schema/admin"; | ||
| import { Button, Modal, TextInput } from "@mantine/core"; | ||
| import { useForm } from "@mantine/form"; | ||
| import { notifications } from "@mantine/notifications"; | ||
| import { zodResolver } from "mantine-form-zod-resolver"; | ||
| import { useState } from "react"; | ||
| import { z } from "zod"; | ||
|
|
||
| export default function NewDiscordAnnouncementModal() { | ||
| const [isModalOpen, setModalOpen] = useState(false); | ||
| const { mutate, status } = useCreateDiscordAnnouncementMutation(); | ||
| const form = useForm({ | ||
| validate: zodResolver(newDiscordAnnouncementSchema), | ||
| initialValues: { | ||
| message: "", | ||
| }, | ||
| }); | ||
|
|
||
| const toggleModal = () => { | ||
| form.clearErrors(); | ||
| setModalOpen((prev) => !prev); | ||
| }; | ||
|
|
||
| const onSubmit = (data: z.infer<typeof newDiscordAnnouncementSchema>) => { | ||
| mutate( | ||
| { ...data, message: data.message.trim() }, | ||
| { | ||
| onSuccess: async (data) => { | ||
|
Check warning on line 29 in js/src/app/admin/_components/announcements/NewDiscordAnnouncementModal.tsx
|
||
| notifications.show({ | ||
| message: data.message, | ||
| color: data.success ? undefined : "red", | ||
| }); | ||
| if (data.success) { | ||
| form.reset(); | ||
| setModalOpen(false); | ||
| } | ||
| }, | ||
| onError: (error) => { | ||
| notifications.show({ | ||
| message: error.message, | ||
| color: "red", | ||
| }); | ||
| }, | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Button onClick={toggleModal} top={12}> | ||
| New Discord Announcement | ||
| </Button> | ||
| <Modal | ||
| opened={isModalOpen} | ||
| onClose={toggleModal} | ||
| title="Create new discord announcement" | ||
| > | ||
| <form onSubmit={form.onSubmit(onSubmit)}> | ||
| <TextInput | ||
| {...form.getInputProps("message")} | ||
| label="Announcement message" | ||
| error={form.errors.message} | ||
| withAsterisk | ||
| mb={"sm"} | ||
| /> | ||
| <Button | ||
| type="submit" | ||
| size="xs" | ||
| mt="sm" | ||
| variant="outline" | ||
| disabled={status === "pending"} | ||
| > | ||
| Submit | ||
| </Button> | ||
| </form> | ||
| </Modal> | ||
| </> | ||
| ); | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
19 changes: 19 additions & 0 deletions
19
...in/java/org/patinanetwork/codebloom/api/admin/body/jda/CreateDiscordAnnouncementBody.java
This file contains hidden or 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,19 @@ | ||
| package org.patinanetwork.codebloom.api.admin.body.jda; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Size; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @Builder | ||
| @AllArgsConstructor | ||
| public class CreateDiscordAnnouncementBody { | ||
|
|
||
| @NotBlank | ||
| @Size(min = 1, max = 4096) | ||
| private String message; | ||
|
Pendoof marked this conversation as resolved.
Resolved
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.