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

[Frontend] The loading state only last for 5 seconds when reporting #573

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
@@ -1,5 +1,5 @@
import { Dialog } from '@/features/shared'
import { FieldValues, useForm, UseFormGetValues } from 'react-hook-form'
import { FieldValues, useForm } from 'react-hook-form'
import { useReportComment } from '../../hooks/useReportComment/useReportComment'
import {
REGISTER_ID_DESC,
Expand All @@ -11,10 +11,9 @@ import {
ReportFormStepGroup,
ReportFormStepLabel,
ReportFormSubmitBtn,
ReportFormSubmitFailure,
ReportFormSubmitSuccess,
ReportFormSubmitting,
} from '../ReportForm'
import { useEffect, useState } from 'react'

interface CommentReportDialogProps {
postId: string
Expand All @@ -34,6 +33,8 @@ export function CommentReportDialog({
isOpen,
onClose,
}: CommentReportDialogProps) {
const [isSubmitting, setIsSubmitting] = useState(false)

const {
register,
handleSubmit,
Expand All @@ -47,8 +48,6 @@ export function CommentReportDialog({
const {
isIdle,
isPending,
isError,
isSuccess,
reportComment,
reset: resetState,
} = useReportComment()
Expand All @@ -64,22 +63,30 @@ export function CommentReportDialog({
} catch (_error) {}
})

const onFailureResubmit = useFailureResubmitDialogFlow({
resetForm,
resetState,
getValues,
})

const onCloseDialog = useCloseDialogFlow({
resetForm,
resetState,
onClose,
})

useEffect(() => {
if (isPending) {
setIsSubmitting(true)

const timer = setTimeout(() => {
onCloseDialog()
}, 5000)

return () => {
clearTimeout(timer)
}
}
}, [isPending, onCloseDialog])
Copy link
Collaborator

Choose a reason for hiding this comment

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

如果當前 epoch 已做過 user state transition 時,reportComment 的時間會少於 5 秒,因而導致 5 秒過後不會自動關閉 ReportFormSubmitting。


return (
<>
{isIdle && (
<Dialog isOpen={isOpen} onClose={onClose}>
<Dialog isOpen={isOpen} onClose={onCloseDialog}>
<ReportFormCtn onSubmit={onSubmit}>
<ReportFormIntro />
<ReportFormStepGroup>
Expand Down Expand Up @@ -109,14 +116,10 @@ export function CommentReportDialog({
</ReportFormCtn>
</Dialog>
)}
{isPending && <ReportFormSubmitting />}
{isError && (
<ReportFormSubmitFailure
onClose={onCloseDialog}
onResubmit={onFailureResubmit}
/>
)}
{isSuccess && <ReportFormSubmitSuccess onClose={onCloseDialog} />}
<ReportFormSubmitting
isOpen={isSubmitting}
onClose={onCloseDialog}
Copy link
Collaborator

Choose a reason for hiding this comment

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

按下右上角的 close button 時,沒有關閉 ReportFormSubmitting 哦

/>
</>
)
}
Expand All @@ -136,18 +139,3 @@ function useCloseDialogFlow({
onClose()
}
}

function useFailureResubmitDialogFlow({
resetForm,
resetState,
getValues,
}: {
resetForm: (values: FieldValues) => void
resetState: () => void
getValues: UseFormGetValues<FieldValues>
}) {
return () => {
resetForm(getValues())
resetState()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '@/features/post'
import { useQueryClient } from '@tanstack/react-query'
import { useEffect, useState } from 'react'
import toast from 'react-hot-toast'

export default function CreatePost({
disabled = false,
Expand All @@ -25,7 +24,6 @@ export default function CreatePost({

try {
await createPost(values)
toast('貼文成功送出')
} catch {
setIsSubmitting(false)
queryClient.setQueryData(['posts'], previousPostsData)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Dialog } from '@/features/shared'
import { FieldValues, useForm, UseFormGetValues } from 'react-hook-form'
import { useEffect, useState } from 'react'
import { FieldValues, useForm } from 'react-hook-form'
import { useReportPost } from '../../hooks/useReportPost/useReportPost'
import {
REGISTER_ID_DESC,
Expand All @@ -11,8 +12,6 @@ import {
ReportFormStepGroup,
ReportFormStepLabel,
ReportFormSubmitBtn,
ReportFormSubmitFailure,
ReportFormSubmitSuccess,
ReportFormSubmitting,
} from '../ReportForm'

Expand All @@ -32,6 +31,8 @@ export function PostReportDialog({
isOpen,
onClose,
}: PostReportDialogProps) {
const [isSubmitting, setIsSubmitting] = useState(false)

const {
register,
handleSubmit,
Expand All @@ -43,7 +44,6 @@ export function PostReportDialog({
} = useForm<FieldValues>({ defaultValues })

const {
isIdle,
isPending,
isError,
isSuccess,
Expand All @@ -61,59 +61,63 @@ export function PostReportDialog({
} catch (error) {}
})

const onFailureResubmit = useFailureResubmitDialogFlow({
resetForm,
resetState,
getValues,
})
const onCloseTransition = () => {
setIsSubmitting(false)
}

const onCloseDialog = useCloseDialogFlow({
resetForm,
resetState,
onClose,
onCloseTransition,
})

useEffect(() => {
if (isPending) {
onClose()
setIsSubmitting(true)
const timer = setTimeout(() => {
onCloseDialog()
}, 5000)

return () => {
clearTimeout(timer)
}
}
}, [isPending, onClose, onCloseDialog])

useEffect(() => {
if (isSuccess || isError) {
onCloseDialog()
}
}, [isSuccess, isError, onCloseDialog])

return (
<>
{isIdle && (
<Dialog isOpen={isOpen} onClose={onClose}>
<ReportFormCtn onSubmit={onSubmit}>
<ReportFormIntro />
<ReportFormStepGroup>
<ReportFormStepLabel
title="1. 檢舉原因"
isRequired
/>
<ReportFormCategories
register={register}
errors={errors}
setValue={setValue}
getValues={getValues}
trigger={trigger}
/>
</ReportFormStepGroup>
<ReportFormStepGroup>
<ReportFormStepLabel
title="2. 檢舉描述"
isRequired
/>
<ReportFormReason
register={register}
errors={errors}
/>
</ReportFormStepGroup>
<ReportFormSubmitBtn />
</ReportFormCtn>
</Dialog>
)}
{isPending && <ReportFormSubmitting />}
{isError && (
<ReportFormSubmitFailure
onClose={onCloseDialog}
onResubmit={onFailureResubmit}
/>
)}
{isSuccess && <ReportFormSubmitSuccess onClose={onCloseDialog} />}
<Dialog isOpen={isOpen} onClose={onCloseDialog}>
<ReportFormCtn onSubmit={onSubmit}>
<ReportFormIntro />
<ReportFormStepGroup>
<ReportFormStepLabel title="1. 檢舉原因" isRequired />
<ReportFormCategories
register={register}
errors={errors}
setValue={setValue}
getValues={getValues}
trigger={trigger}
/>
</ReportFormStepGroup>
<ReportFormStepGroup>
<ReportFormStepLabel title="2. 檢舉描述" isRequired />
<ReportFormReason register={register} errors={errors} />
</ReportFormStepGroup>
<ReportFormSubmitBtn />
</ReportFormCtn>
</Dialog>
<ReportFormSubmitting
isOpen={isSubmitting}
onClose={onCloseDialog}
/>
</>
)
}
Expand All @@ -122,29 +126,17 @@ function useCloseDialogFlow({
resetForm,
resetState,
onClose,
onCloseTransition,
}: {
resetForm: () => void
resetState: () => void
onClose: () => void
onCloseTransition: () => void
}) {
return () => {
resetForm()
resetState()
onClose()
}
}

function useFailureResubmitDialogFlow({
resetForm,
resetState,
getValues,
}: {
resetForm: (values: FieldValues) => void
resetState: () => void
getValues: UseFormGetValues<FieldValues>
}) {
return () => {
resetForm(getValues())
resetState()
onCloseTransition()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ jest.mock('../../ReportForm/ReportFormCategories', () => ({

const mockValues = {
isPending: false,
isIdle: false,
isSuccess: false,
isError: false,
reset: jest.fn(),
reportPost: jest.fn(),
}

describe('PostReportDialog', () => {
it('should render isIdle content', () => {
it('should render the initial form content', () => {
const mockUseReportPost = useReportPost as jest.MockedFunction<
typeof useReportPost
>
mockUseReportPost.mockReturnValue({ ...mockValues, isIdle: true })

mockUseReportPost.mockReturnValue({ ...mockValues })
render(
<PostReportDialog postId={''} isOpen={true} onClose={() => {}} />,
{ wrapper },
Expand All @@ -46,28 +46,4 @@ describe('PostReportDialog', () => {
)
expect(screen.getByText('您的檢舉報告正在送出中')).toBeInTheDocument()
})

it('should render isSuccess content', () => {
const mockUseReportPost = useReportPost as jest.MockedFunction<
typeof useReportPost
>
mockUseReportPost.mockReturnValue({ ...mockValues, isSuccess: true })
render(
<PostReportDialog postId={''} isOpen={true} onClose={() => {}} />,
{ wrapper },
)
expect(screen.getByText('您的檢舉報告傳送成功!')).toBeInTheDocument()
})

it('should render isError content', () => {
const mockUseReportPost = useReportPost as jest.MockedFunction<
typeof useReportPost
>
mockUseReportPost.mockReturnValue({ ...mockValues, isError: true })
render(
<PostReportDialog postId={''} isOpen={true} onClose={() => {}} />,
{ wrapper },
)
expect(screen.getByText('導致傳送失敗。')).toBeInTheDocument()
})
})
Loading
Loading