forked from amanintech/amansharma.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement simple feedback for wzulfikar#8.
- Loading branch information
Showing
7 changed files
with
242 additions
and
6 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { useState, useEffect, useRef } from 'react' | ||
import format from 'comma-number' | ||
|
||
export function SimpleFeedback({ slug }) { | ||
const [helpful, setHelpful] = useState(null) | ||
const [count, setCount] = useState({ helpful: 0, not_helpful: 0 }) | ||
|
||
const uuidRef = useRef(null) | ||
const mountedRef = useRef(null) | ||
|
||
useEffect(() => { | ||
mountedRef.current = true | ||
import('device-uuid').then((mod) => { | ||
const uuid = new mod.DeviceUUID().get() | ||
uuidRef.current = uuid | ||
syncFeedback(uuid) | ||
}) | ||
|
||
return () => { | ||
mountedRef.current = false | ||
} | ||
}, [slug]) | ||
|
||
function syncFeedback(uuid) { | ||
fetch(`/api/feedbacks/${slug}?uuid=${uuid}`) | ||
.then((res) => res.json()) | ||
.then( | ||
({ helpful: countHelpful, not_helpful: countNotHelpful, feedback }) => { | ||
if (!mountedRef.current) return | ||
|
||
const userFeedback = | ||
feedback === 'helpful' | ||
? true | ||
: feedback === 'not_helpful' | ||
? false | ||
: null | ||
|
||
if (userFeedback !== helpful) { | ||
setHelpful(userFeedback) | ||
} | ||
|
||
if ( | ||
count.helpful !== countHelpful || | ||
count.not_helpful !== countNotHelpful | ||
) { | ||
setCount({ helpful: countHelpful, not_helpful: countNotHelpful }) | ||
} | ||
} | ||
) | ||
} | ||
|
||
function sendFeedback(isHelpful) { | ||
// Add feedback or remove | ||
const prevState = | ||
helpful === true ? 'helpful' : helpful === false ? 'not_helpful' : null | ||
const newVal = helpful === isHelpful ? null : isHelpful | ||
|
||
setHelpful(newVal) | ||
|
||
// Optimistic update | ||
const newCount = { ...count } | ||
if (newVal === true) { | ||
newCount.helpful++ | ||
} else if (newVal === false) { | ||
newCount.not_helpful++ | ||
} | ||
|
||
if (prevState !== null) { | ||
newCount[prevState]-- | ||
} | ||
setCount(newCount) | ||
|
||
fetch(`/api/feedbacks/${slug}`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
uuid: uuidRef.current, | ||
helpful: newVal, | ||
prevState: prevState | ||
}) | ||
}).then((res) => { | ||
syncFeedback(uuidRef.current) | ||
}) | ||
} | ||
|
||
return ( | ||
<> | ||
<style jsx>{` | ||
.container { | ||
display: flex; | ||
align-items: baseline; | ||
} | ||
.btn-feedback { | ||
margin: 0px 5px; | ||
padding: 0; | ||
background: none; | ||
border: none; | ||
cursor: pointer; | ||
font-size: unset; | ||
} | ||
@media only screen and (max-width: 600px) { | ||
.container { | ||
flex-direction: column; | ||
} | ||
.btn-feedback:first-child { | ||
margin-left: 0px; | ||
} | ||
} | ||
`}</style> | ||
<div className='container'> | ||
<div>Was this helpful?</div> | ||
<div> | ||
<button | ||
className='btn-feedback' | ||
onClick={() => sendFeedback(true)} | ||
style={{ | ||
color: helpful === true ? '#4c8bf3' : 'unset', | ||
textDecoration: helpful === true ? 'underline 2px' : undefined | ||
}} | ||
> | ||
Yes{' '} | ||
{helpful === null || | ||
(count.helpful > 0 && `(${format(count.helpful)})`)} | ||
</button> | ||
<span>·</span> | ||
<button | ||
className='btn-feedback' | ||
onClick={() => sendFeedback(false)} | ||
style={{ | ||
color: helpful === false ? '#4c8bf3' : 'unset', | ||
textDecoration: helpful === false ? 'underline 2px' : undefined | ||
}} | ||
> | ||
No{' '} | ||
{helpful === null || | ||
(count.not_helpful > 0 && `(${format(count.not_helpful)})`)} | ||
</button> | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as firestore from '@google-cloud/firestore' | ||
import * as db from '@lib/db' | ||
|
||
export default async function handler(req, res) { | ||
if (req.method === 'POST') { | ||
const { uuid, helpful, prevState } = req.body | ||
|
||
const increment = firestore.FieldValue.increment(1) | ||
const decrement = firestore.FieldValue.increment(-1) | ||
|
||
const slugRef = db.feedbacks.doc(req.query.slug) | ||
const voterRef = db.db | ||
.collection(`/${db.collections.feedbacks}/${req.query.slug}/voters`) | ||
.doc(uuid) | ||
|
||
await db.db.runTransaction(async (tx) => { | ||
const feedback = | ||
helpful === true ? 'helpful' : helpful === false ? 'not_helpful' : null | ||
|
||
const payload = { | ||
count: helpful === null ? decrement : increment | ||
} as any | ||
|
||
if (feedback === 'helpful') { | ||
payload.helpful = increment | ||
} else if (feedback === 'not_helpful') { | ||
payload.not_helpful = increment | ||
} | ||
|
||
if (prevState !== null) { | ||
payload[prevState] = decrement | ||
} | ||
|
||
tx.set(slugRef, payload, { merge: true }) | ||
tx.set(voterRef, { feedback }, { merge: true }) | ||
}) | ||
|
||
const data = (await slugRef.get()).data() | ||
|
||
return res.status(200).json({ | ||
helpful: data.helpful, | ||
not_helpful: data.not_helpful | ||
}) | ||
} | ||
|
||
if (req.method === 'GET') { | ||
const { slug, uuid } = req.query | ||
const [snapshot, voter] = await Promise.all([ | ||
db.feedbacks.doc(slug).get(), | ||
db.db | ||
.collection(`/${db.collections.feedbacks}/${req.query.slug}/voters`) | ||
.doc(uuid) | ||
.get() | ||
]) | ||
|
||
const { helpful, not_helpful } = snapshot.data() || {} | ||
const feedback = voter.data()?.feedback || null | ||
|
||
return res.status(200).json({ helpful, not_helpful, feedback }) | ||
} | ||
} |
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