add ability to set delete_video_after_link_expires

This commit is contained in:
MarconLP 2023-04-14 15:49:07 +02:00
parent 62555101b2
commit a52dfcc7fb
No known key found for this signature in database
GPG key ID: A08A9C8B623F5EA5
3 changed files with 79 additions and 26 deletions

View file

@ -23,7 +23,7 @@ model Video {
video_url String video_url String
userId String userId String
sharing Boolean @default(false) sharing Boolean @default(false)
deleteAfterExpiry Boolean @default(false) delete_after_link_expires Boolean @default(false)
shareExpiryAt DateTime? shareExpiryAt DateTime?
linkShareSeo Boolean @default(false) linkShareSeo Boolean @default(false)
user User @relation(fields: [userId], references: [id], onDelete: Cascade) user User @relation(fields: [userId], references: [id], onDelete: Cascade)

View file

@ -20,7 +20,28 @@ export function ShareModal({ video }: Props) {
} }
return { previousValue }; return { previousValue };
}, },
onError: async (err, { videoId }, context) => { onError: (err, { videoId }, context) => {
if (context?.previousValue) {
utils.video.get.setData({ videoId }, context.previousValue);
}
console.error(err.message);
},
});
const setDeleteAfterLinkExpiresMutation =
api.video.setDeleteAfterLinkExpires.useMutation({
onMutate: async ({ videoId, delete_after_link_expires }) => {
await utils.video.get.cancel();
const previousValue = utils.video.get.getData({ videoId });
if (previousValue) {
utils.video.get.setData(
{ videoId },
{ ...previousValue, delete_after_link_expires }
);
}
return { previousValue };
},
onError: (err, { videoId }, context) => {
if (context?.previousValue) { if (context?.previousValue) {
utils.video.get.setData({ videoId }, context.previousValue); utils.video.get.setData({ videoId }, context.previousValue);
} }
@ -104,25 +125,33 @@ export function ShareModal({ video }: Props) {
{linkCopied ? "Copied!" : "Copy public link"} {linkCopied ? "Copied!" : "Copy public link"}
</button> </button>
<div className="w-full border border-solid border-[#e9ebf0] bg-[#fafbfc] px-[15px] py-3 text-xs"> <div className="w-full border border-solid border-[#e9ebf0] bg-[#fafbfc] px-[15px] py-3 text-xs">
{/*<div className="flex h-6 items-center justify-between">*/} <div className="flex h-6 items-center justify-between">
{/* <span>Expire link</span>*/} <span>Expire link</span>
<button className="h-6 rounded border border-solid border-[#d5d9df] bg-white px-[7px] font-medium">
{/* <button className="h-6 rounded border border-solid border-[#d5d9df] bg-white px-[7px] font-medium">*/} Never expire
{/* Never expire*/} </button>
{/* </button>*/} </div>
{/*</div>*/}
{/*<div className="mt-3 flex h-6 items-center justify-between">*/}
{/* <span>Delete video when expired</span>*/}
{/* <ModernSwitch enabled={s} toggle={ss} />*/}
{/*</div>*/}
<div className="mt-3 flex h-6 items-center justify-between"> <div className="mt-3 flex h-6 items-center justify-between">
<span>Share link with search engines</span> <span>Delete video after link expires</span>
<ModernSwitch <ModernSwitch
enabled={video.linkShareSeo} enabled={video.delete_after_link_expires}
toggle={() => console.log("test")} toggle={() =>
setDeleteAfterLinkExpiresMutation.mutate({
videoId: video.id,
delete_after_link_expires:
!video.delete_after_link_expires,
})
}
/> />
</div> </div>
{/*<div className="mt-3 flex h-6 items-center justify-between">*/} {/*<div className="mt-3 flex h-6 items-center justify-between">*/}
{/* <span>Share link with search engines</span>*/}
{/* <ModernSwitch*/}
{/* enabled={video.linkShareSeo}*/}
{/* toggle={() => console.log("test")}*/}
{/* />*/}
{/*</div>*/}
{/*<div className="mt-3 flex h-6 items-center justify-between">*/}
{/* <span>Embed code</span>*/} {/* <span>Embed code</span>*/}
{/* <button className="h-6 rounded border border-solid border-[#d5d9df] bg-white px-[7px] font-medium">*/} {/* <button className="h-6 rounded border border-solid border-[#d5d9df] bg-white px-[7px] font-medium">*/}
{/* Copy code*/} {/* Copy code*/}

View file

@ -95,6 +95,30 @@ export const videoRouter = createTRPCRouter({
throw new TRPCError({ code: "FORBIDDEN" }); throw new TRPCError({ code: "FORBIDDEN" });
} }
return {
success: true,
updateVideo,
};
}),
setDeleteAfterLinkExpires: protectedProcedure
.input(
z.object({ videoId: z.string(), delete_after_link_expires: z.boolean() })
)
.mutation(async ({ ctx, input }) => {
const updateVideo = await ctx.prisma.video.updateMany({
where: {
id: input.videoId,
userId: ctx.session.user.id,
},
data: {
delete_after_link_expires: input.delete_after_link_expires,
},
});
if (updateVideo.count === 0) {
throw new TRPCError({ code: "FORBIDDEN" });
}
return { return {
success: true, success: true,
updateVideo, updateVideo,