|
17 | 17 | :contentNode="content"
|
18 | 18 | :thumbnailSrc="content.thumbnail"
|
19 | 19 | :headingLevel="cardsHeadingLevel"
|
| 20 | + :isBookmarked="isBookmarked(content.id)" |
| 21 | + @toggleBookmark="toggleBookmark" |
20 | 22 | >
|
21 | 23 | <template #belowTitle>
|
22 | 24 | <p v-if="contentCardMessage(content)">{{ contentCardMessage(content) }}</p>
|
|
71 | 73 |
|
72 | 74 | <script>
|
73 | 75 |
|
| 76 | + import { computed, ref } from 'vue'; |
| 77 | + import urls from 'kolibri/urls'; |
| 78 | + import client from 'kolibri/client'; |
| 79 | + import useUser from 'kolibri/composables/useUser'; |
| 80 | + import BookmarksResource from 'kolibri-common/apiResources/BookmarksResource'; |
74 | 81 | import commonCoreStrings from 'kolibri/uiText/commonCoreStrings';
|
75 | 82 | import AccessibleFolderCard from 'kolibri-common/components/Cards/AccessibleFolderCard';
|
76 | 83 | import AccessibleResourceCard from 'kolibri-common/components/Cards/AccessibleResourceCard';
|
|
84 | 91 | },
|
85 | 92 | mixins: [commonCoreStrings],
|
86 | 93 | setup() {
|
| 94 | + // Map of contentnode_id to bookmark resource ID |
| 95 | + const bookmarks = ref({}); |
| 96 | + // Map of contentNode IDs to bookmark resource IDs |
| 97 | + const bookmarkedContentNodeIds = computed(() => Object.keys(bookmarks.value)); |
| 98 | + const { currentUserId } = useUser(); |
| 99 | +
|
| 100 | + /** |
| 101 | + * Fetch bookmarks and store them in the bookmarks ref mapping |
| 102 | + * their contentnode_id to the bokomark's own ID. |
| 103 | + * The contentnode_id is used for creating it, but we need the |
| 104 | + * bookmark's ID to delete it. |
| 105 | + */ |
| 106 | + function getBookmarks() { |
| 107 | + BookmarksResource.fetchCollection({ force: true }).then(data => { |
| 108 | + bookmarks.value = data.reduce((memo, bookmark) => { |
| 109 | + memo[bookmark.contentnode_id] = bookmark.id; |
| 110 | + return memo; |
| 111 | + }, {}); |
| 112 | + }); |
| 113 | + } |
| 114 | +
|
| 115 | + function deleteBookmark(contentnode_id) { |
| 116 | + client({ |
| 117 | + method: 'delete', |
| 118 | + url: urls['kolibri:core:bookmarks_detail'](contentnode_id), |
| 119 | + }).then(() => { |
| 120 | + getBookmarks(); |
| 121 | + }); |
| 122 | + } |
| 123 | +
|
| 124 | + function addBookmark(contentnode_id) { |
| 125 | + client({ |
| 126 | + method: 'post', |
| 127 | + url: urls['kolibri:core:bookmarks_list'](), |
| 128 | + data: { |
| 129 | + contentnode_id: contentnode_id, |
| 130 | + user: currentUserId.value, |
| 131 | + }, |
| 132 | + }).then(() => { |
| 133 | + getBookmarks(); |
| 134 | + }); |
| 135 | + } |
| 136 | +
|
| 137 | + function isBookmarked(contentnode_id) { |
| 138 | + return bookmarkedContentNodeIds.value.includes(contentnode_id); |
| 139 | + } |
| 140 | +
|
| 141 | + function toggleBookmark(contentnode_id) { |
| 142 | + if (isBookmarked(contentnode_id)) { |
| 143 | + const bookmarkId = bookmarks.value[contentnode_id]; |
| 144 | + deleteBookmark(bookmarkId); |
| 145 | + } else { |
| 146 | + addBookmark(contentnode_id); |
| 147 | + } |
| 148 | + } |
| 149 | +
|
| 150 | + getBookmarks(); |
87 | 151 | return {
|
88 | 152 | ViewMoreButtonStates,
|
| 153 | + toggleBookmark, |
| 154 | + isBookmarked, |
89 | 155 | };
|
90 | 156 | },
|
91 | 157 | props: {
|
|
0 commit comments