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

경북대 FE_이효은_4주차 과제 Step2 #54

Open
wants to merge 19 commits into
base: hyoeunkh
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
60 changes: 31 additions & 29 deletions src/components/Order/CashReceipt.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import { Checkbox, Input, Select } from '@chakra-ui/react';
import styled from '@emotion/styled';
import { useState } from 'react';
import { forwardRef, useState } from 'react';

export const CashReceipt = () => {
const [checked, setChecked] = useState(false);
const [value, setValue] = useState('');
export const CashReceipt = forwardRef<HTMLInputElement, { checked: boolean; setChecked: (checked: boolean) => void }>(
({ checked, setChecked }, ref) => {
const [value, setValue] = useState('');

return (
<CashReceiptWrapper>
<Checkbox
checked={checked}
onChange={(e) => setChecked(e.target.checked)}
borderColor="#e6e6e6"
size="lg"
padding="10px 0px"
colorScheme="yellow"
>
<Label>현금영수증 신청</Label>
</Checkbox>
<Select borderColor="#e6e6e6">
<option value="option1">개인소득공제</option>
<option value="option2">사업자증빙용</option>
</Select>
<Input
placeholder="(-없이) 숫자만 입력해주세요."
value={value}
onChange={(e) => setValue(e.target.value)}
borderColor="#e6e6e6"
/>
</CashReceiptWrapper>
);
};
return (
<CashReceiptWrapper>
<Checkbox
isChecked={checked}
onChange={(e) => setChecked(e.target.checked)}
borderColor="#e6e6e6"
size="lg"
padding="10px 0px"
colorScheme="yellow"
>
<Label>현금영수증 신청</Label>
</Checkbox>
<Select borderColor="#e6e6e6">
<option value="option1">개인소득공제</option>
<option value="option2">사업자증빙용</option>
</Select>
<Input
placeholder="(-없이) 숫자만 입력해주세요."
value={value}
onChange={(e) => setValue(e.target.value)}
borderColor="#e6e6e6"
ref={ref}
/>
</CashReceiptWrapper>
);
},
);
const CashReceiptWrapper = styled.div`
display: flex;
flex-direction: column;
Expand Down
38 changes: 34 additions & 4 deletions src/components/Order/PaymentSection.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@
import { Divider } from '@chakra-ui/layout';
import styled from '@emotion/styled';
import { useRef, useState } from 'react';

import { Button } from '../common/Button';
import { HandleBox, Loading } from '../common/Handle';
import { CashReceipt } from './CashReceipt';

import { useProductDetail } from '@/services/useProductDetail';

export const PaymentSection = ({ orderHistory }: { orderHistory: { id: number; count: number } }) => {
export const PaymentSection = ({
orderHistory,
inputRef,
}: {
orderHistory: { id: number; count: number };
inputRef: React.RefObject<HTMLInputElement>;
}) => {
const { isError, isPending, data, error } = useProductDetail(orderHistory.id.toString());
const [checked, setChecked] = useState(false);
const numberRef = useRef<HTMLInputElement>(null);

if (isPending) {
return <Loading />;
}
if (isError) {
return <HandleBox>{error.message}</HandleBox>;
}
const totalPrice = data.detail.price.basicPrice * orderHistory.count;

const handleClick = () => {
window.alert('주문이 완료되었습니다.');
const number = numberRef.current?.value;
const input = inputRef.current?.value || '';

if (input.length > 100) {
alert('메시지는 100자 이내로 입력해주세요.');
return;
}
if (!input) {
alert('메시지를 입력해주세요.');
return;
}
if (checked && !number) {
alert('현금영수증 번호를 입력해주세요');
return;
}
if (isNaN(Number(number))) {
alert('현금영수증 번호는 숫자로만 입력해주세요.');
return;
}
alert('주문이 완료되었습니다.');
};

const totalPrice = data.detail.price.basicPrice * orderHistory.count;

return (
<PaymentSectionWrapper>
<Title>결제 정보</Title>
<Divider color="#e6e6e6" />
<CashReceipt />
<CashReceipt ref={numberRef} checked={checked} setChecked={setChecked}/>
<PaymentInfo>
<Divider color="#e6e6e6" />
<FinallyPrice>
Expand Down
8 changes: 6 additions & 2 deletions src/pages/OrderPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import styled from '@emotion/styled';
import { useRef } from 'react';

import { Container } from '@/components/common/Layout/Container';
import { GiftMessageSection } from '@/components/Order/GiftMessageSection';
Expand All @@ -7,15 +8,18 @@ import { useOrderHistory } from '@/hooks/useOrderHistory';

export default function OrderPage() {
const { orderHistoryToken } = useOrderHistory();

const inputRef = useRef<HTMLInputElement>(null);

return (
<OrderPageWrapper>
<Container maxWidth="1280px">
<Inner>
<Left>
<GiftMessageSection orderHistory={orderHistoryToken} />
<GiftMessageSection orderHistory={orderHistoryToken} inputRef={inputRef} />
</Left>
<Right>
<PaymentSection orderHistory={orderHistoryToken} />
<PaymentSection orderHistory={orderHistoryToken} inputRef={inputRef} />
</Right>
</Inner>
Comment on lines +12 to 24
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재는 inputRef를 끌어올려서 부모에서 각 섹션으로 넘겨주는 형태인데요,
submit, 유효성 검사에 필요한 모든 정보를 부모로 끌어올려 관리하는 것은 어떨까요?

현금 영수증 정보까지 모두 부모에서 관리하면 각 섹션은 각자가 해야하는 일에 집중할 수 있게 됩니다.
GiftMessageSection는 메시지를 입력하고, 관련 ui를 보여줍니다.
PaymentSection는 현금 영수증 정보를 입력하고, 결제를 처리하고, 관련 ui를 보여줍니다. (현금 영수증과 결제를 분리해도 괜찮을 것 같네요. 결제는 부모에서, 현금 영수증은 별도의 섹션으로 관리)
이제 각 섹션은 자신이 입력하는 데이터가 유효한지는 생각하지 않아도 됩니다.

근데 이러면 부모쪽에 책임이 집중된다는 단점이 있어서 완전히 좋다고 하기는 좀 그러네요...
저라면 부모로 옮기는 방식과 contextAPI 사용을 고려해봤을 것 같아요.

</Container>
Expand Down