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

[Fix] 인증 실패 메시지 뜨는 문제 수정 #508

Merged
merged 1 commit into from
Feb 10, 2024
Merged
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
56 changes: 23 additions & 33 deletions src/app/record/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,13 @@ import { checkImageType, uploadImageToServer, useImage } from '@/hooks/useImage'
import useModal from '@/hooks/useModal';
import { eventLogger } from '@/utils';
import { css } from '@styled-system/css';
import { useMutation } from '@tanstack/react-query';

export default function MissionRecordPage() {
const router = useRouter();
const { triggerSnackBar } = useSnackBar();
const params = useParams();
const { mutateAsync: uploadComplete, isPending: isUploadCompletePending } = useMutation({
mutationFn: RECORD_API.uploadComplete,
onSuccess: () => {
router.replace(ROUTER.RECORD.SUCCESS);
},
onError: () => {
triggerSnackBar({
message: '미션 인증에 실패했습니다. 다시 시도해주세요.',
});
},
});

const { mutateAsync: uploadImageToServerMutate, isPending: isUpLoadingPending } = useMutation({
mutationFn: ({ missionId, imageFile }: { missionId: string; imageFile: File }) =>
uploadImageToServer(missionId, imageFile),
onSuccess: async ({ imageFileExtension }) => {
await uploadComplete({ missionRecordId: missionId, imageFileExtension, remark });
},
onError: () => {
triggerSnackBar({
message: '미션 인증에 실패했습니다. 다시 시도해주세요.',
});
},
});

const [isSubmitLoading, setIsSubmitLoading] = useState(false);

const missionId = params.id as string;

Expand All @@ -55,8 +32,6 @@ export default function MissionRecordPage() {

const { uploadImageChange, imagePreview, imageFile } = useImage();

const isPending = isUpLoadingPending || isUploadCompletePending;

const onChangeText = (e: ChangeEvent<HTMLTextAreaElement>) => {
// TODO: 200자 제한
setRemark(e.target.value);
Expand All @@ -75,22 +50,37 @@ export default function MissionRecordPage() {
}

eventLogger.logEvent(EVENT_LOG_NAME.CERTIFICATION.CLICK_CONFIRM, EVENT_LOG_CATEGORY.CERTIFICATION, { remark });
await uploadImageToServerMutate({
missionId,
imageFile,
});

await onSubmit(missionId, imageFile);
};

const isButtonDisabled = !imagePreview || isPending;
const isButtonDisabled = !imagePreview || isSubmitLoading;

const onImageClick = () => {
imageRef.current?.click();
};

// react query 지우로 axios 호출
const onSubmit = async (_missionId: string, _imageFile: File) => {
setIsSubmitLoading(true);

try {
const { imageFileExtension } = await uploadImageToServer(_missionId, _imageFile);
await RECORD_API.uploadComplete({ missionRecordId: _missionId, imageFileExtension, remark });

router.replace(ROUTER.RECORD.SUCCESS);
} catch (e) {
triggerSnackBar({
message: '미션 인증에 실패했습니다. 다시 시도해주세요.',
});
setIsSubmitLoading(false);
}
};

return (
<main className={mainWrapperCss}>
<div className={headerWrapperCss}>
{isPending && <Loading />}
{isSubmitLoading && <Loading />}
<div className={headerTitleCss}>미션 인증</div>
<div className={headerRightCss} onClick={openModal}>
<Icon name="normal-close" />
Expand Down
Loading