Skip to content

Commit 333bd01

Browse files
committed
fix: added ability to post ratings to the package
1 parent fae7a95 commit 333bd01

File tree

5 files changed

+217
-9
lines changed

5 files changed

+217
-9
lines changed

frontend/src/pages/package.js

+26-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from "../store/actions/packageActions";
2424
import ShowUserListDialog from "./showUserListDialog";
2525
import ReportPackageForm from "./reportPackageForm";
26+
import RatePackageForm from "./ratePackageForm";
2627
import { Button } from "react-bootstrap";
2728
import PackageRatingGraph from "./packageRatingGraph";
2829

@@ -37,6 +38,7 @@ const PackagePage = () => {
3738
const [togglePackageMaintainersDialog, settogglePackageMaintainersDialog] =
3839
useState(false);
3940
const [showReportForm, setShowReportForm] = useState(false);
41+
const [showRateForm, setRateForm] = useState(false);
4042

4143
const handleIconsClick = (value) => {
4244
if (value === iconsActive) {
@@ -123,7 +125,7 @@ const PackagePage = () => {
123125
<MDBRow>
124126
<MDBCol size="9">{data.description}</MDBCol>
125127

126-
{sideBar(data, setShowReportForm)}
128+
{sideBar(data, setShowReportForm, setRateForm)}
127129
</MDBRow>
128130
</MDBContainer>
129131
</MDBTabsPane>
@@ -194,6 +196,12 @@ const PackagePage = () => {
194196
</MDBContainer>
195197
</MDBTabsPane>
196198
</MDBTabsContent>
199+
<RatePackageForm
200+
namespace={namespace_name}
201+
package={package_name}
202+
show={showRateForm}
203+
onHide={() => setRateForm(false)}
204+
></RatePackageForm>
197205
<ReportPackageForm
198206
namespace={namespace_name}
199207
package={package_name}
@@ -249,7 +257,7 @@ const ViewPackageMaintainersButton = ({
249257
);
250258
};
251259

252-
const sideBar = (data, setShowReportForm) => {
260+
const sideBar = (data, setShowReportForm, setRateForm) => {
253261
return (
254262
<MDBCol size="3">
255263
<p style={{ fontSize: 16, textAlign: "left" }}>Install</p>
@@ -271,13 +279,22 @@ const sideBar = (data, setShowReportForm) => {
271279
<p style={{ fontSize: 16, textAlign: "left" }}>Last publish</p>
272280
{updatedDays(data.updated_at)} days ago
273281
<hr></hr>
274-
<Button
275-
variant="danger"
276-
style={{ margin: 0 }}
277-
onClick={() => setShowReportForm(true)}
278-
>
279-
Report
280-
</Button>
282+
<div>
283+
<Button
284+
variant="success"
285+
style={{ margin: 0 }}
286+
onClick={() => setRateForm(true)}
287+
>
288+
Rate
289+
</Button>
290+
<Button
291+
variant="danger"
292+
style={{ marginLeft: "4px" }}
293+
onClick={() => setShowReportForm(true)}
294+
>
295+
Report
296+
</Button>
297+
</div>
281298
</MDBCol>
282299
);
283300
};

frontend/src/pages/ratePackageForm.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import React, { useEffect, useState } from "react";
2+
import { Form, Button, Modal, Spinner } from "react-bootstrap";
3+
import { useDispatch, useSelector } from "react-redux";
4+
import { toast, ToastContainer } from "react-toastify";
5+
import {
6+
ratePackage,
7+
resetErrorMessage,
8+
} from "../store/actions/ratePackageActions";
9+
10+
const RatePackageForm = (props) => {
11+
const dispatch = useDispatch();
12+
const [rating, setRating] = useState("");
13+
const accessToken = useSelector((state) => state.auth.accessToken);
14+
const isLoading = useSelector((state) => state.ratePackage.isLoading);
15+
const statusCode = useSelector((state) => state.ratePackage.statuscode);
16+
const message = useSelector((state) => state.ratePackage.message);
17+
18+
const handleSubmit = async (e) => {
19+
e.preventDefault();
20+
dispatch(
21+
ratePackage(
22+
{ rating: rating, namespace: props.namespace, package: props.package },
23+
accessToken
24+
)
25+
);
26+
};
27+
28+
useEffect(() => {
29+
if (statusCode === 200) {
30+
toast.success(message);
31+
} else {
32+
toast.error(message);
33+
}
34+
35+
dispatch(resetErrorMessage());
36+
}, [statusCode]);
37+
38+
return (
39+
<Form onSubmit={handleSubmit}>
40+
<Modal {...props} size="md">
41+
<Modal.Header closeButton>
42+
<Modal.Title>Rate Package</Modal.Title>
43+
</Modal.Header>
44+
<ToastContainer
45+
position="top-center"
46+
autoClose={5000}
47+
hideProgressBar={false}
48+
newestOnTop={false}
49+
closeOnClick
50+
rtl={false}
51+
pauseOnFocusLoss
52+
pauseOnHover
53+
theme="light"
54+
/>
55+
<Modal.Body>
56+
<Form.Group className="mb-3" controlId="formRatePackage">
57+
<Form.Label>Please rate this package on a scale of 1-5</Form.Label>
58+
<Form.Control
59+
type="number"
60+
placeholder="Enter rating"
61+
min={1}
62+
max={5}
63+
value={rating}
64+
onChange={(e) => setRating(e.target.value)}
65+
/>
66+
</Form.Group>
67+
</Modal.Body>
68+
<Modal.Footer>
69+
{!isLoading ? (
70+
<Button variant="primary" type="submit" onClick={handleSubmit}>
71+
Rate
72+
</Button>
73+
) : (
74+
<div style={{ margin: 0 }}>
75+
<Spinner
76+
className="spinner-border m-3"
77+
animation="border"
78+
role="status"
79+
>
80+
<span className="visually-hidden">Loading...</span>
81+
</Spinner>
82+
</div>
83+
)}
84+
</Modal.Footer>
85+
</Modal>
86+
</Form>
87+
);
88+
};
89+
90+
export default RatePackageForm;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import axios from "axios";
2+
3+
export const RATE_PACKAGE_REQUEST = "RATE_PACKAGE_REQUEST";
4+
export const RATE_PACKAGE_SUCCESS = "RATE_PACKAGE_SUCCESS";
5+
export const RATE_PACKAGE_FAILURE = "RATE_PACKAGE_FAILURE";
6+
export const RESET_ERROR_MESSAGE = "RESET_ERROR_MESSAGE";
7+
8+
export const ratePackage = (data, access_token) => async (dispatch) => {
9+
let formData = new FormData();
10+
formData.append("rating", data.rating);
11+
12+
let packageName = data.package;
13+
let namespaceName = data.namespace;
14+
15+
try {
16+
dispatch({
17+
type: RATE_PACKAGE_REQUEST,
18+
});
19+
20+
const result = await axios({
21+
method: "post",
22+
url: `${process.env.REACT_APP_REGISTRY_API_URL}/ratings/${namespaceName}/${packageName}`,
23+
data: formData,
24+
headers: {
25+
Authorization: `Bearer ${access_token}`,
26+
},
27+
});
28+
29+
dispatch({
30+
type: RATE_PACKAGE_SUCCESS,
31+
payload: {
32+
message: result.data.message,
33+
statuscode: result.data.code,
34+
},
35+
});
36+
} catch (error) {
37+
dispatch({
38+
type: RATE_PACKAGE_FAILURE,
39+
payload: {
40+
message: error.response.data.message,
41+
statuscode: error.response.data.code,
42+
},
43+
});
44+
}
45+
};
46+
47+
export const resetErrorMessage = () => (dispatch) => {
48+
dispatch({
49+
type: RESET_ERROR_MESSAGE,
50+
});
51+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {
2+
RATE_PACKAGE_REQUEST,
3+
RATE_PACKAGE_SUCCESS,
4+
RATE_PACKAGE_FAILURE,
5+
RESET_ERROR_MESSAGE,
6+
} from "../actions/ratePackageActions";
7+
8+
const initialState = {
9+
isLoading: false,
10+
error: null,
11+
message: null,
12+
statuscode: 0,
13+
};
14+
15+
const ratePackageReducer = (state = initialState, action) => {
16+
switch (action.type) {
17+
case RATE_PACKAGE_REQUEST:
18+
return {
19+
...state,
20+
isLoading: true,
21+
};
22+
case RATE_PACKAGE_SUCCESS:
23+
return {
24+
...state,
25+
isLoading: false,
26+
message: action.payload.message,
27+
statuscode: action.payload.statuscode,
28+
};
29+
case RATE_PACKAGE_FAILURE:
30+
return {
31+
...state,
32+
isLoading: false,
33+
message: action.payload.message,
34+
statuscode: action.payload.statuscode,
35+
};
36+
case RESET_ERROR_MESSAGE:
37+
return {
38+
...state,
39+
error: null,
40+
message: null,
41+
statuscode: 0,
42+
};
43+
default:
44+
return state;
45+
}
46+
};
47+
48+
export default ratePackageReducer;

frontend/src/store/reducers/rootReducer.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import verifyEmailReducer from "./verifyEmailReducer";
1919
import userListReducer from "./userListReducer";
2020
import reportPackageReducer from "./reportPackageReducer";
2121
import viewMalicousReportsReducer from "./viewMalicousReportsReducer";
22+
import ratePackageReducer from "./ratePackageReducer";
2223

2324
const rootReducer = combineReducers({
2425
auth: authReducer,
@@ -41,6 +42,7 @@ const rootReducer = combineReducers({
4142
archives: archivesReducer,
4243
reportPackage: reportPackageReducer,
4344
malicousReport: viewMalicousReportsReducer,
45+
ratePackage: ratePackageReducer,
4446
});
4547

4648
export default rootReducer;

0 commit comments

Comments
 (0)