import React from 'react'; import { withRouter } from "react-router-dom"; import { HomeRounded as HomeRoundedIcon, PlayArrowRounded as PlayArrowRoundedIcon, EditRounded as EditRoundedIcon, CloudQueueRounded as CloudQueueRoundedIcon, GroupAddRounded as GroupAddRoundedIcon, CloseRounded as CloseRoundedIcon, DeleteRounded as DeleteRoundedIcon } from "@material-ui/icons"; import NavBar from "./NavBar"; import Button from "./Button"; import LinkButton from "./LinkButton"; import Error404 from "./Error404"; import Footer from "./Footer"; import "./css/PopUp.css"; import "./css/SetPage.css"; import "./css/ConfirmationDialog.css"; export default withRouter(class SetPage extends React.Component { constructor(props) { super(props); this.state = { user: props.user, db: props.db, functions: { createProgress: props.functions.httpsCallable("createProgress"), addSetToGroup: props.functions.httpsCallable("addSetToGroup"), }, loading: false, canStartTest: true, navbarItems: [ { type: "link", link: "/", icon: , hideTextMobile: true, } ], set: { title: "", public: false, vocab: [], }, setInaccessible: false, showAddSetToGroup: false, canAddSetToGroup: true, addSetToGroupLoading: {}, groups: {}, currentSetGroups: [], showDeleteConfirmation: false, }; let isMounted = true; Object.defineProperty(this, "isMounted", { get: () => isMounted, set: (value) => isMounted = value, }); } setState = (state, callback = null) => { if (this.isMounted) super.setState(state, callback); } componentDidMount() { const setId = this.props.match.params.setId; const setRef = this.state.db .collection("sets") .doc(setId); const setVocabRef = setRef .collection("vocab") .orderBy("term"); setRef.get().then((setDoc) => { document.title = `${setDoc.data().title} | Parandum`; setVocabRef.get().then((querySnapshot) => { let vocab = []; querySnapshot.docs.map((doc) => { const data = doc.data(); return vocab.push({ term: data.term, definition: data.definition, sound: data.sound, }); }); this.setState({ set: { ...this.state.set, title: setDoc.data().title, public: setDoc.data().public, vocab: vocab, owner: setDoc.data().owner, }, currentSetGroups: setDoc.data().groups, }); }); }).catch((error) => { this.setState({ setInaccessible: true, }); console.log(`Can't access set: ${error}`); }); } componentWillUnmount() { this.isMounted = false; } stopLoading = () => { this.setState({ canStartTest: true, loading: false, }); } startTest = () => { if (this.state.canStartTest) { this.state.functions.createProgress({ sets: [this.props.match.params.setId], switch_language: false, mode: "questions", limit: 10, }).then((result) => { const progressId = result.data; this.stopLoading(); this.props.history.push("/progress/" + progressId); }).catch((error) => { console.log(`Couldn't start test: ${error}`); this.stopLoading(); }); this.setState({ canStartTest: false, loading: true, }); } }; showAddSetToGroup = async () => { let newState = { showAddSetToGroup: true, groups: {}, addSetToGroupLoading: {}, }; await this.state.db.collection("users") .doc(this.state.user.uid) .collection("groups") .where("role", "!=", "member") .get() .then((querySnapshot) => { return Promise.all(querySnapshot.docs.map((userGroupDoc) => { if (!this.state.currentSetGroups.includes(userGroupDoc.id)) return this.state.db.collection("groups") .doc(userGroupDoc.id) .get() .then((groupDoc) => { newState.groups[userGroupDoc.id] = groupDoc.data().display_name; newState.addSetToGroupLoading[userGroupDoc.id] = false; }); return true; })); }) this.setState(newState); } hideAddSetToGroup = () => { this.setState({ showAddSetToGroup: false, }); } stopAddSetToGroupLoading = (groupId, addedSetToGroup = false) => { let newState = { addSetToGroupLoading: { ...this.state.addSetToGroupLoading, [groupId]: false, }, canAddSetToGroup: true, showAddSetToGroup: false, }; if (addedSetToGroup) newState.currentSetGroups = this.state.currentSetGroups.concat(groupId); this.setState(newState); } addSetToGroup = (groupId) => { if (this.state.canAddSetToGroup) { this.setState({ addSetToGroupLoading: { ...this.state.addSetToGroupLoading, [groupId]: true, }, canAddSetToGroup: false, }); this.state.functions.addSetToGroup({ groupId: groupId, setId: this.props.match.params.setId, }).then((result) => { this.stopAddSetToGroupLoading(groupId, true); }).catch((error) => { console.log(`Couldn't add set to group: ${error}`); }); } } showDeleteSet = () => { this.setState({ showDeleteConfirmation: true, }); } hideDeleteConfirmation = () => { this.setState({ showDeleteConfirmation: false, }); } deleteSet = () => { this.state.db.collection("sets") .doc(this.props.match.params.setId) .delete() .then(() => { this.props.history.push("/"); }).catch((error) => { console.log(`Couldn't delete set: ${error}`); this.setState({ showDeleteConfirmation: false, }) }); } render() { return (
{ this.state.setInaccessible ? : <>

{this.state.set.title} { this.state.set.public ? : "" }

{ this.state.set.owner === this.state.user.uid && } className="button--round" > } { this.state.set.owner === this.state.user.uid && this.state.currentSetGroups.length === 0 && }

Terms

Definitions

{this.state.set.vocab.map((contents, index) =>
{contents.term} {contents.definition}
)}
) } })