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] 지출에 멤버가 1명일 때, 금액이 수정되는 오류 #641

Merged
merged 2 commits into from
Sep 26, 2024

Conversation

Todari
Copy link
Contributor

@Todari Todari commented Sep 26, 2024

issue

구현 목적

지출에서 수정되지 않은 멤버가 1명일 때 이 한명이 내야 하는 금액은 총 금액에서 고정된 멤버들을 뺀 금액과 같을 수 밖에 없어서 별도의 수정이 가능하지 않도록 막아야 합니다.
현재 수정이 가능하고, 이를 요청보내면 app이 터져버리는 버그가 있습니다.

구현 사항

기존 구현 상황에서, 수정되지 않은 멤버가 1명을 판단하는 로직 자체가 없었습니다.
이를 판단하는 로직을 추가하고, 해당 상황이라면 금액이 변경되지 않게 수정했습니다.

// useEditBillState.ts
// ...
  const isLastUnfixedMember = (keyboardTargetId: number) =>
    !newBillDetails.find(({id}) => id === keyboardTargetId)?.isFixed &&
    newBillDetails.filter(({isFixed}) => isFixed === false).length === 1;

  const handleChangeBillDetails = ({value, keyboardTargetId}: HandleChangeBillDetailsProps) => {
    if (isLastUnfixedMember(keyboardTargetId)) return;
    if (Number(value.replace(/,/g, '')) === newBillDetails.find(({id}) => id === keyboardTargetId)?.price) return;
    setNewBillDetails(prev => {
      const updatedDetails = prev.map(detail =>
        detail.id === keyboardTargetId ? {...detail, price: Number(value.replace(/,/g, '')), isFixed: true} : detail,
      );

      const totalFixedPrice = updatedDetails.reduce((sum, detail) => (detail.isFixed ? sum + detail.price : sum), 0);

      const remainingPrice = newBill.price - totalFixedPrice;
      const unfixedCount = updatedDetails.filter(detail => !detail.isFixed).length;

      const unfixedPrice = Math.floor(remainingPrice / unfixedCount);
      const lastUnfixedIndex = updatedDetails.map(detail => !detail.isFixed).lastIndexOf(true);

      return updatedDetails.map((detail, index) => {
        if (detail.isFixed) return detail;
        if (index === lastUnfixedIndex) {
          return {...detail, price: remainingPrice - unfixedPrice * (unfixedCount - 1)};
        }
        return {...detail, price: unfixedPrice};
      });
    });
  };
// ...

@Todari Todari added 🖥️ FE Frontend 🚨 bug bug 🔍 QC quality check labels Sep 26, 2024
@Todari Todari added this to the v2.0.0 milestone Sep 26, 2024
@Todari Todari self-assigned this Sep 26, 2024
Copy link

@soi-ha soi-ha merged commit 949ed88 into fe-dev Sep 26, 2024
2 checks passed
@soi-ha soi-ha deleted the feature/#621 branch September 26, 2024 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚨 bug bug 🖥️ FE Frontend 🔍 QC quality check
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

3 participants