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] 미션인증 사진 업로드시 로딩 인디케이터 표시 #462

Merged
merged 2 commits into from
Feb 4, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/apis/record.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import getQueryKey from '@/apis/getQueryKey';
import { type RecordType } from '@/apis/schema/record';
import { type UploadBaseRequest } from '@/apis/schema/upload';
import { type ImageFileExtensionType, type UploadBaseRequest } from '@/apis/schema/upload';
import {
useMutation,
type UseMutationOptions,
Expand Down Expand Up @@ -35,7 +35,7 @@ interface UploadUrlResponse {

interface UploadCompleteRequest {
missionRecordId: string;
imageFileExtension: string;
imageFileExtension: ImageFileExtensionType;
remark: string;
}

Expand Down
20 changes: 2 additions & 18 deletions src/app/mypage/profile_modify/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Dialog from '@/components/Dialog/Dialog';
import Header from '@/components/Header/Header';
import Icon from '@/components/Icon';
import Input from '@/components/Input/Input';
import LoadingSpinner from '@/components/Loading/LoadingSpinner';
import Loading from '@/components/Loading';
import { useSnackBar } from '@/components/SnackBar/SnackBarProvider';
import Thumbnail from '@/components/Thumbnail/Thumbnail';
import { ROUTER } from '@/constants/router';
Expand Down Expand Up @@ -145,11 +145,7 @@ function ProfileModifyPage() {

<main className={mainCss}>
<div className={dimCss} />
{isImageUploadLoading && (
<div className={loadingCss}>
<LoadingSpinner />
</div>
)}
{isImageUploadLoading && <Loading />}
<section className={myTabContainerCss}>
<section className={thumbnailWrapperCss} onClick={handleImageClick}>
<input
Expand Down Expand Up @@ -225,18 +221,6 @@ const backgroundCss = css({
position: 'relative',
});

const loadingCss = css({
position: 'absolute',
top: 0,
zIndex: 'appBar',
display: 'flex',
width: '100%',
background: 'linear-gradient(180deg, rgba(0, 0, 0, 0.34) 0%, rgba(0, 0, 0, 0.15) 100%)',
height: '100vh',
justifyContent: 'center',
alignItems: 'center',
});

const dimCss = css({
position: 'absolute',
width: '100%',
Expand Down
56 changes: 40 additions & 16 deletions src/app/record/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,46 @@ import RECORD_API from '@/apis/record';
import Button from '@/components/Button/Button';
import Dialog from '@/components/Dialog/Dialog';
import Icon from '@/components/Icon';
import Loading from '@/components/Loading';
import { type ModalProps } from '@/components/Modal/Modal';
import { useSnackBar } from '@/components/SnackBar/SnackBarProvider';
import { EVENT_LOG_CATEGORY, EVENT_LOG_NAME } from '@/constants/eventLog';
import { ROUTER } from '@/constants/router';
import { uploadImageToServer, useImage } from '@/hooks/useImage';
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 missionId = params.id as string;

const { isOpen, openModal, closeModal } = useModal();
Expand All @@ -28,6 +55,8 @@ 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 @@ -41,24 +70,18 @@ export default function MissionRecordPage() {
};

const onClickSubmitButton = async () => {
try {
if (!imageFile) {
throw new Error('Image file Not Found');
}

eventLogger.logEvent(EVENT_LOG_NAME.CERTIFICATION.CLICK_CONFIRM, EVENT_LOG_CATEGORY.CERTIFICATION, { remark });
// S3에 이미지 업로드
const { imageFileExtension } = await uploadImageToServer(missionId, imageFile);
await RECORD_API.uploadComplete({ missionRecordId: missionId, imageFileExtension, remark });
router.replace(ROUTER.RECORD.SUCCESS);
} catch (error) {
triggerSnackBar({
message: '미션 인증에 실패했습니다. 다시 시도해주세요.',
});
if (!imageFile || !checkImageType(imageFile.type)) {
return;
}

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

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

const onImageClick = () => {
imageRef.current?.click();
Expand All @@ -67,6 +90,7 @@ export default function MissionRecordPage() {
return (
<main className={mainWrapperCss}>
<div className={headerWrapperCss}>
{isPending && <Loading />}
<div className={headerTitleCss}>미션 인증</div>
<div className={headerRightCss} onClick={openModal}>
<Icon name="normal-close" />
Expand Down
Loading