Skip to content

Commit

Permalink
feat :: 기업 로고 수정 presigned로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
은지 committed Aug 22, 2024
1 parent 656d51c commit 952a896
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 119 deletions.
1 change: 1 addition & 0 deletions src/Apis/Companies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
useMutation,
useQueries,
useQuery,
useQueryClient,
} from 'react-query';
import { useParams } from 'react-router-dom';
import { instance } from '../axios';
Expand Down
12 changes: 9 additions & 3 deletions src/Components/Detail/CompanyDetail/Basic/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ export function CompanyDetailBasic({
setCanEdit,
}: PropsType) {
const navigate = useNavigate();

const nameArray = decodeURI(companyDetailInfo?.biz_registration_url).split(
'/'
);
const { mutate: downloadFile } = useDownloadData({
fileName: nameArray[nameArray.length - 1].substring(37),
fileUrl: companyDetailInfo?.biz_registration_url,
});

return (
<_.Container>
<_.Wrapper>
Expand All @@ -36,7 +34,15 @@ export function CompanyDetailBasic({
</_.BackWrapper>
<_.LogoWrapper>
<_.CompanyLogo
src={`${process.env.REACT_APP_FILE_URL}${companyDetailInfo?.company_profile_url}`}
src={
!!companyDetailInfo &&
`${
process.env.REACT_APP_FILE_URL
}${companyDetailInfo?.company_profile_url.replace(
/\s+/g,
''
)}`
}
/>
</_.LogoWrapper>
</Stack>
Expand Down
90 changes: 59 additions & 31 deletions src/Components/Detail/CompanyDetail/Edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import {
useToastStore,
Text,
} from '@team-return/design-system';
import { Dispatch, SetStateAction, useRef, useState } from 'react';
import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react';
import { CompanyDetailResponse } from '../../../../Apis/Companies/response';
import { useForm } from '../../../../Hooks/useForm';
import * as _ from '../../style';
import { CompanyInfoEditType } from '../../../../Apis/Companies/request';
import { useChangeCompanyInfo } from '../../../../Apis/Companies';
import { useFileUpload } from '../../../../Apis/File';
import { useNavigate } from 'react-router-dom';
import { usePresignedUrl } from '../../../../Apis/Files';
import { useQueryClient } from 'react-query';

interface PropsType {
companyDetailInfo: CompanyDetailResponse;
Expand All @@ -28,7 +29,15 @@ export function CompanyDetailEdit({
const navigate = useNavigate();
const { append } = useToastStore();
const fileInput = useRef<HTMLInputElement>(null);
const [file, setFile] = useState<File | null>(null);

const [imageUrl, setImageUrl] = useState<string | null>(
`${process.env.REACT_APP_FILE_URL}${companyDetailInfo?.company_profile_url}` ||
null
);
console.log(companyDetailInfo?.company_profile_url);

const [selectedFile, setSelectedFile] = useState<File | null>(null);

const {
form: companyDetailEditInfo,
setForm: setCompanyDetailEditInfo,
Expand Down Expand Up @@ -76,6 +85,7 @@ export function CompanyDetailEdit({
representative_phone_no,
} = companyDetailEditInfo;

const queryClient = useQueryClient();
const { mutate: editCompanyInfo } = useChangeCompanyInfo(
companyDetailEditInfo,
{
Expand All @@ -87,6 +97,9 @@ export function CompanyDetailEdit({
});
refetchCompanyDetailInfo();
setCanEdit(false);
queryClient.invalidateQueries({
queryKey: ['editCompanyInfo'],
});
},
onError: () => {
append({
Expand All @@ -98,26 +111,49 @@ export function CompanyDetailEdit({
}
);

const { mutate: fileUploader } = useFileUpload(file!, {
onSuccess: (res: any) => {
setCompanyDetailEditInfo((companyDetailEditInfo) => ({
...companyDetailEditInfo,
company_profile_url: res.data?.urls[0],
}));
setTimeout(editCompanyInfo);
},
onError: () => {
append({
title: '파일 업로드에 실패하였습니다.',
message: '',
type: 'RED',
});
},
});
const { mutateAsync: getPresignedUrl } = usePresignedUrl();

const handleFileUploadAndEdit = async () => {
if (selectedFile) {
const response = await getPresignedUrl([selectedFile]);
console.log(response);
if (response?.presignedUrls?.urls?.[0]?.file_path) {
const uploadedUrl = `${process.env.REACT_APP_FILE_URL}${response.presignedUrls.urls[0].file_path}`;
setImageUrl(uploadedUrl);
setCompanyDetailEditInfo((companyDetailEditInfo) => ({
...companyDetailEditInfo,
company_profile_url:
response.presignedUrls.urls[0].file_path,
}));
setTimeout(() => {
editCompanyInfo();
}, 500);
}
}
};

// useEffect(() => {
// if (
// companyDetailEditInfo.company_profile_url !==
// companyDetailInfo.company_profile_url
// ) {
// editCompanyInfo();
// }
// // eslint-disable-next-line
// }, [companyDetailEditInfo.company_profile_url]);

const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.files && event.target.files.length > 0) {
const file = event.target.files[0];
setSelectedFile(file);
const fileUrl = URL.createObjectURL(file);
setImageUrl(fileUrl);
}
};

const submitEditCompanyInfo = () => {
if (file) {
fileUploader();
if (selectedFile) {
handleFileUploadAndEdit();
} else {
editCompanyInfo();
}
Expand All @@ -134,13 +170,7 @@ export function CompanyDetailEdit({
</Text>
</_.BackWrapper>
<_.LogoWrapper>
<_.CompanyLogo
src={
file
? URL.createObjectURL(file)
: `${process.env.REACT_APP_FILE_URL}${companyDetailInfo?.company_profile_url}`
}
/>
<_.CompanyLogo src={imageUrl || ''} />
</_.LogoWrapper>
<_.LogoEditImg
onClick={() => {
Expand All @@ -154,9 +184,7 @@ export function CompanyDetailEdit({
hidden
ref={fileInput}
accept="image/jpg, image/png"
onChange={(e) => {
setFile(e.target.files ? e.target.files[0] : null);
}}
onChange={handleFileChange}
/>
</_.LogoEditWrapper>
<Stack gap={20}>
Expand Down
Loading

0 comments on commit 952a896

Please sign in to comment.