|
|
@@ -4,46 +4,36 @@ import { Types } from 'mongoose';
|
|
|
import { UncontrolledTooltip, Popover, PopoverBody } from 'reactstrap';
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
|
+import { IUser } from '../interfaces/user';
|
|
|
+
|
|
|
import UserPictureList from './User/UserPictureList';
|
|
|
import { toastError } from '~/client/util/apiNotification';
|
|
|
import { useIsGuestUser } from '~/stores/context';
|
|
|
-import { useSWRxBookmarksInfo } from '~/stores/bookmarks';
|
|
|
+import { useSWRBookmarkInfo } from '~/stores/bookmark';
|
|
|
import { apiv3Put } from '~/client/util/apiv3-client';
|
|
|
|
|
|
interface Props {
|
|
|
- pageId: Types.ObjectId
|
|
|
+ hideTotalNumber?: boolean
|
|
|
+ isBookmarked: boolean
|
|
|
+ sumOfBookmarks: number
|
|
|
+ bookmarkedUsers: IUser[]
|
|
|
+ onBookMarkClicked: ()=>void;
|
|
|
}
|
|
|
|
|
|
-const BookmarkButton: FC<Props> = (props: Props) => {
|
|
|
+const BookmarkButtons: FC<Props> = (props: Props) => {
|
|
|
const { t } = useTranslation();
|
|
|
- const { pageId } = props;
|
|
|
|
|
|
const [isPopoverOpen, setIsPopoverOpen] = useState(false);
|
|
|
|
|
|
const { data: isGuestUser } = useIsGuestUser();
|
|
|
- const { data: bookmarksInfo, mutate } = useSWRxBookmarksInfo(pageId);
|
|
|
-
|
|
|
- const isBookmarked = bookmarksInfo?.isBookmarked != null ? bookmarksInfo.isBookmarked : false;
|
|
|
- const sumOfBookmarks = bookmarksInfo?.sumOfBookmarks != null ? bookmarksInfo.sumOfBookmarks : 0;
|
|
|
- const bookmarkedUsers = bookmarksInfo?.bookmarkedUsers != null ? bookmarksInfo.bookmarkedUsers : [];
|
|
|
|
|
|
const togglePopover = () => {
|
|
|
setIsPopoverOpen(!isPopoverOpen);
|
|
|
};
|
|
|
|
|
|
const handleClick = async() => {
|
|
|
- if (isGuestUser) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- const res = await apiv3Put('/bookmarks', { pageId, bool: !isBookmarked });
|
|
|
- if (res) {
|
|
|
- mutate();
|
|
|
- }
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
- toastError(err);
|
|
|
+ if (props.onBookMarkClicked != null) {
|
|
|
+ props.onBookMarkClicked();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -54,9 +44,9 @@ const BookmarkButton: FC<Props> = (props: Props) => {
|
|
|
id="bookmark-button"
|
|
|
onClick={handleClick}
|
|
|
className={`btn btn-bookmark border-0
|
|
|
- ${isBookmarked ? 'active' : ''} ${isGuestUser ? 'disabled' : ''}`}
|
|
|
+ ${props.isBookmarked ? 'active' : ''} ${isGuestUser ? 'disabled' : ''}`}
|
|
|
>
|
|
|
- <i className="icon-star"></i>
|
|
|
+ <i className={`fa ${props.isBookmarked ? 'fa-bookmark' : 'fa-bookmark-o'}`}></i>
|
|
|
</button>
|
|
|
|
|
|
{isGuestUser && (
|
|
|
@@ -65,19 +55,22 @@ const BookmarkButton: FC<Props> = (props: Props) => {
|
|
|
</UncontrolledTooltip>
|
|
|
)}
|
|
|
|
|
|
- <button type="button" id="po-total-bookmarks" className={`btn btn-bookmark border-0 total-bookmarks ${isBookmarked ? 'active' : ''}`}>
|
|
|
- {sumOfBookmarks}
|
|
|
- </button>
|
|
|
-
|
|
|
- <Popover placement="bottom" isOpen={isPopoverOpen} target="po-total-bookmarks" toggle={togglePopover} trigger="legacy">
|
|
|
- <PopoverBody className="seen-user-popover">
|
|
|
- <div className="px-2 text-right user-list-content text-truncate text-muted">
|
|
|
- {bookmarkedUsers.length ? <UserPictureList users={bookmarkedUsers} /> : t('No users have bookmarked yet')}
|
|
|
- </div>
|
|
|
- </PopoverBody>
|
|
|
- </Popover>
|
|
|
+ { !props.hideTotalNumber && (
|
|
|
+ <>
|
|
|
+ <button type="button" id="po-total-bookmarks" className={`btn btn-bookmark border-0 total-bookmarks ${props.isBookmarked ? 'active' : ''}`}>
|
|
|
+ {props.sumOfBookmarks}
|
|
|
+ </button>
|
|
|
+ <Popover placement="bottom" isOpen={isPopoverOpen} target="po-total-bookmarks" toggle={togglePopover} trigger="legacy">
|
|
|
+ <PopoverBody className="seen-user-popover">
|
|
|
+ <div className="px-2 text-right user-list-content text-truncate text-muted">
|
|
|
+ {props.bookmarkedUsers.length ? <UserPictureList users={props.bookmarkedUsers} /> : t('No users have bookmarked yet')}
|
|
|
+ </div>
|
|
|
+ </PopoverBody>
|
|
|
+ </Popover>
|
|
|
+ </>
|
|
|
+ ) }
|
|
|
</div>
|
|
|
);
|
|
|
};
|
|
|
|
|
|
-export default BookmarkButton;
|
|
|
+export default BookmarkButtons;
|