From ea33309b76ca3b9d20a33cc9abb032e625672e62 Mon Sep 17 00:00:00 2001 From: MarconLP <13001502+MarconLP@users.noreply.github.com> Date: Fri, 14 Apr 2023 17:06:07 +0200 Subject: [PATCH] add ability to setShareLinkExpiresAt value --- prisma/schema.prisma | 2 +- src/components/ExpireDateSelectMenu.tsx | 51 +++++++++++++++++++++---- src/components/ShareModal.tsx | 3 +- src/server/api/routers/video.ts | 22 +++++++++++ src/utils/use-popper.ts | 41 -------------------- 5 files changed, 69 insertions(+), 50 deletions(-) delete mode 100644 src/utils/use-popper.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 16632ca..97d8a33 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -24,7 +24,7 @@ model Video { userId String sharing Boolean @default(false) delete_after_link_expires Boolean @default(false) - shareExpiryAt DateTime? + shareLinkExpiresAt DateTime? linkShareSeo Boolean @default(false) user User @relation(fields: [userId], references: [id], onDelete: Cascade) diff --git a/src/components/ExpireDateSelectMenu.tsx b/src/components/ExpireDateSelectMenu.tsx index cff0ea2..221dee9 100644 --- a/src/components/ExpireDateSelectMenu.tsx +++ b/src/components/ExpireDateSelectMenu.tsx @@ -1,14 +1,51 @@ import { Listbox, Transition } from "@headlessui/react"; -import { Fragment, useState } from "react"; +import { Fragment } from "react"; import { getTime } from "~/utils/getTime"; +import { api } from "~/utils/api"; interface Props { - shareExpiryAt: Date | null; + shareLinkExpiresAt: Date | null; + videoId: string; } -export default function ExpireDateSelectMenu({ shareExpiryAt }: Props) { - const handleChange = (value) => { +export default function ExpireDateSelectMenu({ + shareLinkExpiresAt, + videoId, +}: Props) { + const utils = api.useContext(); + + const setShareLinkExpiresAtMutation = + api.video.setShareLinkExpiresAt.useMutation({ + onMutate: async ({ videoId, shareLinkExpiresAt }) => { + await utils.video.get.cancel(); + const previousValue = utils.video.get.getData({ videoId }); + if (previousValue) { + utils.video.get.setData( + { videoId }, + { ...previousValue, shareLinkExpiresAt } + ); + } + return { previousValue }; + }, + onError: (err, { videoId }, context) => { + if (context?.previousValue) { + utils.video.get.setData({ videoId }, context.previousValue); + } + console.error(err.message); + }, + }); + const handleChange = (value: string) => { console.log("selecting value", value); + + const now = new Date(); + now.setHours(now.getHours() + 1); + + console.log(now); + + setShareLinkExpiresAtMutation.mutate({ + videoId, + shareLinkExpiresAt: now, + }); }; const availableExpireDates = [ @@ -21,12 +58,12 @@ export default function ExpireDateSelectMenu({ shareExpiryAt }: Props) { return (