From 62555101b2c16b35664a4a68802066506f1ad18f Mon Sep 17 00:00:00 2001 From: MarconLP <13001502+MarconLP@users.noreply.github.com> Date: Fri, 14 Apr 2023 15:34:14 +0200 Subject: [PATCH] rollback if an error occurs while setting share state --- src/components/ShareModal.tsx | 6 ++++++ src/server/api/routers/video.ts | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/components/ShareModal.tsx b/src/components/ShareModal.tsx index e684cfa..f8c0005 100644 --- a/src/components/ShareModal.tsx +++ b/src/components/ShareModal.tsx @@ -20,6 +20,12 @@ export function ShareModal({ video }: Props) { } return { previousValue }; }, + onError: async (err, { videoId }, context) => { + if (context?.previousValue) { + utils.video.get.setData({ videoId }, context.previousValue); + } + console.error(err.message); + }, }); const [linkCopied, setLinkCopied] = useState(false); diff --git a/src/server/api/routers/video.ts b/src/server/api/routers/video.ts index 5d71b46..1946d49 100644 --- a/src/server/api/routers/video.ts +++ b/src/server/api/routers/video.ts @@ -91,6 +91,10 @@ export const videoRouter = createTRPCRouter({ }, }); + if (updateVideo.count === 0) { + throw new TRPCError({ code: "FORBIDDEN" }); + } + return { success: true, updateVideo,