Skip to content

Commit 48e8913

Browse files
committed
handle bookmarking in lesson resource selection KCards
1 parent 743a6af commit 48e8913

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

kolibri/plugins/coach/assets/src/views/lessons/LessonResourceSelectionPage/ContentCardList.vue

+66
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
:contentNode="content"
1818
:thumbnailSrc="content.thumbnail"
1919
:headingLevel="cardsHeadingLevel"
20+
:isBookmarked="isBookmarked(content.id)"
21+
@toggleBookmark="toggleBookmark"
2022
>
2123
<template #belowTitle>
2224
<p v-if="contentCardMessage(content)">{{ contentCardMessage(content) }}</p>
@@ -71,6 +73,11 @@
7173

7274
<script>
7375
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';
7481
import commonCoreStrings from 'kolibri/uiText/commonCoreStrings';
7582
import AccessibleFolderCard from 'kolibri-common/components/Cards/AccessibleFolderCard';
7683
import AccessibleResourceCard from 'kolibri-common/components/Cards/AccessibleResourceCard';
@@ -84,8 +91,67 @@
8491
},
8592
mixins: [commonCoreStrings],
8693
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();
87151
return {
88152
ViewMoreButtonStates,
153+
toggleBookmark,
154+
isBookmarked,
89155
};
90156
},
91157
props: {

packages/kolibri-common/components/Cards/AccessibleResourceCard.vue

+5-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
:color="$themePalette.grey.v_700"
3535
:ariaLabel="coreString('savedFromBookmarks')"
3636
:tooltip="coreString('savedFromBookmarks')"
37-
@click.stop="isBookmarked = !isBookmarked"
37+
@click.stop="$emit('toggleBookmark', contentNode.id)"
3838
/>
3939
</div>
4040
</template>
@@ -58,7 +58,6 @@
5858
mixins: [commonCoreStrings],
5959
setup() {
6060
const { windowBreakpoint } = useKResponsiveWindow();
61-
6261
return {
6362
windowBreakpoint,
6463
};
@@ -73,6 +72,10 @@
7372
type: Object,
7473
required: true,
7574
},
75+
isBookmarked: {
76+
type: Boolean,
77+
default: false,
78+
},
7679
headingLevel: {
7780
type: Number,
7881
required: true,
@@ -86,12 +89,6 @@
8689
default: 'centerInside',
8790
},
8891
},
89-
data() {
90-
return {
91-
isBookmarked: false,
92-
};
93-
},
94-
computed: {},
9592
};
9693
9794
</script>

0 commit comments

Comments
 (0)