From ec1d32b7c901d8f8897e5579d438586e3d047606 Mon Sep 17 00:00:00 2001 From: Avi Date: Mon, 5 Feb 2024 09:58:34 +0200 Subject: [PATCH 1/4] added download image functionality --- .../post/components/options/index.tsx | 41 +++++++------ .../post/[postid]/components/post/index.tsx | 58 ++++++++++--------- src/utils/utils.ts | 21 +++++++ 3 files changed, 76 insertions(+), 44 deletions(-) create mode 100644 src/utils/utils.ts diff --git a/src/app/(site)/app/post/[postid]/components/post/components/options/index.tsx b/src/app/(site)/app/post/[postid]/components/post/components/options/index.tsx index 3d5b05a..c5eb14c 100644 --- a/src/app/(site)/app/post/[postid]/components/post/components/options/index.tsx +++ b/src/app/(site)/app/post/[postid]/components/post/components/options/index.tsx @@ -1,16 +1,20 @@ -"use client"; +'use client'; -import { useSupabase } from "@/hooks/use-supabase"; -import { MoreHorizontal } from "lucide-react"; -import { useRouter } from "next/navigation"; -import { useState } from "react"; -import { Option } from "./option.models"; -import { PrimaryButton } from "@/components/ui/buttons/primary"; +import { useSupabase } from '@/hooks/use-supabase'; +import { MoreHorizontal } from 'lucide-react'; +import { useRouter } from 'next/navigation'; +import { useState } from 'react'; +import { Option } from './option.models'; +import { PrimaryButton } from '@/components/ui/buttons/primary'; +import { downloadImage } from '@/utils/utils'; export function PostOptions({ post_id, + image_url, + doesUserOwnPost, }: { post_id: number; + image_url: string; doesUserOwnPost: boolean; }) { const { supabase } = useSupabase(); @@ -23,21 +27,21 @@ export function PostOptions({ >(null); const OWNER_OPTIONS: Option[] = [ - { title: "Edit", action: () => {} }, + { title: 'Edit', action: () => {} }, { - title: "Delete", + title: 'Delete', action: async () => { const indexOfOption = 1; setIndexOfOptionLoading(indexOfOption); try { const { error } = await supabase - .from("posts") + .from('posts') .delete() - .eq("id", post_id) + .eq('id', post_id) .single(); console.log({ error }); setIndexOfOptionLoading(null); - if (error) throw new Error("Error trying to delete"); + if (error) throw new Error('Error trying to delete'); router.refresh(); } catch (e) { // @@ -46,7 +50,10 @@ export function PostOptions({ }, ]; - const PUBLIC_OPTIONS: Option[] = [{ title: "Share", action: () => {} }]; + const PUBLIC_OPTIONS: Option[] = [ + { title: 'Share', action: () => {} }, + { title: 'Download', action: async () => await downloadImage(image_url) }, + ]; const FINAL_OPTIONS = [...OWNER_OPTIONS, ...PUBLIC_OPTIONS]; @@ -55,18 +62,18 @@ export function PostOptions({ } return ( -
+
{showDropdown && (