Closed DPigeon closed 5 years ago
Can you post the JSON you are sending/receiving and any other information ?
It worked fine on my end. If you look at the code, as long as the requestID is valid, the method "Add member to group" is called only when accept is set to true (accept field from the request body/json you send)
Remember that if you add the same member twice, it will not block (I haven't protected duplication yet because I didnt have the time and it's not needed if the front-end does everything perfectly, and I cant make the field unique in the database because user/groups relation can possess the same user_id/email, same for the requests
Also, when rejecting a request, please verify that the user isn't already in the group. Because of course it won't delete the member from the group and won't add member so nothing will change. (But the request will be deleted as normal because it has been processed)
acceptRejectRequest = (requestId, userId, response) => {
if (response === true) {
//accepted member, congratulations {name}!
fetch(`http://localhost:8000/groupRequests/${requestId}`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
accept: response
})
});
} else if (response === false) {
//rejected member, sorry {name}!
fetch(`http://localhost:8000/groupRequests/${requestId}`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
accept: response
})
});
}
if (response == true) alert("You have accepted " + userId + "'s request !");
else alert("You have rejected " + userId + "'s request !");
window.location.reload();
};
This is my the code above. You can see the JSON request. I am looking for duplicates when joining a group also.
And what isn't working ?
Also you can remove the if/else and put the thing together as they're the same and the response variable is the boolean.
Can you verify the route + json you're sending and the response you get back from the server ?
I think because Javascript is a shitty language, the response variable might be transformed to a string because it isn't typed, and a non empty string is always true.
You said it worked before didn't it ?
When I reject a member, the request gets deleted, but the user is still getting added to the group.
Ok I think I fixed it. There was a wrong thing in my code.
What was it ? Please update me
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
});
this.beforeRequest(this.state.members, email, id, name); //We did not have access to the members (fixed David)
});
};
//console.log(newMembersArray); //BUG: the array is empty on first click of join button
beforeRequest(members, email, id, name) {
if (this.memberAlreadyExistsInGroup(members, email) === false) {
//sees if member already exists
this.sendGroupRequest(email, id); //send a group request to the database
alert("You have sent a request to join " + name + "'s group !");
} 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 !");
}
}
The problem was that I was doing a POST method right when I was clicking on the join button. The logic was wrong. I'm gonna close this since I tested it and it works. Thanks for the help