import React, { Component } from 'react';
import { HomeRounded as HomeRoundedIcon, GroupAddRounded as GroupAddRoundedIcon, GroupRounded as GroupRoundedIcon, CloseRounded as CloseRoundedIcon, ArrowForwardRounded as ArrowForwardRoundedIcon, PersonRounded as PersonRoundedIcon, EditRounded as EditRoundedIcon, VisibilityRounded as VisibilityRoundedIcon } from "@material-ui/icons";
import NavBar from "./NavBar";
import Button from "./Button";
import Footer from "./Footer";
import { withRouter, Link } from "react-router-dom";
import "./css/PopUp.css";
import "./css/UserGroups.css";
export default withRouter(class UserGroups extends Component {
constructor(props) {
super(props);
this.state = {
user: props.user,
db: props.db,
functions: {
createGroup: props.functions.httpsCallable("createGroup"),
},
navbarItems: [
{
type: "link",
link: "/",
icon: ,
hideTextMobile: true,
}
],
createGroup: false,
joinGroup: false,
groupName: "",
joinCode: "",
canCreateGroup: false,
canJoinGroup: false,
canFindGroup: true,
userGroups: null,
};
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() {
document.title = "Groups | Parandum";
const userGroupsRef = this.state.db.collection("users")
.doc(this.state.user.uid)
.collection("groups")
.orderBy("role");
userGroupsRef.get().then(async (querySnapshot) => {
let newState = {
userGroups: {},
};
await Promise.all(querySnapshot.docs.map((userGroupDoc) => {
return this.state.db
.collection("groups")
.doc(userGroupDoc.id)
.get()
.then((groupDoc) => {
if (userGroupDoc.data() && groupDoc.data()) newState.userGroups[userGroupDoc.id] = {
role: userGroupDoc.data().role,
displayName: groupDoc.data().display_name,
setCount: groupDoc.data().sets.length,
memberCount: Object.keys(groupDoc.data().users).length,
};
});
}));
this.setState(newState);
this.props.page.load();
});
this.props.logEvent("page_view");
}
componentWillUnmount() {
this.isMounted = false;
this.props.page.unload();
}
showJoinGroup = () => {
this.setState({
joinGroup: true,
}, () => this.joinCodeInput.focus());
}
hideJoinGroup = () => {
this.setState({
joinGroup: false,
});
}
showCreateGroup = () => {
this.setState({
createGroup: true,
}, () => this.groupNameInput.focus());
}
hideCreateGroup = () => {
this.setState({
createGroup: false,
});
}
createGroup = () => {
if (this.state.canCreateGroup) {
this.startCreateGroupLoading();
this.state.functions.createGroup(this.state.groupName)
.then((result) => {
this.props.history.push(`/groups/${result.data}`);
this.stopCreateGroupLoading();
this.props.logEvent("create_group", {
group_id: result.data,
});
}).catch((error) => {
console.log(`Couldn't create group: ${error}`);
this.stopCreateGroupLoading();
});
}
}
joinGroup = () => {
if (this.state.canJoinGroup) {
this.startJoinGroupLoading();
this.state.db.collection("join_codes")
.doc(this.state.joinCode)
.get()
.then((joinCodeDoc) => {
this.state.db.collection("users")
.doc(this.state.user.uid)
.collection("groups")
.doc(joinCodeDoc.data().group)
.set({
role: "member",
}).then(() => {
this.props.history.push(`/groups/${joinCodeDoc.data().group}`);
this.stopJoinGroupLoading();
});
}).catch((error) => {
this.stopJoinGroupLoading(false);
});
}
}
handleGroupNameChange = (event) => {
this.setState({
groupName: event.target.value,
canCreateGroup: event.target.value.replace(" ", "") !== "",
});
}
handleJoinCodeChange = (event) => {
this.setState({
joinCode: event.target.value,
canJoinGroup: event.target.value.replace(" ", "") !== "",
canFindGroup: true,
});
}
startJoinGroupLoading = () => {
this.setState({
loading: true,
canJoinGroup: false,
});
}
stopJoinGroupLoading = (canFindGroup = true) => {
let newState = {
loading: false,
canJoinGroup: true,
};
if (!canFindGroup) newState.canFindGroup = false;
this.setState(newState);
}
startCreateGroupLoading = () => {
this.setState({
loading: true,
canCreateGroup: false,
});
}
stopCreateGroupLoading = () => {
this.setState({
loading: false,
canCreateGroup: true,
});
}
handleGroupNameInputKeypress = (event) => {
if (event.key === "Enter") {
this.createGroup();
}
}
handleJoinCodeInputKeypress = (event) => {
if (event.key === "Enter") {
this.joinGroup();
}
}
render() {
return (
Groups
}
>
Join
}
>
Create
{
this.state.userGroups && Object.keys(this.state.userGroups).length > 0
?
<>
{Object.keys(this.state.userGroups)
.sort((a, b) => {
if (this.state.userGroups[a].displayName < this.state.userGroups[b].displayName) {
return -1;
}
if (this.state.userGroups[a].displayName > this.state.userGroups[b].displayName) {
return 1;
}
return 0;
})
.map((groupId, index) =>
{this.state.userGroups[groupId].displayName}
{
this.state.userGroups[groupId].role === "owner"
?
:
this.state.userGroups[groupId].role === "contributor"
?
:
}
)}
>
:
You aren't a member of any groups
}
{
this.state.createGroup
?
<>
>
:
this.state.joinGroup &&
<>
Join Group
(this.joinCodeInput = inputEl)}
autoComplete="off"
/>
}
className="button--round"
loading={this.state.loading}
disabled={!this.state.canJoinGroup}
>
{
!this.state.canFindGroup &&
Can't find that group!
}
}
className="button--no-background popup-close-button"
>
>
}
)
}
})