[resumes][refactor] Abstract out sign in button

pull/349/head
Tan Su Yin 3 years ago
parent a47b1b7472
commit 001a18649b

@ -0,0 +1,24 @@
import { signIn } from 'next-auth/react';
type Props = Readonly<{
text: string;
}>;
export default function SignInButton({ text }: Props) {
return (
<div className="flex justify-center pt-4">
<p>
<a
className="text-primary-800 hover:text-primary-500"
href="/api/auth/signin"
onClick={(event) => {
event.preventDefault();
signIn();
}}>
Sign in
</a>{' '}
{text}
</p>
</div>
);
}

@ -1,12 +1,13 @@
import { useSession } from 'next-auth/react'; import { useSession } from 'next-auth/react';
import { useState } from 'react'; import { useState } from 'react';
import { Tabs } from '@tih/ui'; import { Tabs } from '@tih/ui';
import { Button } from '@tih/ui';
import { trpc } from '~/utils/trpc'; import { trpc } from '~/utils/trpc';
import Comment from './comment/Comment'; import Comment from './comment/Comment';
import CommentsListButton from './CommentsListButton';
import { COMMENTS_SECTIONS } from './constants'; import { COMMENTS_SECTIONS } from './constants';
import SignInButton from '../SignInButton';
type CommentsListProps = Readonly<{ type CommentsListProps = Readonly<{
resumeId: string; resumeId: string;
@ -22,11 +23,25 @@ export default function CommentsList({
const commentsQuery = trpc.useQuery(['resumes.reviews.list', { resumeId }]); const commentsQuery = trpc.useQuery(['resumes.reviews.list', { resumeId }]);
const renderButton = () => {
if (session === null) {
return <SignInButton text="to join discussion" />;
}
return (
<Button
display="block"
label="Add your review"
variant="tertiary"
onClick={() => setShowCommentsForm(true)}
/>
);
};
// TODO: Add loading prompt // TODO: Add loading prompt
return ( return (
<div className="space-y-3"> <div className="space-y-3">
<CommentsListButton setShowCommentsForm={setShowCommentsForm} /> {renderButton()}
<Tabs <Tabs
label="comments" label="comments"
tabs={COMMENTS_SECTIONS} tabs={COMMENTS_SECTIONS}

@ -1,48 +0,0 @@
import { signIn, useSession } from 'next-auth/react';
import { Button } from '@tih/ui';
type CommentsListButtonProps = {
setShowCommentsForm: (show: boolean) => void;
};
export default function CommentsListButton({
setShowCommentsForm,
}: CommentsListButtonProps) {
const { data: session, status } = useSession();
const isSessionLoading = status === 'loading';
// Don't render anything
if (isSessionLoading) {
return null;
}
// Not signed in
if (session == null) {
return (
<div className="flex justify-center">
<p>
<a
className="text-primary-800 hover:text-primary-500"
href="/api/auth/signin"
onClick={(event) => {
event.preventDefault();
signIn();
}}>
Sign in
</a>{' '}
to join discussion
</p>
</div>
);
}
// Signed in. Return Add review button
return (
<Button
display="block"
label="Add your review"
variant="tertiary"
onClick={() => setShowCommentsForm(true)}
/>
);
}
Loading…
Cancel
Save