From 266a2287d9464be63723011a6d08bc1203a3b577 Mon Sep 17 00:00:00 2001 From: Kevin Compton Date: Fri, 4 Aug 2023 10:54:48 -0700 Subject: [PATCH] updates view --- pages/updates/index.tsx | 105 ++++++++++++++++++++++++++++++++-------- types/mongo.ts | 5 ++ 2 files changed, 90 insertions(+), 20 deletions(-) diff --git a/pages/updates/index.tsx b/pages/updates/index.tsx index 51284e1..8f13305 100644 --- a/pages/updates/index.tsx +++ b/pages/updates/index.tsx @@ -1,7 +1,8 @@ "use client"; import React, { useState, useEffect, ReactNode } from "react"; -import { Project } from "@/types/mongo"; +import { Project, ProjectUpdate } from "@/types/mongo"; import { AdminNav } from "@/components/AdminNav"; +import { useForm, SubmitHandler } from "react-hook-form"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"; import { Textarea } from "@/components/ui/textarea"; import { @@ -13,29 +14,82 @@ import { } from "@/components/ui/select"; import { Button } from "@/components/ui/button"; import { useGetCurrentUser } from "@/hooks/useGetCurrentUser"; +import { useContext } from "react"; +import { AuthContext } from "@/components/AuthProvider"; export default function Updates() { const { data } = useGetCurrentUser() const [projects, setProjects] = useState([]); const [project, setProject] = useState(); + const [updates, setUpdates] = useState([]); + const { idToken } = useContext(AuthContext) + const { + register, + handleSubmit, + formState: { errors }, + } = useForm({ + mode: "onBlur", + defaultValues: { + project: '', + text: '' + } + }); useEffect(() => { - getProjects(data._id) + if(data) + getProjects(data._id) }, [data]); - ///projects/:projectId/updates + const onSubmit: SubmitHandler = (formData) => { + try { + sendUpdate(formData) + } catch (error) { + console.log(error) + } + }; + + async function sendUpdate(values: ProjectUpdate) { + const res = await fetch(`${process.env.BACKEND_URL}/projects/${values.project}/updates`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${idToken}`, + }, + body: JSON.stringify(values), + }); + return await res.json(); + } function selectProject(projectId:string) { const currentProject = projects.filter((p) => p._id === projectId) setProject(currentProject[0]) + if(project) + getUpdates(project?._id) + } + + async function getUpdates(projectId:string) { + const res = await fetch(`${process.env.BACKEND_URL}/projects/${projectId}/updates`, { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${idToken}`, + } + }) + .then(res => res.json()) + .then( + (result) => { + setUpdates(result) + } + ); + return res; } - async function getProjects(id:number) { + async function getProjects(id:string) { const res = await fetch(`${process.env.BACKEND_URL}/users/${id}/projects`, { method: "GET", headers: { "Content-Type": "application/json", - Authorization: `Bearer ${id}`, + Authorization: `Bearer ${idToken}`, } }) .then(res => res.json()) @@ -54,19 +108,20 @@ export default function Updates() {

Share an update with your supporters

Update your supporters with the latest intel on your project and progress. Let them know what you're looking for and how they can support you. Help them help you spread your vision!

- - -