[resumes][fix] use spinner for laggy star

pull/374/head
Wu Peirong 3 years ago
parent fd67a20a2b
commit 6413bb755e

@ -4,7 +4,6 @@ import Error from 'next/error';
import Head from 'next/head'; import Head from 'next/head';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { useSession } from 'next-auth/react'; import { useSession } from 'next-auth/react';
import { useEffect, useState } from 'react';
import { import {
AcademicCapIcon, AcademicCapIcon,
BriefcaseIcon, BriefcaseIcon,
@ -27,6 +26,7 @@ export default function ResumeReviewPage() {
const { data: session } = useSession(); const { data: session } = useSession();
const router = useRouter(); const router = useRouter();
const { resumeId } = router.query; const { resumeId } = router.query;
const utils = trpc.useContext();
// Safe to assert resumeId type as string because query is only sent if so // Safe to assert resumeId type as string because query is only sent if so
const detailsQuery = trpc.useQuery( const detailsQuery = trpc.useQuery(
['resumes.resume.findOne', { resumeId: resumeId as string }], ['resumes.resume.findOne', { resumeId: resumeId as string }],
@ -36,33 +36,14 @@ export default function ResumeReviewPage() {
); );
const starMutation = trpc.useMutation('resumes.resume.star', { const starMutation = trpc.useMutation('resumes.resume.star', {
onSuccess() { onSuccess() {
setStarDetails({ utils.invalidateQueries(['resumes.resume.findOne']);
isStarred: true,
numStars: starDetails.numStars + 1,
});
}, },
}); });
const unstarMutation = trpc.useMutation('resumes.resume.unstar', { const unstarMutation = trpc.useMutation('resumes.resume.unstar', {
onSuccess() { onSuccess() {
setStarDetails({ utils.invalidateQueries(['resumes.resume.findOne']);
isStarred: false,
numStars: starDetails.numStars - 1,
});
}, },
}); });
const [starDetails, setStarDetails] = useState({
isStarred: false,
numStars: 0,
});
useEffect(() => {
if (detailsQuery?.data !== undefined) {
setStarDetails({
isStarred: !!detailsQuery.data?.stars.length,
numStars: detailsQuery.data?._count.stars ?? 0,
});
}
}, [detailsQuery.data]);
const onStarButtonClick = () => { const onStarButtonClick = () => {
if (session?.user?.id == null) { if (session?.user?.id == null) {
@ -72,7 +53,7 @@ export default function ResumeReviewPage() {
// Star button only rendered if resume exists // Star button only rendered if resume exists
// Star button only clickable if user exists // Star button only clickable if user exists
if (starDetails.isStarred) { if (detailsQuery.data?.stars.length) {
unstarMutation.mutate({ unstarMutation.mutate({
resumeId: resumeId as string, resumeId: resumeId as string,
}); });
@ -104,30 +85,37 @@ export default function ResumeReviewPage() {
</h1> </h1>
<button <button
className={clsx( className={clsx(
starDetails.isStarred detailsQuery.data?.stars.length
? 'z-10 border-indigo-500 outline-none ring-1 ring-indigo-500' ? 'z-10 border-indigo-500 outline-none ring-1 ring-indigo-500'
: '', : '',
'isolate inline-flex max-h-10 items-center space-x-4 rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 disabled:hover:bg-white', 'isolate inline-flex max-h-10 items-center space-x-4 rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 shadow-sm hover:bg-gray-50 disabled:hover:bg-white',
)} )}
disabled={starMutation.isLoading || unstarMutation.isLoading} disabled={
id="star-button" session?.user === undefined ||
starMutation.isLoading ||
unstarMutation.isLoading
}
type="button" type="button"
onClick={onStarButtonClick}> onClick={onStarButtonClick}>
<span className="relative inline-flex"> <span className="relative inline-flex">
<StarIcon <div className="-ml-1 mr-2 h-5 w-5">
aria-hidden="true" {starMutation.isLoading || unstarMutation.isLoading ? (
className={clsx( <Spinner className="mt-0.5" size="xs" />
starDetails.isStarred ) : (
? 'text-orange-400' <StarIcon
: 'text-gray-400', aria-hidden="true"
'-ml-1 mr-2 h-5 w-5', className={clsx(
detailsQuery.data?.stars.length
? 'text-orange-400'
: 'text-gray-400',
)}
/>
)} )}
id="star-icon" </div>
/>
Star Star
</span> </span>
<span className="relative -ml-px inline-flex"> <span className="relative -ml-px inline-flex">
{starDetails.numStars} {detailsQuery.data?._count.stars}
</span> </span>
</button> </button>
</div> </div>

Loading…
Cancel
Save