DPigeon / Edge

A web-base application kind of like a social media site to allow parents to communicate with teachers and vice-versa.
0 stars 1 forks source link

Bug: Array members of a group is empty on first click #100

Closed DPigeon closed 6 years ago

DPigeon commented 6 years ago

Once I want to join a new group and click on the JOIN button on first click, the members array of that group is empty when it goes to check if a members already exists. Everything works after first click, but not on first click...

memberAlreadyExistsInGroup(email) {
    for (var i = 0; i < this.state.members.length; i++) {
      if (email === this.state.members[i].user_id) return true; //does not exist
    }
    return false; //exists
  }

  joinGroup = (id, email, name) => {
    //make it so if user already exists in the group, alert(You cannot join cause you are already inside this group)
    fetch(`http://localhost:8000/groups/${id}/members`) //gets all the members inside the group
      .then(res => res.json())
      .then(json => {
        this.setState({
          members: json
        });
      });
    console.log(this.state.members); //BUG: the array is empty on first click of join button
    if (this.memberAlreadyExistsInGroup(email) === false) {
      //sees if member already exists
      fetch(`http://localhost:8000/groups/${id}/members`, {
        method: "POST",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json"
        },
        body: JSON.stringify({
          user_id: email,
          admin: false
        })
      });
      //give access to the group afterwards
      //this.sendGroupRequest(email, id); //send a group request to the database
      alert("You have joined " + name + "'s group !");
      //window.location.replace("/group/" + id);
    } else {
      //if the email is the same as one of the emails in the group members list
      alert("You are already a member of this group !");
    }
  };
DPigeon commented 6 years ago

This has been fixed by using the response of the JSON file.