Skip to content

Commit

Permalink
๐Ÿšš Rename : ๋ฃธ ์ปดํฌ๋„ŒํŠธ ํด๋” ์ด๋™ #151
Browse files Browse the repository at this point in the history
  • Loading branch information
eunjju2 committed Jan 9, 2025
1 parent df7e55c commit 09752c6
Showing 1 changed file with 171 additions and 0 deletions.
171 changes: 171 additions & 0 deletions src/pages/RegisterSpace/components/RoomForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import TextareaAutosize from 'react-textarea-autosize';
import { ChangeEvent, FormEvent, useState } from 'react';
import { Room } from '@typings/types';
import { ERROR_MESSAGE } from '@constants/constants';
import RoomImage from '@components/RoomImage';
import CountPeople from '@components/CountPeople';

interface RoomFormProps {
room: Room;
updateRoomData: (data: Partial<Room>) => void;
completeAdd: (id: string) => void;
}

const RoomForm = ({ room, updateRoomData, completeAdd }: RoomFormProps) => {
const handleChange = (
e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
) => {
if (e.target.name === 'description' && e.target.value.length > 200) {
e.target.value = e.target.value.substring(0, 200);
}
if (e.target.name === 'price') {
e.target.value = e.target.value.replace(/[^0-9]/g, '');
}
updateRoomData({ [e.target.name]: e.target.value });
};

const [errorMessage, setErrorMessage] = useState({
roomNameError: '',
roomDescriptionError: '',
roomImagesError: '',
priceError: '',
});

const isValid = () => {
let pass = true;
const newErrorMessage = {
roomNameError: '',
roomDescriptionError: '',
roomImagesError: '',
priceError: '',
};
if (room.roomName === '') {
newErrorMessage.roomNameError = ERROR_MESSAGE.roomName;
pass = false;
}
if (room.description === '') {
newErrorMessage.roomDescriptionError = ERROR_MESSAGE.roomDescription;
pass = false;
}
if (room.roomImages.length === 0) {
newErrorMessage.roomImagesError = ERROR_MESSAGE.image;
pass = false;
}
if (room.price === '' || room.people === 0) {
newErrorMessage.priceError = ERROR_MESSAGE.priceAndPeople;
pass = false;
}

setErrorMessage(newErrorMessage);
return pass;
};

const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (isValid()) {
completeAdd('');
}
};

return (
<div className='flex justify-center pb-24 pt-[35px]'>
<form
className='w-custom'
onSubmit={handleSubmit}
>
<div className='flex flex-col'>
<label
htmlFor='roomName'
className='mb-[6px] text-[14px] font-normal'
>
๋ฃธ ์ด๋ฆ„
</label>
<input
name='roomName'
type='text'
className='main-input'
placeholder='๋ฃธ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.'
onChange={handleChange}
value={room.roomName}
/>
</div>
{errorMessage.roomNameError && (
<div className='mt-[8px] text-[12px] font-medium text-[#F83A3A]'>
{errorMessage.roomNameError}
</div>
)}
<div className='mt-[40px] flex flex-col'>
<div className='flex justify-between'>
<label
htmlFor='description'
className='mb-[6px] text-[14px] font-normal'
>
๋ฃธ ์„ค๋ช…
</label>
<p className='text-[14px] font-normal text-subfont'>
{room.description.length}/200
</p>
</div>
<TextareaAutosize
cacheMeasurements
minRows={4.6}
name='description'
className='main-textarea text-[14px]'
placeholder='๋ฃธ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.'
onChange={handleChange}
value={room.description}
/>
</div>
{errorMessage.roomDescriptionError && (
<div className='mt-[8px] text-[12px] font-medium text-[#F83A3A]'>
{errorMessage.roomDescriptionError}
</div>
)}
<RoomImage
roomImages={room.roomImages}
onUpdateImages={(roomImages) => updateRoomData({ roomImages })}
/>
{errorMessage.roomImagesError && (
<div className='mt-[8px] text-[12px] font-medium text-[#F83A3A]'>
{errorMessage.roomImagesError}
</div>
)}
<div className='mt-[40px] flex items-center justify-between'>
<div className='flex items-center'>
<label htmlFor='price'>
<p className='text-center text-[14px] font-normal'>
๊ฐ€๊ฒฉ
<br /> <span className='text-[11px]'>(1์ธ๋‹น 1์‹œ๊ฐ„)</span>
</p>
</label>
<input
name='price'
type='text'
className='main-input ml-[20px] h-[38px] w-[84px] rounded-[5px]'
placeholder='ex) 3000'
onChange={handleChange}
value={room.price}
/>
</div>
<CountPeople
room={room}
updateRoomData={updateRoomData}
/>
</div>
{errorMessage.priceError && (
<div className='mt-[8px] text-[12px] font-medium text-[#F83A3A]'>
{errorMessage.priceError}
</div>
)}
<button
type='submit'
className='btn-primary mt-[40px] text-[16px]'
>
๋ฃธ ๋“ฑ๋ก ์™„๋ฃŒ
</button>
</form>
</div>
);
};

export default RoomForm;

0 comments on commit 09752c6

Please sign in to comment.