diff --git a/components/SingleClassView/Communicate/Communicate.tsx b/components/SingleClassView/Communicate/Communicate.tsx new file mode 100644 index 00000000..ca85ad1d --- /dev/null +++ b/components/SingleClassView/Communicate/Communicate.tsx @@ -0,0 +1,126 @@ +import React, { useContext, useEffect, useState } from "react"; +import { APIContext } from "../../../context/APIContext"; +import { AuthContext } from "../../../context/AuthContext"; +import Button from "@mui/material/Button"; +import styles from "./communicate.module.css"; +import { Announcement } from "../../../models"; +import { CustomError } from "../../../components/util/CustomError"; +import { CommunicateItem } from "./CommunicateItem"; + +type CommunicateProps = { + id: string; +}; + +export const Communicate: React.FC = ({ id }) => { + const api = useContext(APIContext); + const { user } = useContext(AuthContext); + const [popup, setPopup] = useState(false); + const [announcements, setAnnouncements] = useState([]); + const [title, setTitle] = useState(""); + const [content, setContent] = useState(""); + const [refresh, setRefresh] = useState(false); + const [error, setError] = useState(false); + + useEffect(() => { + (async () => { + const res = await api.getAnnouncements(id); + setAnnouncements(res); + })(); + }, [refresh]); + + const handleClick: VoidFunction = () => { + setPopup(!popup); + }; + + const handleCancel = async (): Promise => { + setError(false); + setPopup(false); + }; + + const handleSubmit = async (): Promise => { + if (title == "" || content == "") { + setError(true); + } else { + const announcement = { + title: title, + content: content, + }; + await api.createAnnouncement(id, announcement); + triggerClassModuleRefresh(); + setError(false); + setPopup(false); + setTitle(""); + setContent(""); + } + }; + + const triggerClassModuleRefresh = (): void => { + setRefresh(!refresh); + }; + + if (user == null) return ; + + return ( +
+ {popup ? ( +
+
+
New Post
+ setTitle(e.target.value)} + type="text" + placeholder="Summary" + /> +