From 1b947ee91511e37e3933ab1ab923e2e2cc8475cb Mon Sep 17 00:00:00 2001 From: Swarup Bhise Date: Mon, 13 May 2024 14:47:44 +0530 Subject: [PATCH] feat: create a Share Post Feature --- package-lock.json | 39 ++++++++ package.json | 1 + public/assets/icons/cancel.svg | 7 ++ src/components/shared/PostStats.tsx | 50 +++++++---- src/components/ui/Share.tsx | 134 ++++++++++++++++++++++++++++ 5 files changed, 213 insertions(+), 18 deletions(-) create mode 100644 public/assets/icons/cancel.svg create mode 100644 src/components/ui/Share.tsx diff --git a/package-lock.json b/package-lock.json index c6c6b37..db046e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ "react-hook-form": "^7.51.3", "react-intersection-observer": "^9.10.1", "react-router-dom": "^6.23.0", + "react-share": "^5.1.0", "tailwind-merge": "^2.3.0", "zod": "^3.23.5" }, @@ -2644,6 +2645,11 @@ "node": ">=6" } }, + "node_modules/classnames": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/classnames/-/classnames-2.5.1.tgz", + "integrity": "sha512-saHYOzhIQs6wy2sVxTM6bUDsQO4F50V9RQ22qBpEdCW+I+/Wmke2HOl6lS6dTpdxVhb88/I6+Hs+438c3lfUow==" + }, "node_modules/clsx": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/clsx/-/clsx-2.1.1.tgz", @@ -3527,6 +3533,27 @@ "node": ">=6" } }, + "node_modules/jsonp": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/jsonp/-/jsonp-0.2.1.tgz", + "integrity": "sha512-pfog5gdDxPdV4eP7Kg87M8/bHgshlZ5pybl+yKxAnCZ5O7lCIn7Ixydj03wOlnDQesky2BPyA91SQ+5Y/mNwzw==", + "dependencies": { + "debug": "^2.1.3" + } + }, + "node_modules/jsonp/node_modules/debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dependencies": { + "ms": "2.0.0" + } + }, + "node_modules/jsonp/node_modules/ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" + }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -4231,6 +4258,18 @@ "react-dom": ">=16.8" } }, + "node_modules/react-share": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/react-share/-/react-share-5.1.0.tgz", + "integrity": "sha512-OvyfMtj/0UzH1wi90OdHhZVJ6WUC/+IeWvBwppeZozwIGyAjQgyR0QXlHOrxVHVECqnGvcpBaFTXVrqouTieaw==", + "dependencies": { + "classnames": "^2.3.2", + "jsonp": "^0.2.1" + }, + "peerDependencies": { + "react": "^17 || ^18" + } + }, "node_modules/react-style-singleton": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/react-style-singleton/-/react-style-singleton-2.2.1.tgz", diff --git a/package.json b/package.json index 3274621..21666a2 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "react-hook-form": "^7.51.3", "react-intersection-observer": "^9.10.1", "react-router-dom": "^6.23.0", + "react-share": "^5.1.0", "tailwind-merge": "^2.3.0", "zod": "^3.23.5" }, diff --git a/public/assets/icons/cancel.svg b/public/assets/icons/cancel.svg new file mode 100644 index 0000000..e1f4aba --- /dev/null +++ b/public/assets/icons/cancel.svg @@ -0,0 +1,7 @@ + + + + + + cancel1 + \ No newline at end of file diff --git a/src/components/shared/PostStats.tsx b/src/components/shared/PostStats.tsx index 662ec58..e1fe277 100644 --- a/src/components/shared/PostStats.tsx +++ b/src/components/shared/PostStats.tsx @@ -9,6 +9,7 @@ import { useLikePost, useSavePost, } from "@/lib/react-query/queriesAndMutations"; +import Share from "../ui/Share"; type PostStatsProps = { post?: Models.Document; @@ -31,6 +32,7 @@ const PostStats = ({ post, userId }: PostStatsProps) => { const savedPostRecord = currentUser?.save.find( (record: Models.Document) => record.post.$id === post?.$id ); + const [shareurl, setShareUrl] = useState(""); useEffect(() => { setIsSaved(!!savedPostRecord); @@ -83,32 +85,44 @@ const PostStats = ({ post, userId }: PostStatsProps) => { } }; + const handleShare = () => { + const hostUrl = window.location.origin; + + setShareUrl(hostUrl + "/posts/" + post?.$id); + }; + const containerStyles = location.pathname.startsWith("/profile") ? "w-full" : ""; return (
-
- like handleLikePost(e)} - onKeyDown={(e) => handleLikePost(e)} - className="cursor-pointer" - tabIndex={0} - /> -

{likes.length}

+
+
+ like handleLikePost(e)} + onKeyDown={(e) => handleLikePost(e)} + className="cursor-pointer" + tabIndex={0} + /> +

{likes.length}

+
+
+ + + {/*

0

*/} +
-
void; +}; + +const Share = ({ shareurl, handleShare }: ShareProps) => { + const data = [ + { + label: "Facebook", + icon: ( + + + + ), + }, + + { + label: "Whatsapp", + icon: ( + + + + ), + }, + { + label: "Twitter", + icon: ( + + + + ), + }, + { + label: "Telegram", + icon: ( + + + + ), + }, + { + label: "Email", + icon: ( + + + + ), + }, + ]; + + return ( + + + share + + + + Share Link + +
+

Share this Post via

+
+ {data.map((ele) => { + return ( +
+ {ele.icon} + {ele.label} +
+ ); + })} +
+
{" "} +
+

Or copy link

+

+ Anyone who has this link will be able to view this +

+
+ + +
+
+
+
+ ); +}; + +export default Share;