Skip to content

Commit

Permalink
Merge pull request #161 from Team-Lecue/feature/TargetPage
Browse files Browse the repository at this point in the history
[ TargetPage ] dev 머지 PR입니다!
  • Loading branch information
jungwoo3490 authored Jan 18, 2024
2 parents 665517a + e3a8ec9 commit e9097de
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/Target/components/FavoriteImageInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { IcCamera, ImgBook } from '../../../assets';
import * as S from './FavoriteImageInput.style';

interface FavoriteImageInputProps {
imgFile: File | null;
changeFileData: (file: File) => void;
}

Expand Down
14 changes: 14 additions & 0 deletions src/Target/hooks/useGetPresignedUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useQuery } from 'react-query';

import { getPresignedUrl } from '../util/api';

const useGetPresignedUrl = () => {
const { isLoading, error, data } = useQuery({
queryKey: ['get-presigned-url'],
queryFn: () => getPresignedUrl(),
});

return { isLoading, error, data };
};

export default useGetPresignedUrl;
22 changes: 22 additions & 0 deletions src/Target/hooks/usePutPresignedUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AxiosError } from 'axios';
import { useMutation } from 'react-query';

import { putPresignedUrl } from '../util/api';

interface usePutPresignedUrlProps {
url: string;
data: ArrayBuffer;
contentType: string;
}

const usePutPresignedUrl = () => {
const mutation = useMutation({
mutationFn: ({ url, data, contentType }: usePutPresignedUrlProps) => {
return putPresignedUrl(url, data, contentType);
},
onError: (err: AxiosError) => console.log(err),
});
return mutation;
};

export default usePutPresignedUrl;
34 changes: 17 additions & 17 deletions src/Target/page/TargetPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,43 @@ import Header from '../../../components/common/Header';
import CompleteButton from '../../components/CompleteButton';
import FavoriteImageInput from '../../components/FavoriteImageInput';
import NameInput from '../../components/NameInput';
import { getPresignedUrl, uploadFile } from '../../util/api';
import useGetPresignedUrl from '../../hooks/useGetPresignedUrl';
import usePutPresignedUrl from '../../hooks/usePutPresignedUrl';
import * as S from './TargetPage.style';

function TargetPage() {
const [presignedFileName, setPresignedFileName] = useState('');
const [name, setName] = useState('');
const [fileData, setFileData] = useState<File | null>(null); // Add this line
const [fileData, setFileData] = useState<File | null>(null);
const [presignedData, setPresignedData] = useState({
url: '',
fileName: '',
});

const navigate = useNavigate();

const { data } = useGetPresignedUrl();
const putMutation = usePutPresignedUrl();

useEffect(() => {
const fetchPresignedData = async () => {
const { url, fileName } = await getPresignedUrl('/api/images/book');
if (data) {
const { url, fileName } = data;
setPresignedData({ url, fileName });
setPresignedFileName(fileName);
};

fetchPresignedData();
}, []);
}
}, [data]);

const handleClickCompleteButton = async () => {
if (fileData) {
const reader = new FileReader();
reader.readAsArrayBuffer(fileData);
reader.onloadend = async () => {
if (reader.result !== null) {
await uploadFile(
presignedData.url,
reader.result as ArrayBuffer,
fileData.type,
);
putMutation.mutate({
url: presignedData.url,
data: reader.result as ArrayBuffer,
contentType: fileData.type,
});
}
};
}
Expand All @@ -59,10 +62,7 @@ function TargetPage() {
</S.NameInputWrapper>
<S.FavoriteInputWrapper>
<S.SectionTitle>최애의 사진 업로드</S.SectionTitle>
<FavoriteImageInput
imgFile={fileData}
changeFileData={(file) => setFileData(file)}
/>
<FavoriteImageInput changeFileData={(file) => setFileData(file)} />
</S.FavoriteInputWrapper>
</S.InputSectionWrapper>
<CompleteButton
Expand Down
14 changes: 8 additions & 6 deletions src/Target/util/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ interface PresignedUrlResponse {
};
}

const getPresignedUrl = async (
endpoint: string,
): Promise<{ url: string; fileName: string }> => {
const response: AxiosResponse<PresignedUrlResponse> = await api.get(endpoint);
const getPresignedUrl = async (): Promise<{
url: string;
fileName: string;
}> => {
const response: AxiosResponse<PresignedUrlResponse> =
await api.get('/api/images/book');
return {
url: response.data.data.url,
fileName: response.data.data.fileName,
};
};

const uploadFile = async (
const putPresignedUrl = async (
url: string,
data: ArrayBuffer,
contentType: string,
Expand All @@ -31,4 +33,4 @@ const uploadFile = async (
});
};

export { getPresignedUrl, uploadFile };
export { getPresignedUrl, putPresignedUrl };

0 comments on commit e9097de

Please sign in to comment.