mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-20 22:07:15 +00:00
chore: fix update shortcut
This commit is contained in:
parent
61b167ef66
commit
c85442d39f
@ -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
|
||||
|
@ -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";
|
||||
@ -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,
|
||||
|
@ -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";
|
||||
@ -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>;
|
||||
}
|
||||
|
||||
@ -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`);
|
||||
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user