From 818c90a95ece1b40e7316603322ca11ef118450f Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Tue, 9 Aug 2022 23:05:56 -0700 Subject: [PATCH] Refactor tipper (#734) * Clean up tipping components * Pass comment into tip callback --- web/components/tipper.tsx | 70 ++++++++++++++------------------------- 1 file changed, 25 insertions(+), 45 deletions(-) diff --git a/web/components/tipper.tsx b/web/components/tipper.tsx index 68ca5308..1c76c3e7 100644 --- a/web/components/tipper.tsx +++ b/web/components/tipper.tsx @@ -37,7 +37,7 @@ export function Tipper(prop: { comment: Comment; tips: CommentTips }) { // declare debounced function only on first render const [saveTip] = useState(() => - debounce(async (user: User, change: number) => { + debounce(async (user: User, comment: Comment, change: number) => { if (change === 0) { return } @@ -71,30 +71,24 @@ export function Tipper(prop: { comment: Comment; tips: CommentTips }) { // instant save on unrender useEffect(() => () => void saveTip.flush(), [saveTip]) - const changeTip = (tip: number) => { - setLocalTip(tip) - me && saveTip(me, tip - savedTip) + const addTip = (delta: number) => { + setLocalTip(localTip + delta) + me && saveTip(me, comment, localTip - savedTip + delta) } + const canDown = me && localTip > savedTip + const canUp = me && me.id !== comment.userId && me.balance >= localTip + 5 return ( - + addTip(-5) : undefined} /> {Math.floor(total)} - + addTip(+5) : undefined} value={localTip} /> {localTip === 0 ? ( '' ) : ( 0 ? 'text-primary' : 'text-red-400' )} > @@ -105,21 +99,17 @@ export function Tipper(prop: { comment: Comment; tips: CommentTips }) { ) } -function DownTip(prop: { - value: number - onChange: (tip: number) => void - disabled?: boolean -}) { - const { onChange, value, disabled } = prop +function DownTip(props: { onClick?: () => void }) { + const { onClick } = props return ( @@ -127,30 +117,20 @@ function DownTip(prop: { ) } -function UpTip(prop: { - value: number - onChange: (tip: number) => void - disabled?: boolean -}) { - const { onChange, value, disabled } = prop - +function UpTip(props: { onClick?: () => void; value: number }) { + const { onClick, value } = props + const IconKind = value >= 10 ? ChevronDoubleRightIcon : ChevronRightIcon return ( )