mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-18 21:19:44 +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
|
update.Link = &request.Shortcut.Link
|
||||||
case "title":
|
case "title":
|
||||||
update.Title = &request.Shortcut.Title
|
update.Title = &request.Shortcut.Title
|
||||||
|
case "description":
|
||||||
|
update.Description = &request.Shortcut.Description
|
||||||
case "tags":
|
case "tags":
|
||||||
tag := strings.Join(request.Shortcut.Tags, " ")
|
tag := strings.Join(request.Shortcut.Tags, " ")
|
||||||
update.Tag = &tag
|
update.Tag = &tag
|
||||||
case "description":
|
|
||||||
update.Description = &request.Shortcut.Description
|
|
||||||
case "visibility":
|
case "visibility":
|
||||||
visibility := store.Visibility(request.Shortcut.Visibility.String())
|
visibility := store.Visibility(request.Shortcut.Visibility.String())
|
||||||
update.Visibility = &visibility
|
update.Visibility = &visibility
|
||||||
|
@ -16,7 +16,7 @@ import { isUndefined, uniq } from "lodash-es";
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
import { useTranslation } from "react-i18next";
|
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 { Visibility } from "@/types/proto/api/v2/common";
|
||||||
import { Shortcut } from "@/types/proto/api/v2/shortcut_service";
|
import { Shortcut } from "@/types/proto/api/v2/shortcut_service";
|
||||||
import { convertVisibilityFromPb } from "@/utils/visibility";
|
import { convertVisibilityFromPb } from "@/utils/visibility";
|
||||||
@ -184,11 +184,15 @@ const CreateShortcutDrawer: React.FC<Props> = (props: Props) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (shortcutId) {
|
if (shortcutId) {
|
||||||
await shortcutStore.updateShortcut({
|
const updatingShortcut = {
|
||||||
...state.shortcutCreate,
|
...state.shortcutCreate,
|
||||||
id: shortcutId,
|
id: shortcutId,
|
||||||
tags: tag.split(" ").filter(Boolean),
|
tags: tag.split(" ").filter(Boolean),
|
||||||
});
|
};
|
||||||
|
await shortcutStore.updateShortcut(
|
||||||
|
updatingShortcut,
|
||||||
|
getShortcutUpdateMask(shortcutStore.getShortcutById(updatingShortcut.id), updatingShortcut)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
await shortcutStore.createShortcut({
|
await shortcutStore.createShortcut({
|
||||||
...state.shortcutCreate,
|
...state.shortcutCreate,
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { isEqual } from "lodash-es";
|
||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
import { shortcutServiceClient } from "@/grpcweb";
|
import { shortcutServiceClient } from "@/grpcweb";
|
||||||
import { Shortcut } from "@/types/proto/api/v2/shortcut_service";
|
import { Shortcut } from "@/types/proto/api/v2/shortcut_service";
|
||||||
@ -9,7 +10,7 @@ interface ShortcutState {
|
|||||||
getShortcutById: (id: number) => Shortcut;
|
getShortcutById: (id: number) => Shortcut;
|
||||||
getShortcutList: () => Shortcut[];
|
getShortcutList: () => Shortcut[];
|
||||||
createShortcut: (shortcut: Shortcut) => Promise<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>;
|
deleteShortcut: (id: number) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,9 +61,10 @@ const useShortcutStore = create<ShortcutState>()((set, get) => ({
|
|||||||
set(shortcutMap);
|
set(shortcutMap);
|
||||||
return createdShortcut;
|
return createdShortcut;
|
||||||
},
|
},
|
||||||
updateShortcut: async (shortcut: Partial<Shortcut>) => {
|
updateShortcut: async (shortcut: Partial<Shortcut>, updateMask: string[]) => {
|
||||||
const { shortcut: updatedShortcut } = await shortcutServiceClient.updateShortcut({
|
const { shortcut: updatedShortcut } = await shortcutServiceClient.updateShortcut({
|
||||||
shortcut: shortcut,
|
shortcut: shortcut,
|
||||||
|
updateMask,
|
||||||
});
|
});
|
||||||
if (!updatedShortcut) {
|
if (!updatedShortcut) {
|
||||||
throw new Error(`Failed to update shortcut`);
|
throw new Error(`Failed to update shortcut`);
|
||||||
@ -87,4 +89,30 @@ const unknownShortcut: Shortcut = Shortcut.fromPartial({
|
|||||||
name: "Unknown",
|
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;
|
export default useShortcutStore;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user