|
|
|
@ -1,30 +1,102 @@
|
|
|
|
|
import { signIn, useSession } from 'next-auth/react';
|
|
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { ClipboardDocumentIcon, ShareIcon } from '@heroicons/react/24/outline';
|
|
|
|
|
import { Button, HorizontalDivider, Spinner, TextArea } from '@tih/ui';
|
|
|
|
|
|
|
|
|
|
import ExpandableCommentCard from './comments/ExpandableCommentCard';
|
|
|
|
|
import ExpandableCommentCard from '~/components/offers/profile/comments/ExpandableCommentCard';
|
|
|
|
|
|
|
|
|
|
import { trpc } from '~/utils/trpc';
|
|
|
|
|
|
|
|
|
|
import type { OffersDiscussion, Reply } from '~/types/offers';
|
|
|
|
|
|
|
|
|
|
type ProfileHeaderProps = Readonly<{
|
|
|
|
|
handleCopyEditLink: () => void;
|
|
|
|
|
handleCopyPublicLink: () => void;
|
|
|
|
|
isDisabled: boolean;
|
|
|
|
|
isEditable: boolean;
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
profileId: string;
|
|
|
|
|
profileName?: string;
|
|
|
|
|
token?: string;
|
|
|
|
|
}>;
|
|
|
|
|
|
|
|
|
|
export default function ProfileComments({
|
|
|
|
|
handleCopyEditLink,
|
|
|
|
|
handleCopyPublicLink,
|
|
|
|
|
isDisabled,
|
|
|
|
|
isEditable,
|
|
|
|
|
isLoading,
|
|
|
|
|
profileId,
|
|
|
|
|
profileName,
|
|
|
|
|
token,
|
|
|
|
|
}: ProfileHeaderProps) {
|
|
|
|
|
function handleReply(replayingToId: string, userId: string) {
|
|
|
|
|
return replayingToId + userId; // To integrate with API
|
|
|
|
|
const { data: session, status } = useSession();
|
|
|
|
|
const [currentReply, setCurrentReply] = useState<string>('');
|
|
|
|
|
const [replies, setReplies] = useState<Array<Reply>>();
|
|
|
|
|
|
|
|
|
|
const commentsQuery = trpc.useQuery(
|
|
|
|
|
['offers.comments.getComments', { profileId }],
|
|
|
|
|
{
|
|
|
|
|
onSuccess(response: OffersDiscussion) {
|
|
|
|
|
setReplies(response.data);
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const trpcContext = trpc.useContext();
|
|
|
|
|
const createCommentMutation = trpc.useMutation(['offers.comments.create'], {
|
|
|
|
|
onSuccess() {
|
|
|
|
|
trpcContext.invalidateQueries([
|
|
|
|
|
'offers.comments.getComments',
|
|
|
|
|
{ profileId },
|
|
|
|
|
]);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function handleComment(message: string) {
|
|
|
|
|
if (isEditable) {
|
|
|
|
|
// If it is with edit permission, send comment to API with username = null
|
|
|
|
|
createCommentMutation.mutate(
|
|
|
|
|
{
|
|
|
|
|
message,
|
|
|
|
|
profileId,
|
|
|
|
|
token,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
setCurrentReply('');
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} else if (status === 'authenticated') {
|
|
|
|
|
// If not the OP and logged in, send comment to API
|
|
|
|
|
createCommentMutation.mutate(
|
|
|
|
|
{
|
|
|
|
|
message,
|
|
|
|
|
profileId,
|
|
|
|
|
userId: session.user?.id,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
setCurrentReply('');
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
// If not the OP and not logged in, direct users to log in
|
|
|
|
|
signIn();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleCopyEditLink() {
|
|
|
|
|
// TODO: Add notification
|
|
|
|
|
navigator.clipboard.writeText(
|
|
|
|
|
`${window.location.origin}/offers/profile/${profileId}?token=${token}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleComment() {
|
|
|
|
|
return 'profileId'; // To integrate with API
|
|
|
|
|
function handleCopyPublicLink() {
|
|
|
|
|
navigator.clipboard.writeText(
|
|
|
|
|
`${window.location.origin}/offers/profile/${profileId}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="col-span-10 pt-4">
|
|
|
|
@ -33,7 +105,7 @@ export default function ProfileComments({
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<div className="m-4">
|
|
|
|
|
<div className="m-4 h-full">
|
|
|
|
|
<div className="flex-end flex justify-end space-x-4">
|
|
|
|
|
{isEditable && (
|
|
|
|
|
<Button
|
|
|
|
@ -61,57 +133,41 @@ export default function ProfileComments({
|
|
|
|
|
<h2 className="mt-2 mb-6 text-2xl font-bold">Discussions</h2>
|
|
|
|
|
<div>
|
|
|
|
|
<TextArea
|
|
|
|
|
label="Comment as anonymous"
|
|
|
|
|
label={`Comment as ${
|
|
|
|
|
isEditable ? profileName : session?.user?.name ?? 'anonymous'
|
|
|
|
|
}`}
|
|
|
|
|
placeholder="Type your comment here"
|
|
|
|
|
value={currentReply}
|
|
|
|
|
onChange={(value) => setCurrentReply(value)}
|
|
|
|
|
/>
|
|
|
|
|
<div className="mt-2 flex w-full justify-end">
|
|
|
|
|
<div className="w-fit">
|
|
|
|
|
<Button
|
|
|
|
|
disabled={commentsQuery.isLoading}
|
|
|
|
|
display="block"
|
|
|
|
|
isLabelHidden={false}
|
|
|
|
|
isLoading={createCommentMutation.isLoading}
|
|
|
|
|
label="Comment"
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="primary"
|
|
|
|
|
onClick={handleComment}
|
|
|
|
|
onClick={() => handleComment(currentReply)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<HorizontalDivider />
|
|
|
|
|
</div>
|
|
|
|
|
<ExpandableCommentCard
|
|
|
|
|
comment={{
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
id: '1',
|
|
|
|
|
message: 'Test comment',
|
|
|
|
|
profileId: '123',
|
|
|
|
|
replies: [
|
|
|
|
|
{
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
id: '123',
|
|
|
|
|
message: 'Test comment',
|
|
|
|
|
profileId: '123',
|
|
|
|
|
replies: undefined,
|
|
|
|
|
replyingToId: '12345',
|
|
|
|
|
userId: '12314',
|
|
|
|
|
username: 'nihao username',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
id: '12334',
|
|
|
|
|
message: 'Test comment',
|
|
|
|
|
profileId: '123',
|
|
|
|
|
replies: undefined,
|
|
|
|
|
replyingToId: '12345',
|
|
|
|
|
userId: '12314',
|
|
|
|
|
username: 'nihao username',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
replyingToId: '12345',
|
|
|
|
|
userId: '12314',
|
|
|
|
|
username: 'nihao username',
|
|
|
|
|
}}
|
|
|
|
|
handleReply={handleReply}
|
|
|
|
|
/>
|
|
|
|
|
<div className="h-full overflow-y-scroll">
|
|
|
|
|
<div className="h-content mb-96 w-full">
|
|
|
|
|
{replies?.map((reply: Reply) => (
|
|
|
|
|
<ExpandableCommentCard
|
|
|
|
|
key={reply.id}
|
|
|
|
|
comment={reply}
|
|
|
|
|
profileId={profileId}
|
|
|
|
|
token={isEditable ? token : undefined}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|