-
Notifications
You must be signed in to change notification settings - Fork 1
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
[ Feature/dev 112] 문제풀기 Floating Button 구현 #173
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e81f6a5
feat: article to problem floating button
soomin9106 bfcc8a8
fix: topbar button area fix
soomin9106 8ec4a8a
feat: set maxAge to 60 days
soomin9106 11a5ce2
test: add ArticleFloatingButton test
soomin9106 de0072a
refactor: Add function name in useEffect
soomin9106 87e5d4d
refactor: specify button type
soomin9106 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
65 changes: 65 additions & 0 deletions
65
src/article/components/ArticleFloatingButton/ArticleFloatingButton.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,65 @@ | ||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import ArticleFloatingButton from "."; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
const push = vi.fn(); | ||
const setProblemIds = vi.fn(); | ||
|
||
describe("ArticleFloatingButton", () => { | ||
beforeAll(() => { | ||
vi.mock("next/navigation", async () => { | ||
const actual = | ||
await vi.importActual<typeof import("next/navigation")>( | ||
"next/navigation", | ||
); | ||
return { | ||
...actual, | ||
useParams: vi.fn(() => ({ | ||
get: vi.fn().mockReturnValueOnce(() => "1"), | ||
})), | ||
useRouter: vi.fn(() => ({ | ||
push, | ||
})), | ||
}; | ||
}); | ||
|
||
vi.mock("@shared/models/useProblemIdsViewModel", async () => { | ||
const actual = await vi.importActual< | ||
typeof import("@shared/models/useProblemIdsViewModel") | ||
>("@shared/models/useProblemIdsViewModel"); | ||
return { | ||
...actual, | ||
useProblemIdsViewModel: vi.fn(() => ({ | ||
isExistNextProblem: vi.fn(), | ||
nextSetProblemId: vi.fn(), | ||
clearProblem: vi.fn(), | ||
setProblemIds, | ||
getCurrentProblemId: vi.fn().mockReturnValueOnce("1"), | ||
getTagCurrentProblemText: vi.fn(), | ||
currentIdx: 0, | ||
prevSetProblemId: vi.fn(), | ||
getArticlePathText: vi.fn(), | ||
getDayText: vi.fn(), | ||
})), | ||
}; | ||
}); | ||
}); | ||
beforeEach(() => { | ||
render(<ArticleFloatingButton />); | ||
}); | ||
|
||
it("버튼이 잘 렌더링 되는지 테스트 한다.", () => { | ||
const button = screen.getByText("퀴즈 풀기"); | ||
expect(button).toBeInTheDocument(); | ||
}); | ||
|
||
it("플로팅 버튼 클릭 시 문제 풀기로 넘어간다.", async () => { | ||
const button = screen.getByText("퀴즈 풀기"); | ||
await userEvent.click(button); | ||
|
||
// Ensure that the router's push method was called with the correct path | ||
expect(push).toHaveBeenCalledWith("/problem/1"); | ||
}); | ||
}); |
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,65 @@ | ||
"use client"; | ||
|
||
import { useParams, useRouter } from "next/navigation"; | ||
|
||
import { useEffect, useState } from "react"; | ||
|
||
import { Button } from "@shared/components/ui/button"; | ||
import { useProblemIdsViewModel } from "@shared/models/useProblemIdsViewModel"; | ||
import { cn } from "@shared/utils/cn"; | ||
|
||
import ProblemIcon from "public/assets/icon/problemIcon.svg"; | ||
|
||
export default function ArticleFloatingButton() { | ||
const { articleId } = useParams<{ articleId: string }>(); | ||
const { push } = useRouter(); | ||
const { getCurrentProblemId } = useProblemIdsViewModel(); | ||
|
||
const [showButton, setShowButton] = useState(true); | ||
|
||
const onClickGoProblem = () => { | ||
push(`/problem/${getCurrentProblemId()}`); | ||
// Mixpanel.track({ | ||
// name: EVENT_NAME.ARTICLE_PROBLEMBUTTON_TAPPED, | ||
// property: { id: articleId }, | ||
// }); | ||
}; | ||
|
||
useEffect(function handleButton () { | ||
const handleShowButton = () => { | ||
const scrollY = window.scrollY; | ||
const viewportHeight = window.innerHeight; | ||
const documentHeight = document.documentElement.scrollHeight; | ||
|
||
if (scrollY + viewportHeight >= documentHeight - 1) { | ||
setShowButton(false); | ||
} else { | ||
setShowButton(true); | ||
} | ||
}; | ||
|
||
window.addEventListener("scroll", handleShowButton); | ||
return () => { | ||
window.removeEventListener("scroll", handleShowButton); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<> | ||
{showButton && ( | ||
<div className="fixed bottom-[48px] left-[60%] z-10 w-fit"> | ||
<Button | ||
className={cn( | ||
"h-[55px] px-5 py-3.5 bg-main rounded-[99px] shadow flex justify-center items-center gap-2" | ||
)} | ||
onClick={onClickGoProblem} | ||
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. button type 명시해주면 시멘틱스러울 것 같습니다! |
||
type="button" | ||
> | ||
<ProblemIcon /> | ||
<span className="text-base font-bold text-white">퀴즈 풀기</span> | ||
</Button> | ||
</div> | ||
)} | ||
</> | ||
); | ||
} |
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
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.
혹시 여기 bottom은 px로 하고 left는 %로 사용하신 이유가 있을 까요?