Skip to content

Commit

Permalink
chore: fix update shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Nov 22, 2023
1 parent 61b167e commit c85442d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions api/v2/shortcut_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ func (s *APIV2Service) UpdateShortcut(ctx context.Context, request *apiv2pb.Upda
update.Link = &request.Shortcut.Link
case "title":
update.Title = &request.Shortcut.Title
case "description":
update.Description = &request.Shortcut.Description
case "tags":
tag := strings.Join(request.Shortcut.Tags, " ")
update.Tag = &tag
case "description":
update.Description = &request.Shortcut.Description
case "visibility":
visibility := store.Visibility(request.Shortcut.Visibility.String())
update.Visibility = &visibility
Expand Down
10 changes: 7 additions & 3 deletions frontend/web/src/components/CreateShortcutDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { isUndefined, uniq } from "lodash-es";
import { useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import { useTranslation } from "react-i18next";
import useShortcutStore from "@/stores/v1/shortcut";
import useShortcutStore, { getShortcutUpdateMask } from "@/stores/v1/shortcut";
import { Visibility } from "@/types/proto/api/v2/common";
import { Shortcut } from "@/types/proto/api/v2/shortcut_service";
import { convertVisibilityFromPb } from "@/utils/visibility";
Expand Down Expand Up @@ -184,11 +184,15 @@ const CreateShortcutDrawer: React.FC<Props> = (props: Props) => {

try {
if (shortcutId) {
await shortcutStore.updateShortcut({
const updatingShortcut = {
...state.shortcutCreate,
id: shortcutId,
tags: tag.split(" ").filter(Boolean),
});
};
await shortcutStore.updateShortcut(
updatingShortcut,
getShortcutUpdateMask(shortcutStore.getShortcutById(updatingShortcut.id), updatingShortcut)
);
} else {
await shortcutStore.createShortcut({
...state.shortcutCreate,
Expand Down
32 changes: 30 additions & 2 deletions frontend/web/src/stores/v1/shortcut.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isEqual } from "lodash-es";
import { create } from "zustand";
import { shortcutServiceClient } from "@/grpcweb";
import { Shortcut } from "@/types/proto/api/v2/shortcut_service";
Expand All @@ -9,7 +10,7 @@ interface ShortcutState {
getShortcutById: (id: number) => Shortcut;
getShortcutList: () => Shortcut[];
createShortcut: (shortcut: Shortcut) => Promise<Shortcut>;
updateShortcut: (shortcut: Partial<Shortcut>) => Promise<Shortcut>;
updateShortcut: (shortcut: Partial<Shortcut>, updateMask: string[]) => Promise<Shortcut>;
deleteShortcut: (id: number) => Promise<void>;
}

Expand Down Expand Up @@ -60,9 +61,10 @@ const useShortcutStore = create<ShortcutState>()((set, get) => ({
set(shortcutMap);
return createdShortcut;
},
updateShortcut: async (shortcut: Partial<Shortcut>) => {
updateShortcut: async (shortcut: Partial<Shortcut>, updateMask: string[]) => {
const { shortcut: updatedShortcut } = await shortcutServiceClient.updateShortcut({
shortcut: shortcut,
updateMask,
});
if (!updatedShortcut) {
throw new Error(`Failed to update shortcut`);
Expand All @@ -87,4 +89,30 @@ const unknownShortcut: Shortcut = Shortcut.fromPartial({
name: "Unknown",
});

export const getShortcutUpdateMask = (shortcut: Shortcut, updatingShortcut: Shortcut) => {
const updateMask: string[] = [];
if (!isEqual(shortcut.name, updatingShortcut.name)) {
updateMask.push("name");
}
if (!isEqual(shortcut.link, updatingShortcut.link)) {
updateMask.push("link");
}
if (!isEqual(shortcut.title, updatingShortcut.title)) {
updateMask.push("title");
}
if (!isEqual(shortcut.description, updatingShortcut.description)) {
updateMask.push("description");
}
if (!isEqual(shortcut.tags, updatingShortcut.tags)) {
updateMask.push("tags");
}
if (!isEqual(shortcut.visibility, updatingShortcut.visibility)) {
updateMask.push("visibility");
}
if (!isEqual(shortcut.ogMetadata, updatingShortcut.ogMetadata)) {
updateMask.push("og_metadata");
}
return updateMask;
};

export default useShortcutStore;

0 comments on commit c85442d

Please sign in to comment.