[resumes][refactor] update invalidateQueries

pull/320/head
Terence Ho 3 years ago
parent 643c02d779
commit 306a70d944

@ -39,23 +39,21 @@ export default function CommentsForm({
skills: '', skills: '',
}, },
}); });
const reviewCreateMutation = trpc.useMutation('resumes.reviews.user.create');
const trpcContext = trpc.useContext(); const trpcContext = trpc.useContext();
const reviewCreateMutation = trpc.useMutation('resumes.reviews.user.create', {
onSuccess: () => {
// New review added, invalidate query to trigger refetch
trpcContext.invalidateQueries(['resumes.reviews.list']);
},
});
// TODO: Give a feedback to the user if the action succeeds/fails // TODO: Give a feedback to the user if the action succeeds/fails
const onSubmit: SubmitHandler<IFormInput> = async (data) => { const onSubmit: SubmitHandler<IFormInput> = async (data) => {
await reviewCreateMutation.mutate( await reviewCreateMutation.mutate({
{ resumeId,
resumeId, ...data,
...data, });
},
{
onSuccess: () => {
// New review added, invalidate query to trigger refetch
trpcContext.invalidateQueries(['resumes.reviews.list']);
},
},
);
// Redirect back to comments section // Redirect back to comments section
setShowCommentsForm(false); setShowCommentsForm(false);

@ -8,8 +8,6 @@ import Comment from './comment/Comment';
import CommentsListButton from './CommentsListButton'; import CommentsListButton from './CommentsListButton';
import { COMMENTS_SECTIONS } from './constants'; import { COMMENTS_SECTIONS } from './constants';
import type { ResumeComment } from '~/types/resume-comments';
type CommentsListProps = Readonly<{ type CommentsListProps = Readonly<{
resumeId: string; resumeId: string;
setShowCommentsForm: (show: boolean) => void; setShowCommentsForm: (show: boolean) => void;
@ -20,15 +18,16 @@ export default function CommentsList({
setShowCommentsForm, setShowCommentsForm,
}: CommentsListProps) { }: CommentsListProps) {
const [tab, setTab] = useState(COMMENTS_SECTIONS[0].value); const [tab, setTab] = useState(COMMENTS_SECTIONS[0].value);
const [comments, setComments] = useState<Array<ResumeComment>>([]);
const { data: session } = useSession(); const { data: session } = useSession();
// Fetch the most updated comments to render // Fetch the most updated comments to render
trpc.useQuery(['resumes.reviews.list', { resumeId, section: tab }], { const commentsQuery = trpc.useQuery([
onSuccess: setComments, 'resumes.reviews.list',
}); { resumeId, section: tab },
]);
// TODO: Add loading prompt // TODO: Add loading prompt
console.log(commentsQuery.data);
return ( return (
<div className="space-y-3"> <div className="space-y-3">
@ -41,7 +40,7 @@ export default function CommentsList({
/> />
<div className="m-2 flow-root h-[calc(100vh-20rem)] w-full flex-col space-y-3 overflow-y-scroll"> <div className="m-2 flow-root h-[calc(100vh-20rem)] w-full flex-col space-y-3 overflow-y-scroll">
{comments.map((comment) => { {commentsQuery.data?.map((comment) => {
return ( return (
<Comment <Comment
key={comment.id} key={comment.id}

Loading…
Cancel
Save