From 1e3f4d6041f231a253390c8e97eccccc5ae1a4cf Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Tue, 18 Jan 2022 18:17:50 -0800 Subject: [PATCH] creating link successfully --- components/LinkCard.tsx | 7 ++++-- components/Modals/CreateOrUpdateLink.tsx | 31 ++++++++++++++++++++++-- models/link.ts | 18 +++++--------- services/collections.tsx | 2 +- services/sendService.tsx | 8 ++++++ 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/components/LinkCard.tsx b/components/LinkCard.tsx index e5de282..082b84a 100644 --- a/components/LinkCard.tsx +++ b/components/LinkCard.tsx @@ -1,5 +1,5 @@ import { FaArchive, FaExternalLinkAlt, FaFilePdf } from "react-icons/fa"; -import Link from "../models/link"; +import Link, { LinkType } from "../models/link"; import Image from "next/image"; import { BsThreeDots } from "react-icons/bs"; import LinkIcon from "./LinkIcon"; @@ -9,7 +9,10 @@ export default function LinkCard(props: { link?: Link }) { {/* attmnt header */} - + report.pdf diff --git a/components/Modals/CreateOrUpdateLink.tsx b/components/Modals/CreateOrUpdateLink.tsx index b025071..9f956be 100644 --- a/components/Modals/CreateOrUpdateLink.tsx +++ b/components/Modals/CreateOrUpdateLink.tsx @@ -11,8 +11,11 @@ import { } from "../../contexts/keyboardContext"; import { useTeamDashboardContext } from "../../contexts/teamDashboardContext"; import Link, { LinkType } from "../../models/link"; +import { SendService } from "../../services/sendService"; import LinkIcon from "../LinkIcon"; +const sendService = new SendService(); + export default function CreateOrUpdateLink() { const { currUser } = useAuth(); const { handleModalType, showModalType } = useKeyboardContext(); @@ -41,12 +44,22 @@ export default function CreateOrUpdateLink() { } } - function handleSubmitLink() { + async function handleSubmitLink() { if (!link || !name) { toast.error("Please add a valid link and name"); return; } + if ( + !isTeamLink && + (!recipientsSelected || recipientsSelected.length == 0) + ) { + toast.error( + "Please select members or send it to the team with the toggle" + ); + return; + } + // if it's a team link, make sure recipients is null ... but the model should handle this for us? try { @@ -59,15 +72,29 @@ export default function CreateOrUpdateLink() { currUser.uid ); - console.log(newLink); + handleModalType(ShowModalType.na); // send to database + await sendService.sendLink(newLink); + + resetForm(); + + toast.success("link sent"); } catch (error) { toast.error("Something went wrong in sending the link."); console.log(error); } } + function resetForm() { + setLink(""); + setName(""); + setDescription(""); + setShowMoreDetails(false); + setIsTeamLink(true); + setRecipientsSelected([]); + } + const [link, setLink] = useState(""); const [name, setName] = useState(""); const [description, setDescription] = useState(""); diff --git a/models/link.ts b/models/link.ts index 5ba897c..c84ec99 100644 --- a/models/link.ts +++ b/models/link.ts @@ -11,7 +11,7 @@ export default class Link { teamId: string; // if it's not a teamAttachment, then have a list of members who it's for - private _recipients: string[]; // userIds + recipients: string[]; // userIds createdByUserId: string; createdDate: Timestamp; @@ -31,19 +31,13 @@ export default class Link { this.recipients = recipientsArr; this.createdByUserId = _createdByUserId; - this.type = Link.getLinkType(_link); - } - - public get recipients(): string[] { - return this._recipients; - } - - public set recipients(arrUserIds: string[]) { - if (!this._recipients || this._recipients?.length == 0) { - this._recipients = null; + if (!recipientsArr || recipientsArr?.length == 0) { + this.recipients = null; } else { - this._recipients = arrUserIds; + this.recipients = recipientsArr; } + + this.type = Link.getLinkType(_link); } static getLinkType(url: string): LinkType { diff --git a/services/collections.tsx b/services/collections.tsx index 8c7a2a1..b224ee6 100644 --- a/services/collections.tsx +++ b/services/collections.tsx @@ -7,5 +7,5 @@ export enum Collections { rooms = "rooms", announcements = "announcements", - attachments = "attachments", + links = "links", } diff --git a/services/sendService.tsx b/services/sendService.tsx index 6fc47bd..8f95aca 100644 --- a/services/sendService.tsx +++ b/services/sendService.tsx @@ -5,6 +5,7 @@ import { getFirestore, serverTimestamp, } from "firebase/firestore"; +import Link from "../models/link"; import { Message } from "../models/message"; import { Collections } from "./collections"; @@ -23,4 +24,11 @@ export class SendService { } ); } + + async sendLink(link: Link) { + await addDoc(collection(this.db, Collections.links), { + ...link, + createdDate: serverTimestamp(), + }); + } }