Skip to content

Commit

Permalink
Updated way to show smartSharelinks
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanishkrawatt committed May 17, 2024
1 parent 5ec1158 commit e9a47f3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 58 deletions.
3 changes: 0 additions & 3 deletions pages/api/deleteItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function deleteDocFromDB(path: string) {
querySnapshot.forEach((docs) => {
const docRef = doc(db, `User/${userId}/Images/`, docs.id);
deleteDoc(docRef);
console.log("Document deleted", docs.id);
});
});
return null;
Expand All @@ -54,12 +53,10 @@ export default function handler(
if (req.method === "POST") {

const { data, deletefromDB } = req.body;
console.log(data,deletefromDB);
data.map((item) => {
const path = getPathStorageFromUrl(item);
const storageRef = ref(storage, path);
deleteObject(storageRef);
console.log("image deleted")
if (deletefromDB) {
deleteDocFromDB(path);
}
Expand Down
35 changes: 35 additions & 0 deletions pages/api/getSmartShareLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { DocumentData, collection, getDocs } from "firebase/firestore";
import db from "../../firebase/firestore";

type Data = {
status: string;
data?: SmartShareData[];
};
type SmartShareData = {
url: string;
time: number;
name: string;
};
type Error = {
error: string;
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data | Error | Data[] | DocumentData[]>
) {
if (req.method === "POST") {
const { uid } = req.body;
const collectionRef = collection(db, `User/${uid}/Smartshare`);
const SmartShareData: SmartShareData[] = [];
await getDocs(collectionRef).then((querySnapshot) => {
querySnapshot.forEach((doc) => {
const { smartLink, time, name } = doc.data();
SmartShareData.push({ url: smartLink, time, name });
});
res.status(200).json({ status: "Done", data: SmartShareData });
});
} else {
res.status(200).json({ status: "Not Done" });
}
}
73 changes: 18 additions & 55 deletions pages/smartshare.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function Smartshare() {
? parseInt(dropDown.split(" ")[0])
: parseInt(dropDown.split(" ")[0]) * 7,
date: new Date().toDateString(),
smartLink: `https://cloudbox.kanishkrawatt.tech/smartshow?id=${id}-${user?.uid}`,
});
await addDoc(collection(db, `User/${user?.uid}/Smartshare/${randomId}/files`), {
name: files[0]?.name,
Expand All @@ -58,37 +59,6 @@ function Smartshare() {
// console.error(error.message);
}
};
const addSmartLinkToLocalStorage = useCallback((url: string, name: string, time: string) => {
const id = randomId;
const urls = localStorage.getItem("urls");
if (urls) {
const urlsArr = JSON.parse(urls);
urlsArr.push({
name: name,
date: new Date().toDateString(),
time:
dropDown.split(" ")[1] !== "week"
? parseInt(dropDown.split(" ")[0])
: parseInt(dropDown.split(" ")[0]) * 7,
url: `${window.location.host}/smartshow?id=${id}-${user?.uid}`,
});
localStorage.setItem("urls", JSON.stringify(urlsArr));
} else {
localStorage.setItem(
"urls",
JSON.stringify([
{
name: name,
time:
dropDown.split(" ")[1] !== "week"
? parseInt(dropDown.split(" ")[0])
: parseInt(dropDown.split(" ")[0]) * 7,
url: `${window.location.host}/smartshow?id=${id}-${user?.uid}`,
},
])
);
}
}, [dropDown, user?.uid]);

const uploadFileToStorage = async (SmartName: string, name: string, file: File) => {
const storageRef = ref(
Expand Down Expand Up @@ -140,11 +110,6 @@ function Smartshare() {
fileUrls.forEach(({ url }) => URL.revokeObjectURL(url));
};
}, [files]);
useEffect(() => {
if (smartId) {
addSmartLinkToLocalStorage(smartId, name.current!.value, dropDown);
}
}, [addSmartLinkToLocalStorage, dropDown, smartId]);
return (
<Layout>
<div className="flex flex-wrap p-2 justify-start gap-[2rem]"
Expand Down Expand Up @@ -495,30 +460,28 @@ export const UploadImage = ({
};

export const SmartShareLink = ({ status }: { status?: number | null }) => {
const { user } = useAuth();
const [urls, setUrl] = useState<
{ name: string; time: string; url: string; date: string }[]
{ name: string; time: string; url: string}[]
>([]);
const getSmartShareLinks = useCallback(async () => {
const data = await fetch("/api/getSmartShareLinks", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ uid: user?.uid }),
})
data.json().then((res) => {
setUrl(res.data);
});
}, [user?.uid]);

const [copyState, setCopyState] = useState(false);

useEffect(() => {
const urls = localStorage.getItem("urls");
if (urls) {
const urlsArr = JSON.parse(urls);
const newUrls = urlsArr.filter((url: any) => {
const date = new Date(url.date);
const time = url.time;
const newDate = new Date(
date.getFullYear(),
date.getMonth(),
date.getDate() + time
);
const today = new Date();
return newDate > today;
});
localStorage.setItem("urls", JSON.stringify(newUrls));
setUrl(JSON.parse(urls));
}
}, []);
getSmartShareLinks();
}, [getSmartShareLinks]);

const copyURL = (id: number) => {
navigator.clipboard.writeText(urls[id].url);
Expand Down

0 comments on commit e9a47f3

Please sign in to comment.