Skip to content

Commit

Permalink
Merge pull request #302 from ODOICHON/fix/#301
Browse files Browse the repository at this point in the history
[fix] 농가거래 게시글 input값 수정사항
  • Loading branch information
sangminlee98 authored Sep 23, 2024
2 parents 733e240 + 61d7eed commit d750f9d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 26 deletions.
7 changes: 6 additions & 1 deletion src/components/Trade/Info/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ function TradeBoardInfo({ info }: TradeBoardInfoProps) {
면적 <p>{info?.size}</p>
</div>
<div>
준공연도 <p>{dayjs(info?.createdDate).format('YYYY')}</p>
준공연도{' '}
<p>
{info?.createdDate
? `${dayjs(info?.createdDate).format('YYYY')}년`
: '-'}
</p>
</div>
<div>
용도 <p>{info?.purpose}</p>
Expand Down
6 changes: 1 addition & 5 deletions src/components/Trade/Quill/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ export default function TradeQuill({

const newForm: TradeBoardForm = {
...form,
contact: form.contact.replace(/\-/g, ''),
size: form.size.replace(/m2/g, ''),
createdDate,
imageUrls,
tmpYn: isTempSave,
Expand Down Expand Up @@ -123,12 +121,10 @@ export default function TradeQuill({
const imageUrls = [thumbnail, ...getImageUrls(form.code)];
const notUsedImageUrls = getNotUsedImageUrl(images, imageUrls);
const extractedYear = form.createdDate.match(/\d{4}/);
const createdDate = extractedYear ? extractedYear[0] : '2002';
const createdDate = extractedYear ? extractedYear[0] : '';

const newForm: TradeBoardForm = {
...form,
contact: form.contact.replace(/\-/g, ''),
size: form.size.replace(/m2/g, ''),
createdDate,
imageUrls,
tmpYn: isTempSave,
Expand Down
79 changes: 64 additions & 15 deletions src/pages/Trade/Write/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,40 @@ export default function TradeWritePage() {
}));
};

const onChangeForm = (e: React.ChangeEvent<HTMLInputElement>) => {
const onChangeForm = (
e: React.ChangeEvent<HTMLInputElement>,
numValue?: number,
) => {
const { name, value } = e.target;
setForm((prev) => ({ ...prev, [name]: value }));
setForm((prev) => ({ ...prev, [name]: numValue ?? value }));
};

const onChangePoints = (e: React.ChangeEvent<HTMLInputElement>) => {
const { value } = e.target;
let numValue = Number(value.replace(/[^0-9]/g, ''));
if (!numValue) numValue = 0;
onChangeForm(e, numValue);
};

const onParsingPhoneNumber = (phoneNum: string) => {
return phoneNum
.replace(/[^0-9]/g, '')
.replace(/^(\d{2,3})(\d{3,4})(\d{4})$/, `$1-$2-$3`);
};

const onParsingDecimal = (decimal: string) => {
return decimal
.replace(/[^0-9.]/g, '')
.replace(/^0+(?!\.)/, '')
.replace(/(\.\d{2})\d*/, '$1');
};

const addComma = (price: number) => {
if (price === 0) return '';
const returnString = price
?.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return returnString;
};

const thumbnailHandler = async (e: React.ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -277,26 +308,26 @@ export default function TradeWritePage() {
</label>
<input
id="임대 가격"
type="number"
placeholder="만원으로 표기"
placeholder="만원 단위로 표기"
name="price"
value={form.price}
onChange={onChangeForm}
value={addComma(form.price) || ''}
onChange={onChangePoints}
/>
</div>
<div
style={{
display: form.rentalType === 'MONTHLYRENT' ? '' : 'none',
}}
>
<label htmlFor="월세">월세</label>
<label htmlFor="월세">
월세<span className={styles.essential}>*</span>
</label>
<input
id="월세"
type="number"
placeholder="만원으로 표기"
placeholder="만원 단위로 표기"
name="monthlyPrice"
value={form.monthlyPrice}
onChange={onChangeForm}
value={addComma(form.monthlyPrice) || ''}
onChange={onChangePoints}
/>
</div>
<div>
Expand All @@ -306,10 +337,19 @@ export default function TradeWritePage() {
<input
id="전화번호"
type="text"
placeholder="01000000000 표기(매물 관련 연락 가능한 연락처)"
placeholder="매물 관련 연락 가능한 연락처"
name="contact"
value={form.contact}
onChange={onChangeForm}
onChange={(event) =>
onChangeForm({
...event,
target: {
...event.target,
name: 'contact',
value: onParsingPhoneNumber(event.target.value),
},
})
}
/>
</div>
{user?.userType === 'AGENT' && (
Expand Down Expand Up @@ -355,10 +395,19 @@ export default function TradeWritePage() {
<input
id="매물 면적"
type="text"
placeholder="㎡ 표기"
placeholder="㎡ 단위로 표기"
name="size"
value={form.size}
onChange={onChangeForm}
onChange={(event) =>
onChangeForm({
...event,
target: {
...event.target,
name: 'size',
value: onParsingDecimal(event.target.value),
},
})
}
/>
</div>
<div>
Expand Down
5 changes: 0 additions & 5 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ export const checkBeforeTradePost = (
imageUrls,
city,
zipCode,
detail,
price,
monthlyPrice,
contact,
Expand All @@ -173,10 +172,6 @@ export const checkBeforeTradePost = (
alert('우편번호를 입력해주세요.');
return false;
}
if (detail === '') {
alert('상세주소를 입력해주세요.');
return false;
}
if (price === 0) {
alert('매매가를 입력해주세요.');
return false;
Expand Down

0 comments on commit d750f9d

Please sign in to comment.