Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Hook up bookmark create/delete

Tianzhou Chen267f4f6f4276
diff --git a/frontend/src/components/BookmarkListSidePanel.vue b/frontend/src/components/BookmarkListSidePanel.vue
index 67378e6116..2f9255d296 100644
--- a/frontend/src/components/BookmarkListSidePanel.vue
+++ b/frontend/src/components/BookmarkListSidePanel.vue
@@ -17,7 +17,7 @@
:to="item.link"
class="sidebar-link group flex items-center px-3 py-1 text-sm"
>
- {{ item.name }}
+ <span class="truncate">{{ item.name }}</span>
</router-link>
</div>
</template>
@@ -37,20 +37,9 @@ export default {
store.getters["auth/currentUser"]()
).value;
- const prepareBookmarkList = () => {
- store
- .dispatch("bookmark/fetchBookmarkListForUser", currentUser.id)
- .catch((error) => {
- console.log(error);
- });
- };
-
const bookmarkList = computed(() =>
store.getters["bookmark/bookmarkListByUser"](currentUser.id)
);
-
- watchEffect(prepareBookmarkList);
-
return {
bookmarkList,
};
diff --git a/frontend/src/components/Breadcrumb.vue b/frontend/src/components/Breadcrumb.vue
index 9fb9b00f10..cab11da050 100644
--- a/frontend/src/components/Breadcrumb.vue
+++ b/frontend/src/components/Breadcrumb.vue
@@ -54,7 +54,7 @@
@click.prevent="toggleBookmark"
>
<svg
- v-if="state.bookmarked"
+ v-if="bookmark"
class="h-6 w-6 text-yellow-400 hover:text-yellow-600"
x-description="Heroicon name: star"
xmlns="http://www.w3.org/2000/svg"
@@ -84,10 +84,10 @@
</template>
<script lang="ts">
-import { reactive, computed } from "vue";
+import { reactive, computed, ComputedRef } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
-import { RouterSlug } from "../types";
+import { RouterSlug, User, Bookmark } from "../types";
interface LocalState {
bookmarked: boolean;
@@ -103,13 +103,24 @@ export default {
components: {},
setup(props, ctx) {
const store = useStore();
+ const currentRoute = useRouter().currentRoute.value;
const state = reactive<LocalState>({
bookmarked: Math.random() > 0.5,
});
+ const currentUser: ComputedRef<User> = computed(() =>
+ store.getters["auth/currentUser"]()
+ );
+
+ const bookmark: ComputedRef<Bookmark> = computed(() =>
+ store.getters["bookmark/bookmarkByUserAndLink"](
+ currentUser.value.id,
+ currentRoute.path
+ )
+ );
+
const breadcrumbList = computed(() => {
- const currentRoute = useRouter().currentRoute.value;
const routeSlug: RouterSlug = store.getters["router/routeSlug"](
currentRoute
);
@@ -133,11 +144,28 @@ export default {
});
const toggleBookmark = () => {
- state.bookmarked = !state.bookmarked;
+ if (bookmark.value) {
+ store
+ .dispatch("bookmark/deleteBookmark", bookmark.value)
+ .catch((error) => {
+ console.log(error);
+ });
+ } else {
+ store
+ .dispatch("bookmark/createBookmark", {
+ name: breadcrumbList.value[breadcrumbList.value.length - 1].name,
+ link: currentRoute.path,
+ creatorId: currentUser.value.id,
+ })
+ .catch((error) => {
+ console.log(error);
+ });
+ }
};
return {
state,
+ bookmark,
breadcrumbList,
toggleBookmark,
};
diff --git a/frontend/src/components/ProvideContext.vue b/frontend/src/components/ProvideContext.vue
index 4904b86321..bef4a2de50 100644
--- a/frontend/src/components/ProvideContext.vue
+++ b/frontend/src/components/ProvideContext.vue
@@ -3,15 +3,25 @@
</template>
<script lang="ts">
+import { computed, ComputedRef } from "vue";
import { useStore } from "vuex";
+import { User } from "../types";
export default {
name: "ProvideContext",
async setup() {
const store = useStore();
+ const currentUser: ComputedRef<User> = computed(() =>
+ store.getters["auth/currentUser"]()
+ );
+
await store.dispatch("principal/fetchPrincipalList");
await store.dispatch("environment/fetchEnvironmentList");
+ await store.dispatch(
+ "bookmark/fetchBookmarkListByUser",
+ currentUser.value.id
+ );
},
};
</script>
diff --git a/frontend/src/miragejs/routes/index.js b/frontend/src/miragejs/routes/index.js
index 1a758c56c2..46cb1c43ef 100644
--- a/frontend/src/miragejs/routes/index.js
+++ b/frontend/src/miragejs/routes/index.js
@@ -376,6 +376,23 @@ export default function routes() {
});
});
+ this.post("/bookmark", function (schema, request) {
+ const newBookmark = {
+ ...this.normalizedRequestAttrs("bookmark"),
+ workspaceId: WORKSPACE_ID,
+ };
+ return schema.bookmarks.create(newBookmark);
+ });
+
+ this.patch("/bookmark/:bookmarkId", function (schema, request) {
+ const attrs = this.normalizedRequestAttrs("bookmark");
+ return schema.bookmarks.find(request.params.bookmarkId).update(attrs);
+ });
+
+ this.delete("/bookmark/:bookmarkId", function (schema, request) {
+ return schema.bookmarks.find(request.params.bookmarkId).destroy();
+ });
+
// Activity
this.get("/activity", function (schema, request) {
const {
diff --git a/frontend/src/miragejs/seeds/index.js b/frontend/src/miragejs/seeds/index.js
index 2cc0cc9c1c..264d9dc114 100644
--- a/frontend/src/miragejs/seeds/index.js
+++ b/frontend/src/miragejs/seeds/index.js
@@ -429,7 +429,7 @@ const workspacesSeeder = (server) => {
const task1 = server.schema.tasks.find(1);
server.create("bookmark", {
workspace: workspace1,
- name: "Task #" + task1.id,
+ name: task1.name,
link: `/task/${task1.id}`,
creatorId: user1.id,
});
@@ -438,7 +438,7 @@ const workspacesSeeder = (server) => {
const task2 = server.schema.tasks.find(2);
server.create("bookmark", {
workspace: workspace1,
- name: "Task #" + task2.id,
+ name: task2.name,
link: `/task/${task2.id}`,
creatorId: user1.id,
});
@@ -449,7 +449,7 @@ const workspacesSeeder = (server) => {
});
server.create("bookmark", {
workspace: workspace2,
- name: "Task #" + task3.id,
+ name: task3.name,
link: `/task/${task3.id}`,
creatorId: user1.id,
});
diff --git a/frontend/src/store/modules/bookmark.ts b/frontend/src/store/modules/bookmark.ts
index 16049470e1..789c833ace 100644
--- a/frontend/src/store/modules/bookmark.ts
+++ b/frontend/src/store/modules/bookmark.ts
@@ -1,5 +1,11 @@
import axios from "axios";
-import { UserId, Bookmark, BookmarkState, ResourceObject } from "../../types";
+import {
+ UserId,
+ Bookmark,
+ BookmarkNew,
+ BookmarkState,
+ ResourceObject,
+} from "../../types";
function convert(bookmark: ResourceObject): Bookmark {
return {
@@ -13,25 +19,78 @@ const state: () => BookmarkState = () => ({
});
const getters = {
- bookmarkListByUser: (state: BookmarkState) => (userId: UserId) => {
+ bookmarkListByUser: (state: BookmarkState) => (
+ userId: UserId
+ ): Bookmark[] | undefined => {
return state.bookmarkListByUser.get(userId);
},
+ bookmarkByUserAndLink: (state: BookmarkState) => (
+ userId: UserId,
+ link: string
+ ): Bookmark | undefined => {
+ const list = state.bookmarkListByUser.get(userId);
+ if (list) {
+ return list.find((item: Bookmark) => item.link == link);
+ }
+ return undefined;
+ },
};
const actions = {
- async fetchBookmarkListForUser({ commit }: any, userId: UserId) {
+ async fetchBookmarkListByUser({ commit }: any, userId: UserId) {
const bookmarkList = (
await axios.get(`/api/bookmark?userid=${userId}`)
).data.data.map((bookmark: ResourceObject) => {
return convert(bookmark);
});
- commit("setBookmarkListForUser", { userId, bookmarkList });
+ commit("setBookmarkListByUserId", { userId, bookmarkList });
return bookmarkList;
},
+
+ async createBookmark({ commit }: any, newBookmark: BookmarkNew) {
+ const createdBookmark = convert(
+ (
+ await axios.post(`/api/bookmark`, {
+ data: {
+ type: "bookmark",
+ attributes: newBookmark,
+ },
+ })
+ ).data.data
+ );
+
+ commit("appendBookmark", createdBookmark);
+
+ return createdBookmark;
+ },
+
+ async patchBookmark({ commit }: any, bookmark: Bookmark) {
+ const { id, ...attrs } = bookmark;
+ const updatedBookmark = convert(
+ (
+ await axios.patch(`/api/bookmark/${bookmark.id}`, {
+ data: {
+ type: "bookmark",
+ attributes: attrs,
+ },
+ })
+ ).data.data
+ );
+
+ commit("replaceBookmark", updatedBookmark);
+
+ return updatedBookmark;
+ },
+
+ async deleteBookmark({ commit }: any, bookmark: Bookmark) {
+ await axios.delete(`/api/bookmark/${bookmark.id}`);
+
+ commit("deleteBookmark", bookmark);
+ },
};
const mutations = {
- setBookmarkListForUser(
+ setBookmarkListByUserId(
state: BookmarkState,
{
userId,
@@ -43,6 +102,37 @@ const mutations = {
) {
state.bookmarkListByUser.set(userId, bookmarkList);
},
+
+ appendBookmark(state: BookmarkState, bookmark: Bookmark) {
+ const list = state.bookmarkListByUser.get(bookmark.creatorId);
+ if (list) {
+ list.push(bookmark);
+ } else {
+ state.bookmarkListByUser.set(bookmark.creatorId, [bookmark]);
+ }
+ },
+
+ replaceBookmark(state: BookmarkState, updatedBookmark: Bookmark) {
+ const list = state.bookmarkListByUser.get(updatedBookmark.creatorId);
+ if (list) {
+ const i = list.findIndex(
+ (item: Bookmark) => item.id == updatedBookmark.id
+ );
+ if (i != -1) {
+ list[i] = updatedBookmark;
+ }
+ }
+ },
+
+ deleteBookmark(state: BookmarkState, bookmark: Bookmark) {
+ const list = state.bookmarkListByUser.get(bookmark.creatorId);
+ if (list) {
+ const i = list.findIndex((item: Bookmark) => item.id == bookmark.id);
+ if (i != -1) {
+ list.splice(i, 1);
+ }
+ }
+ },
};
export default {