-
Notifications
You must be signed in to change notification settings - Fork 3
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(ui): userPrompt
#713
Merged
Merged
feat(ui): userPrompt
#713
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d90a5d8
feat: create `userPrompt` observer and tests
asharonbaltazar dd80531
chore: replace modal context logic with `userPrompt`
asharonbaltazar d6c8afb
chore: remove modal context logic from project; replace with `UserPro…
asharonbaltazar a3dd41d
chore: rename `ConfirmationModal` to `ConfirmationDialog`
asharonbaltazar 9ba4c3d
chore: `activeuserPrompt` -> `activerUserPrompt`
asharonbaltazar 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 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 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,2 @@ | ||
export * from "./ConfirmationDialog"; | ||
export * from "./userPrompt"; |
131 changes: 131 additions & 0 deletions
131
react-app/src/components/ConfirmationDialog/userPrompt.test.tsx
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,131 @@ | ||
import { act, render } from "@testing-library/react"; | ||
import { describe, expect, test, vi } from "vitest"; | ||
import { UserPrompt, userPrompt } from "./userPrompt"; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
describe("userPrompt", () => { | ||
test("Hidden on initial render", () => { | ||
const { container } = render(<UserPrompt />); | ||
|
||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
test("Create a simple user prompt", () => { | ||
const { getByTestId } = render(<UserPrompt />); | ||
|
||
act(() => { | ||
userPrompt({ | ||
header: "Testing", | ||
body: "testing", | ||
onAccept: vi.fn(), | ||
}); | ||
}); | ||
|
||
expect(getByTestId("dialog-content")).toBeInTheDocument(); | ||
}); | ||
|
||
test("User prompt header matches", () => { | ||
const { getByTestId } = render(<UserPrompt />); | ||
|
||
act(() => { | ||
userPrompt({ | ||
header: "Testing", | ||
body: "testing body", | ||
onAccept: vi.fn(), | ||
}); | ||
}); | ||
|
||
expect(getByTestId("dialog-title")).toHaveTextContent("Testing"); | ||
}); | ||
|
||
test("User prompt body matches", () => { | ||
const { getByTestId } = render(<UserPrompt />); | ||
|
||
act(() => { | ||
userPrompt({ | ||
header: "Testing", | ||
body: "testing body", | ||
onAccept: vi.fn(), | ||
}); | ||
}); | ||
|
||
expect(getByTestId("dialog-body")).toHaveTextContent("testing body"); | ||
}); | ||
|
||
test("Clicking Accept successfully closes the user prompt", async () => { | ||
const user = userEvent.setup(); | ||
|
||
const { container, getByTestId } = render(<UserPrompt />); | ||
|
||
act(() => { | ||
userPrompt({ | ||
header: "Testing", | ||
body: "testing body", | ||
onAccept: vi.fn(), | ||
}); | ||
}); | ||
|
||
user.click(getByTestId("dialog-accept")); | ||
|
||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
test("Clicking Cancel successfully closes the user prompt", async () => { | ||
const user = userEvent.setup(); | ||
|
||
const { container, getByTestId } = render(<UserPrompt />); | ||
|
||
act(() => { | ||
userPrompt({ | ||
header: "Testing", | ||
body: "testing body", | ||
onAccept: vi.fn(), | ||
}); | ||
}); | ||
|
||
await user.click(getByTestId("dialog-cancel")); | ||
|
||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
test("Clicking Accept successfully calls the onAccept callback", async () => { | ||
const user = userEvent.setup(); | ||
|
||
const { getByTestId } = render(<UserPrompt />); | ||
|
||
const mockOnAccept = vi.fn(() => {}); | ||
|
||
act(() => { | ||
userPrompt({ | ||
header: "Testing", | ||
body: "testing body", | ||
onAccept: mockOnAccept, | ||
}); | ||
}); | ||
|
||
await user.click(getByTestId("dialog-accept")); | ||
|
||
expect(mockOnAccept).toHaveBeenCalled(); | ||
}); | ||
|
||
test("Clicking Cancel successfully calls the onCancel callback", async () => { | ||
const user = userEvent.setup(); | ||
|
||
const { getByTestId } = render(<UserPrompt />); | ||
|
||
const mockOnCancel = vi.fn(() => {}); | ||
|
||
act(() => { | ||
userPrompt({ | ||
header: "Testing", | ||
body: "testing body", | ||
onAccept: vi.fn(), | ||
onCancel: mockOnCancel, | ||
}); | ||
}); | ||
|
||
await user.click(getByTestId("dialog-cancel")); | ||
|
||
expect(mockOnCancel).toHaveBeenCalled(); | ||
}); | ||
}); |
106 changes: 106 additions & 0 deletions
106
react-app/src/components/ConfirmationDialog/userPrompt.tsx
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,106 @@ | ||
import { useEffect, useState } from "react"; | ||
import { ConfirmationDialog } from "@/components/ConfirmationDialog"; | ||
|
||
export type UserPrompt = { | ||
header: string; | ||
body: string; | ||
cancelButtonText?: string; | ||
acceptButtonText?: string; | ||
areButtonsReversed?: boolean; | ||
onAccept: () => void; | ||
onCancel?: () => void; | ||
}; | ||
|
||
class Observer { | ||
subscribers: Array<(userPrompt: UserPrompt) => void>; | ||
userPrompt: UserPrompt | null; | ||
|
||
constructor() { | ||
this.subscribers = []; | ||
this.userPrompt = null; | ||
} | ||
|
||
subscribe = (subscriber: (userPrompt: UserPrompt | null) => void) => { | ||
this.subscribers.push(subscriber); | ||
|
||
return () => { | ||
const index = this.subscribers.indexOf(subscriber); | ||
this.subscribers.splice(index, 1); | ||
}; | ||
}; | ||
|
||
private publish = (data: UserPrompt | null) => { | ||
this.subscribers.forEach((subscriber) => subscriber(data)); | ||
}; | ||
|
||
create = (data: UserPrompt) => { | ||
this.publish(data); | ||
this.userPrompt = { ...data }; | ||
}; | ||
|
||
dismiss = () => { | ||
this.publish(null); | ||
this.userPrompt = null; | ||
}; | ||
} | ||
|
||
const userPromptState = new Observer(); | ||
|
||
export const userPrompt = (newuserPrompt: UserPrompt) => { | ||
return userPromptState.create(newuserPrompt); | ||
}; | ||
|
||
export const UserPrompt = () => { | ||
const [activeUserPrompt, setActiveUserPrompt] = useState<UserPrompt | null>( | ||
null, | ||
); | ||
const [isOpen, setIsOpen] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
const unsubscribe = userPromptState.subscribe((userPrompt) => { | ||
if (userPrompt) { | ||
setActiveUserPrompt(userPrompt); | ||
setIsOpen(true); | ||
} else { | ||
// artificial delay to prevent content from disappearing first | ||
setTimeout(() => setActiveUserPrompt(null), 100); | ||
setIsOpen(false); | ||
} | ||
}); | ||
|
||
return unsubscribe; | ||
}, []); | ||
|
||
const onCancel = () => { | ||
if (activeUserPrompt) { | ||
if (activeUserPrompt.onCancel) { | ||
activeUserPrompt.onCancel(); | ||
} | ||
userPromptState.dismiss(); | ||
} | ||
}; | ||
|
||
const onAccept = () => { | ||
if (activeUserPrompt) { | ||
activeUserPrompt.onAccept(); | ||
userPromptState.dismiss(); | ||
} | ||
}; | ||
|
||
if (activeUserPrompt === null) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<ConfirmationDialog | ||
open={isOpen} | ||
title={activeUserPrompt.header} | ||
body={activeUserPrompt.body} | ||
onAccept={onAccept} | ||
onCancel={onCancel} | ||
cancelButtonText={activeUserPrompt.cancelButtonText} | ||
acceptButtonText={activeUserPrompt.acceptButtonText} | ||
areButtonsReversed={activeUserPrompt.areButtonsReversed} | ||
/> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from "./userContext"; | ||
export * from "./alertContext"; | ||
export * from "./modalContext"; | ||
export * from "./alertContext"; |
This file was deleted.
Oops, something went wrong.
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.
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.
Function
UserPrompt
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.