Created
July 31, 2019 07:23
-
-
Save m04120310/f222b671d0b22ee4117cf2b6c0ab0dca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 把你本來分堆的地方改成一個 funciton | |
const groupCoupons = () => { | |
if (coupons.length !== 0) { | |
coupons.forEach(item => { | |
if (item.status === 'not_redeemed') { | |
const index = Object.values(couponFilter).map(i => i.cdId).indexOf(item.description) | |
if (index === -1) { | |
couponFilter.push({ | |
cdId: item.description, | |
title: item.title, | |
policy: item.policy, | |
profile: item.profile, | |
cdAmount: 1, | |
// transferable: item.transferable, | |
transferable: true, | |
// 鎖轉贈 | |
availableRedeem: true, | |
// availableRedeem: false, | |
cdIdGroup: [{ | |
groupExp: item.expiration, | |
groupStart: item.start, | |
groupAS: item.availableStores, | |
groupId: [item.id], | |
}], | |
}) | |
} else { | |
const cdIdGroupIndex = Object.values(couponFilter[index].cdIdGroup).map(j => j.groupExp).indexOf(item.expiration) | |
if (cdIdGroupIndex === -1) { | |
couponFilter[index].cdIdGroup.push({ | |
groupExp: item.expiration, | |
groupStart: item.start, | |
groupAS: item.availableStores, | |
groupId: [item.id], | |
}) | |
couponFilter[index].cdAmount += 1 | |
} else { | |
couponFilter[index].cdIdGroup[cdIdGroupIndex].groupId.push(item.id) | |
couponFilter[index].cdAmount += 1 | |
} | |
} | |
} | |
}) | |
} | |
} | |
// 使用 use effect, 當全域的 coupons 值有任何更新就自動重分堆 | |
// 而你本來的寫法是每次component 刷新都會分堆一次 | |
useEffect(() => { | |
groupCoupons() | |
}, [coupons]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment