Skip to content

Commit

Permalink
fix: commission write fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sw326 committed Sep 22, 2024
1 parent be0619d commit a9edcee
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/api/commissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export const fetchCommissionSendDetail = async (
};

export const createCommission = async (
commission: Omit<Commission, 'commissionId' | 'memberNick'>,
commission: Omit<Commission, 'commissionId' | 'memberNick'> & {
addressId: number;
},
): Promise<Commission> => {
const response = await api.post<Commission>('/commission', commission);
return response.data;
Expand Down
33 changes: 20 additions & 13 deletions src/pages/members/CommissionWrite.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useRef } from 'react';
import React, { useState, useRef, useCallback } from 'react';
import { useNavigate } from 'react-router-dom';
import toast from 'react-hot-toast';
import {
Expand All @@ -10,7 +10,6 @@ import {
CleanType,
CommissionFormData,
AddressData,
Status,
CleanTypeKorean,
HouseTypeKorean,
} from '../../types/commission';
Expand Down Expand Up @@ -49,10 +48,10 @@ const CommissionWrite: React.FC = () => {
const [isUploading, setIsUploading] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);

const handleAddressSelect = (addressData: AddressData) => {
const handleAddressSelect = useCallback((addressData: AddressData) => {
setForm(prev => ({ ...prev, addressId: addressData.id }));
setSelectedAddress(addressData);
};
}, []);

const handleChange = (
e: React.ChangeEvent<
Expand Down Expand Up @@ -104,12 +103,12 @@ const CommissionWrite: React.FC = () => {
}
};

const validateForm = (): boolean => {
const validateForm = useCallback((): boolean => {
const validations = [
{ field: 'size', validation: validateSize(form.size?.toString() || '') },
{ field: 'houseType', validation: validateHouseType(form.houseType) },
{ field: 'cleanType', validation: validateCleanType(form.cleanType) },
{ field: 'address', validation: validateAddress(selectedAddress) },
{ field: 'addressId', validation: validateAddress(form.addressId) },
{ field: 'image', validation: validateImage(form.image) },
{
field: 'desiredDate',
Expand All @@ -119,6 +118,13 @@ const CommissionWrite: React.FC = () => {
field: 'significant',
validation: validateSignificant(form.significant),
},
{
field: 'addressId',
validation: {
isValid: !!form.addressId,
message: '주소를 선택해주세요.',
},
},
];

let isValid = true;
Expand All @@ -131,14 +137,14 @@ const CommissionWrite: React.FC = () => {
});

return isValid;
};
}, [form]);

const getFieldName = (field: string): string => {
const fieldNames: { [key: string]: string } = {
size: '평수',
houseType: '주거 형태',
cleanType: '청소 종류',
address: '주소',
addressId: '주소',
image: '이미지',
desiredDate: '희망 날짜',
significant: '특이사항',
Expand All @@ -155,16 +161,17 @@ const CommissionWrite: React.FC = () => {

try {
const newCommission = {
...form,
size: form.size || 0,
addressId: form.addressId || 0,
desiredDate: new Date(form.desiredDate).toISOString(),
addressId: form.addressId as number,
image: form.image,
houseType: form.houseType as HouseType,
cleanType: form.cleanType as CleanType,
address: selectedAddress?.address || '',
status: 'CHECK' as Status,
desiredDate: new Date(form.desiredDate).toISOString(),
significant: form.significant,
};

console.log('Sending commission data:', newCommission);

await createCommissionMutation.mutateAsync(newCommission);
toast.success('의뢰가 성공적으로 작성되었습니다.');
navigate('/commissionlist');
Expand Down
7 changes: 3 additions & 4 deletions src/types/commission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const Status = {
FINISH: 'FINISH',
} as const;

export type Status = typeof Status[keyof typeof Status];
export type Status = (typeof Status)[keyof typeof Status];

export const StatusKorean = {
[Status.CHECK]: '대기',
Expand All @@ -21,7 +21,7 @@ export const HouseType = {
TOILET: 'TOILET',
} as const;

export type HouseType = typeof HouseType[keyof typeof HouseType];
export type HouseType = (typeof HouseType)[keyof typeof HouseType];

export const HouseTypeKorean = {
[HouseType.APT]: '아파트',
Expand All @@ -35,7 +35,7 @@ export const CleanType = {
SPECIAL: 'SPECIAL',
} as const;

export type CleanType = typeof CleanType[keyof typeof CleanType];
export type CleanType = (typeof CleanType)[keyof typeof CleanType];

export const CleanTypeKorean = {
[CleanType.NORMAL]: '일반 청소',
Expand All @@ -52,7 +52,6 @@ export interface Commission {
cleanType: CleanType;
desiredDate: string;
significant?: string;
status: Status;
}

export interface CommissionFormData {
Expand Down
6 changes: 4 additions & 2 deletions src/utils/validationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ export const validateSignificant = createValidator(
);

// 주소 유효성 검사
export const validateAddress = (addressData: any): ValidationResult => {
if (!addressData || !addressData.address || !addressData.addressDetail) {
export const validateAddress = (
addressData: number | null,
): ValidationResult => {
if (!addressData) {
return {
isValid: false,
message: '주소를 선택하고 상세주소를 입력해주세요.',
Expand Down

0 comments on commit a9edcee

Please sign in to comment.