Skip to content

Commit be4ab82

Browse files
authored
Merge pull request #53 from 5-Bits-in-a-Byte/user-role-fix - SUCCESS
user-role-fix merge with main
2 parents 38f5328 + 82eaa2e commit be4ab82

File tree

11 files changed

+276
-166
lines changed

11 files changed

+276
-166
lines changed

client/src/App.js

+30-26
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,45 @@ function App() {
1717
return (
1818
<Router>
1919
<UserProvider>
20-
<UserRoleProvider>
21-
<Switch>
22-
<PrivateRoute path="/home" exact>
23-
<NavigationWrapper>
24-
<Home />
25-
</NavigationWrapper>
26-
</PrivateRoute>
27-
<PrivateRoute path="/" exact>
28-
<NavigationWrapper>
29-
<Courses />
30-
</NavigationWrapper>
31-
</PrivateRoute>
32-
<Route path="/signup" exact>
33-
<SignUp />
34-
</Route>
35-
<Route path="/login" exact>
36-
<Login />
37-
</Route>
38-
<PrivateRoute path="/course/:courseId" exact>
20+
<Switch>
21+
<PrivateRoute path="/home" exact>
22+
<NavigationWrapper>
23+
<Home />
24+
</NavigationWrapper>
25+
</PrivateRoute>
26+
<PrivateRoute path="/" exact>
27+
<NavigationWrapper>
28+
<Courses />
29+
</NavigationWrapper>
30+
</PrivateRoute>
31+
<Route path="/signup" exact>
32+
<SignUp />
33+
</Route>
34+
<Route path="/login" exact>
35+
<Login />
36+
</Route>
37+
<PrivateRoute path="/course/:courseId" exact>
38+
<UserRoleProvider>
3939
<NavigationWrapper>
4040
<ClassView />
4141
</NavigationWrapper>
42-
</PrivateRoute>
43-
<PrivateRoute path="/course/:courseId/post/:postid" exact>
42+
</UserRoleProvider>
43+
</PrivateRoute>
44+
<PrivateRoute path="/course/:courseId/post/:postid" exact>
45+
<UserRoleProvider>
4446
<NavigationWrapper>
4547
<CommentView />
4648
</NavigationWrapper>
47-
</PrivateRoute>
48-
<PrivateRoute path="/course/:courseId/config">
49+
</UserRoleProvider>
50+
</PrivateRoute>
51+
<PrivateRoute path="/course/:courseId/config">
52+
<UserRoleProvider>
4953
<NavigationWrapper>
5054
<ConfigView />
5155
</NavigationWrapper>
52-
</PrivateRoute>
53-
</Switch>
54-
</UserRoleProvider>
56+
</UserRoleProvider>
57+
</PrivateRoute>
58+
</Switch>
5559
</UserProvider>
5660
</Router>
5761
);

client/src/components/comments/Comment.js

+34-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import Dropdown from "../common/dropdown/Dropdown";
1111
import Icon from "../common/Icon";
1212
import OptionDots from "../../imgs/option-dots.svg";
1313
import { UserContext } from "../context/UserProvider";
14+
import { UserRoleContext } from "../context/UserRoleProvider";
1415

15-
const Comment = ({ comment, isDraft, callback, userRole }) => {
16+
const Comment = ({ comment, isDraft, callback }) => {
1617
// console.log("Comment Role Object: ", userRole);
1718
const { courseId, postid } = useParams();
1819
const [content, setContent] = useState("");
1920
const user = useContext(UserContext);
21+
const userRole = useContext(UserRoleContext);
2022

2123
const [newReplies, setNewReplies] = useState([]);
2224
const [isReplying, toggleReply] = useState(false);
@@ -113,10 +115,37 @@ const Comment = ({ comment, isDraft, callback, userRole }) => {
113115
alert("This feature is still a work in progress. Check back soon!");
114116
};
115117

116-
const options = [
117-
{ onClick: handleDelete, label: "Delete comment" },
118-
{ onClick: handleEdit, label: "Edit comment" },
119-
];
118+
const generateDropdownOptions = () => {
119+
if (userRole) {
120+
let deleteOption =
121+
userRole.delete.postComment && comment.postedBy._id == user._id
122+
? {
123+
onClick: handleDelete,
124+
label: "Delete Comment",
125+
}
126+
: null;
127+
let editOption =
128+
userRole.edit.postComment && comment.postedBy._id == user._id
129+
? { onClick: handleEdit, label: "Edit Comment" }
130+
: null;
131+
132+
let result = [];
133+
134+
if (deleteOption) result.push(deleteOption);
135+
if (editOption) result.push(editOption);
136+
137+
if (result.length == 0) return null;
138+
139+
return result;
140+
}
141+
return null;
142+
};
143+
144+
var options = generateDropdownOptions();
145+
// = [
146+
// { onClick: handleDelete, label: "Delete comment" },
147+
// { onClick: handleEdit, label: "Edit comment" },
148+
// ];
120149

121150
// Initialize viewOptions to see if a user should be able to see dropdown options
122151
var viewOptions = false;

client/src/components/comments/CommentReply.js

+32-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const CommentReply = ({
5555
window.location.reload();
5656
},
5757
onFailure: (err) => {
58-
alert(err.response);
58+
alert("Error deleting Reply, Response: ", err.response);
5959
},
6060
});
6161
};
@@ -64,10 +64,37 @@ const CommentReply = ({
6464
alert("This feature is still a work in progress. Check back soon!");
6565
};
6666

67-
const options = [
68-
{ onClick: handleDelete, label: "Delete reply" },
69-
{ onClick: handleEdit, label: "Edit reply" },
70-
];
67+
const generateDropdownOptions = () => {
68+
if (userRole) {
69+
let deleteOption =
70+
userRole.delete.reply && reply.postedBy._id == user._id
71+
? {
72+
onClick: handleDelete,
73+
label: "Delete reply",
74+
}
75+
: null;
76+
let editOption =
77+
userRole.edit.reply && reply.postedBy._id == user._id
78+
? { onClick: handleEdit, label: "Edit Reply" }
79+
: null;
80+
81+
let result = [];
82+
83+
if (deleteOption) result.push(deleteOption);
84+
if (editOption) result.push(editOption);
85+
86+
if (result.length == 0) return null;
87+
88+
return result;
89+
}
90+
return null;
91+
};
92+
93+
var options = generateDropdownOptions();
94+
// = [
95+
// { onClick: handleDelete, label: "Delete reply" },
96+
// { onClick: handleEdit, label: "Edit reply" },
97+
// ];
7198

7299
// Initialize viewOptions to see if a user should be able to see dropdown options
73100
var viewOptions = false;

client/src/components/comments/CommentView.js

+29-34
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ const renderComments = (data, userRole) => {
2121
let ret = [];
2222
if (data) {
2323
data.forEach((comment) => {
24-
ret.push(
25-
<Comment comment={comment} key={comment._id} userRole={userRole} />
26-
);
24+
ret.push(<Comment comment={comment} key={comment._id} />);
2725
});
2826
}
2927
return ret;
@@ -57,7 +55,7 @@ const CommentView = ({ classroomName }) => {
5755
const [highlightedSection, setHighlightedSection] = useState("");
5856
const [pollAns, setPollAns] = useState([{ option: "dummy", votes: 0 }]);
5957

60-
const setUserRole = useContext(UserRoleDispatchContext);
58+
// const setUserRole = useContext(UserRoleDispatchContext);
6159
const userRole = useContext(UserRoleContext);
6260
// console.log("Comment View Role Object: ", userRole);
6361

@@ -70,24 +68,24 @@ const CommentView = ({ classroomName }) => {
7068
setPollAns(newPollAnswers);
7169
};
7270

73-
const attemptGetUserRole = (courseId) => {
74-
LazyFetch({
75-
type: "get",
76-
endpoint: "/api/userRole/" + courseId,
77-
onSuccess: (role) => {
78-
if (role) {
79-
setUserRole(role);
80-
}
81-
},
82-
onFailure: (err) => {
83-
console.log(
84-
"Error getting user role object from {" + courseId + "}:",
85-
err
86-
);
87-
setUserRole(null);
88-
},
89-
});
90-
};
71+
// const attemptGetUserRole = (courseId) => {
72+
// LazyFetch({
73+
// type: "get",
74+
// endpoint: "/api/userRole/" + courseId,
75+
// onSuccess: (role) => {
76+
// if (role) {
77+
// setUserRole(role);
78+
// }
79+
// },
80+
// onFailure: (err) => {
81+
// console.log(
82+
// "Error getting user role object from {" + courseId + "}:",
83+
// err
84+
// );
85+
// setUserRole(null);
86+
// },
87+
// });
88+
// };
9189

9290
const redirect = (sectionFilter) => {
9391
history.push({
@@ -157,7 +155,7 @@ const CommentView = ({ classroomName }) => {
157155
// console.log(commentData);
158156
setCommentData([
159157
...commentData,
160-
<Comment comment={comment} key={comment._id} userRole={userRole} />,
158+
<Comment comment={comment} key={comment._id} />,
161159
]);
162160
}
163161
});
@@ -171,22 +169,20 @@ const CommentView = ({ classroomName }) => {
171169
// console.log(allComments[i], "in for loop");
172170
if (allComments[i].props.comment._id === comment._id) {
173171
// console.log("found a match");
174-
allComments[i] = (
175-
<Comment comment={comment} key={comment._id} userRole={userRole} />
176-
);
172+
allComments[i] = <Comment comment={comment} key={comment._id} />;
177173
break;
178174
}
179175
}
180176
setCommentData(allComments);
181177
});
182178
}, [commentData]);
183179

184-
useEffect(() => {
185-
// console.log("rendered");
186-
if (!userRole) {
187-
attemptGetUserRole(courseId);
188-
}
189-
}, [userRole]);
180+
// useEffect(() => {
181+
// // console.log("rendered");
182+
// if (!userRole) {
183+
// attemptGetUserRole(courseId);
184+
// }
185+
// }, [userRole]);
190186

191187
const draftNewComment = () => {
192188
setNewComments({
@@ -208,7 +204,7 @@ const CommentView = ({ classroomName }) => {
208204
setNewComments({ draft: false });
209205
setCommentData([
210206
...commentData,
211-
<Comment comment={data} key={data._id} userRole={userRole} />,
207+
<Comment comment={data} key={data._id} />,
212208
]);
213209
},
214210
});
@@ -232,7 +228,6 @@ const CommentView = ({ classroomName }) => {
232228
isDraft={true}
233229
callback={finishComment}
234230
key="draft"
235-
userRole={userRole}
236231
/>
237232
);
238233
}

client/src/components/common/Reaction.js

+17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import GreyPlusImg from "../../imgs/grey-plus.svg";
99
import GreyLightbulbImg from "../../imgs/lightbulb-grey.svg";
1010
import { UserContext } from "../context/UserProvider";
1111
import LazyFetch from "./requests/LazyFetch";
12+
import { UserRoleContext } from "../context/UserRoleProvider";
1213

1314
/** Reaction Component
1415
* @brief Intended to be used above "Input" components to label them
@@ -23,6 +24,7 @@ const Reaction = ({ reactions, type, id, postid }) => {
2324
// console.log("reactions: ", reactions);
2425
const urlParams = useParams();
2526
const user = useContext(UserContext);
27+
const userRole = useContext(UserRoleContext);
2628
const [reactionState, setReactions] = useState(reactions);
2729
const [reactClicked, setClicked] = useState({
2830
liked: reactions.likes.includes(user._id),
@@ -49,6 +51,11 @@ const Reaction = ({ reactions, type, id, postid }) => {
4951
}
5052

5153
const handleLike = () => {
54+
if (!userRole.participation.reactions) {
55+
alert("You cannot react to Posts/Comments/Replies/Polls.");
56+
return;
57+
}
58+
5259
LazyFetch({
5360
type: "put",
5461
endpoint: endpoint,
@@ -65,6 +72,11 @@ const Reaction = ({ reactions, type, id, postid }) => {
6572
};
6673

6774
const handleGood = () => {
75+
if (!userRole.participation.reactions) {
76+
alert("You cannot react to Posts/Comments/Replies/Polls.");
77+
return;
78+
}
79+
6880
LazyFetch({
6981
type: "put",
7082
endpoint: endpoint,
@@ -81,6 +93,11 @@ const Reaction = ({ reactions, type, id, postid }) => {
8193
};
8294

8395
const handleHelpful = () => {
96+
if (!userRole.participation.reactions) {
97+
alert("You cannot react to Posts/Comments/Replies/Polls.");
98+
return;
99+
}
100+
84101
LazyFetch({
85102
type: "put",
86103
endpoint: endpoint,

0 commit comments

Comments
 (0)