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: 회사등록 multipart/form-data로 변경 및 API 연결 #122

Merged
merged 1 commit into from
Nov 8, 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
11 changes: 8 additions & 3 deletions src/apis/registerCompany/hooks/useRegisterCompany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ export type CompanyRequestData = {
industryOccupation?: string;
brand?: string;
revenuePerYear?: number;
logoImage?: string;
logoImage?: File;
};

export const getPostCompanyPath = () => `${APIPath.registerCompany}`;

export const postCompany = async (req: CompanyRequestData) => {
const response = await clientInstance.post(getPostCompanyPath(), req);
export const postCompany = async (req: FormData) => {
const response = await clientInstance.post(getPostCompanyPath(), req, {
headers: {
Accept: 'multipart/form-data',
'Content-Type': 'multipart/form-data',
},
});
return response.data;
};

Expand Down
36 changes: 29 additions & 7 deletions src/pages/registerCompany/RegisterCompany.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { usePostCompany } from '@/apis/registerCompany/hooks/useRegisterCompany';
import { CompanyRequestData, usePostCompany } from '@/apis/registerCompany/hooks/useRegisterCompany';
import { Button, Flex, Input, Typo } from '@/components/common';
import Layout from '@/features/layout';
import ROUTE_PATH from '@/routes/path';
Expand All @@ -12,16 +12,17 @@ const default_inputs = {
industryOccupation: '',
brand: '',
revenuePerYear: 0,
logoImage: '',
logoImage: undefined as File | undefined,
};

export default function RegisterCompany() {
const { t } = useTranslation();
const mutation = usePostCompany();
const navigate = useNavigate();
const [inputs, setInputs] = useState({ ...default_inputs });
const [inputs, setInputs] = useState<CompanyRequestData>({ ...default_inputs });
const [file, setFile] = useState<File | null>(null);

const { name, industryOccupation, brand, revenuePerYear, logoImage } = inputs;
const { name, industryOccupation, brand, revenuePerYear } = inputs;

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { value, name } = e.target;
Expand All @@ -31,8 +32,29 @@ export default function RegisterCompany() {
});
};

const onFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newfile = e.target.files?.[0];
if (newfile) {
setFile(newfile);
}
};

const handlePostCompany = () => {
mutation.mutate(inputs, {
const formData = new FormData();

const companyRequest = {
name: inputs.name,
industryOccupation: inputs.industryOccupation,
brand: inputs.brand,
revenuePerYear: inputs.revenuePerYear,
};

formData.append('companyRequest', JSON.stringify(companyRequest));

if (file) {
formData.append('logoImage', file);
}
mutation.mutate(formData, {
onSuccess: () => {
navigate(ROUTE_PATH.HOME);
},
Expand All @@ -41,6 +63,7 @@ export default function RegisterCompany() {
},
});
};

return (
<Layout>
<section>
Expand All @@ -56,8 +79,7 @@ export default function RegisterCompany() {
label={t('registerCompany.LOGOIMAGE')}
name="logoImage"
type="file"
value={logoImage}
onChange={onChange}
onChange={onFileChange}
// style={{ width: '570px', height: '48px' }}
></Input>
</InputContainer>
Expand Down
Loading