Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
guesung committed Jan 24, 2024
1 parent 1391a73 commit 7434a9b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ export default function UploadSection({ control }: ImageThumbnailProps) {
name: 'imageUrl',
control,
});
const { field: previewImageField } = useController({
name: 'previewImage',
control,
});

const { handleFileUploadClick } = useFileUpload(async (files) => {
const { handleFileUploadClick, previewImage } = useFileUpload(async (files) => {
field.onChange(files[0]);
}, previewImageField);
});

return (
<Flex
Expand All @@ -33,13 +29,13 @@ export default function UploadSection({ control }: ImageThumbnailProps) {
className="relative mx-20 aspect-[8/5] overflow-hidden rounded-8 bg-sub"
onClick={handleFileUploadClick}
>
<RenderImage previewImage={previewImageField.value} />
<RenderImage previewImage={previewImage} />
</Flex>
);
}

interface RenderImageProps {
previewImage: string;
previewImage: string | undefined;
}

const RenderImage = memo(function ({ previewImage }: RenderImageProps) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { useController } from 'react-hook-form';

import CountryBotoomSheet from './CountryBotoomSheet';
import { Step1Props } from './Step1.client';
import { formatBirthDTO } from '../../util';
Expand All @@ -17,6 +15,7 @@ import { TextField, TextFieldController } from '@/components/TextField';
import { personalityList } from '@/constants/personalityList';
import { useFileUpload } from '@/hooks/useFileUpload';
import { useModal } from '@/hooks/useModal';
import { useController } from 'react-hook-form';

interface Step1InputFormProps extends Step1Props {}

Expand All @@ -34,7 +33,10 @@ export default function Step1InputForm({ onPrev }: Step1InputFormProps) {
name: 'imageUrl',
control,
});
const { handleFileUploadClick, isPending } = useFileUpload((files) => onChange(files[0]));

const { handleFileUploadClick, isPending, previewImage } = useFileUpload((files) =>
onChange(files[0])
);

const isAllTyped = formState.isValid && !!watch('gender') && personalities.length;

Expand Down Expand Up @@ -66,7 +68,7 @@ export default function Step1InputForm({ onPrev }: Step1InputFormProps) {
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col px-20">
<Flex justify="center">
<Avatar
imageUrl={value}
imageUrl={previewImage || value}
size="large"
iconVariant="draft_orders"
isPending={isPending}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/getBase64FromImage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function getBase64FromImage(file: File) {
export function getBase64FromImage(file: File): Promise<string | undefined> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
Expand Down
15 changes: 6 additions & 9 deletions src/hooks/useFileUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { getBase64FromImage } from './getBase64FromImage';
import { usePostFiles } from '@/apis/common';
import { useCallback } from 'react';
import { useCallback, useState } from 'react';
import { ControllerRenderProps } from 'react-hook-form';

interface UseImageUploadProps {
/**
* 파일 업로드가 완료되었을 때 실행할 함수를 지정합니다.
*/
handleFileChange: (fileUrlList: string[]) => void;
previewImageField: ControllerRenderProps<any, any>;
options?: {
/**
* 업로드할 파일의 타입을 지정합니다. (default: image/*)
Expand All @@ -23,10 +22,10 @@ interface UseImageUploadProps {

export function useFileUpload(
handleFileChange: UseImageUploadProps['handleFileChange'],
previewImageField?: UseImageUploadProps['previewImageField'],
options?: UseImageUploadProps['options']
) {
const { mutate, isPending } = usePostFiles();
const [previewImage, setPreviewImage] = useState<string | undefined>();

const handleFileUploadClick = useCallback(() => {
const input = document.createElement('input');
Expand All @@ -37,11 +36,8 @@ export function useFileUpload(
const { files } = event.target as HTMLInputElement;
if (!files) return;

if (previewImageField) {
const base64 = await getBase64FromImage(files[0]);
console.log(base64);
previewImageField.onChange(base64);
}
const base64 = await getBase64FromImage(files[0]);
setPreviewImage(base64);

mutate(
{ fileList: Array.from(files) },
Expand All @@ -53,10 +49,11 @@ export function useFileUpload(
);
};
input.click();
}, [handleFileChange, mutate, options?.accept, options?.multiple, previewImageField]);
}, [handleFileChange, mutate, options?.accept, options?.multiple]);

return {
handleFileUploadClick,
isPending,
previewImage,
};
}
2 changes: 1 addition & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const middleware = async (request: NextRequest) => {

const lng = request.cookies.get(cookieName)?.value || 'en';

/* 새로운 페이지 접속 */
/* 새로운 페이지 접속 시m */
if (
!languages.some((loc: string) => request.nextUrl.pathname.startsWith(`/${loc}`)) &&
!request.nextUrl.pathname.startsWith('/_next')
Expand Down

0 comments on commit 7434a9b

Please sign in to comment.