-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from arteevraina/post-ratings-from-frontend
fix: added ability to post ratings to the package
- Loading branch information
Showing
5 changed files
with
217 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { Form, Button, Modal, Spinner } from "react-bootstrap"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { toast, ToastContainer } from "react-toastify"; | ||
import { | ||
ratePackage, | ||
resetErrorMessage, | ||
} from "../store/actions/ratePackageActions"; | ||
|
||
const RatePackageForm = (props) => { | ||
const dispatch = useDispatch(); | ||
const [rating, setRating] = useState(""); | ||
const accessToken = useSelector((state) => state.auth.accessToken); | ||
const isLoading = useSelector((state) => state.ratePackage.isLoading); | ||
const statusCode = useSelector((state) => state.ratePackage.statuscode); | ||
const message = useSelector((state) => state.ratePackage.message); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
dispatch( | ||
ratePackage( | ||
{ rating: rating, namespace: props.namespace, package: props.package }, | ||
accessToken | ||
) | ||
); | ||
}; | ||
|
||
useEffect(() => { | ||
if (statusCode === 200) { | ||
toast.success(message); | ||
} else { | ||
toast.error(message); | ||
} | ||
|
||
dispatch(resetErrorMessage()); | ||
}, [statusCode]); | ||
|
||
return ( | ||
<Form onSubmit={handleSubmit}> | ||
<Modal {...props} size="md"> | ||
<Modal.Header closeButton> | ||
<Modal.Title>Rate Package</Modal.Title> | ||
</Modal.Header> | ||
<ToastContainer | ||
position="top-center" | ||
autoClose={5000} | ||
hideProgressBar={false} | ||
newestOnTop={false} | ||
closeOnClick | ||
rtl={false} | ||
pauseOnFocusLoss | ||
pauseOnHover | ||
theme="light" | ||
/> | ||
<Modal.Body> | ||
<Form.Group className="mb-3" controlId="formRatePackage"> | ||
<Form.Label>Please rate this package on a scale of 1-5</Form.Label> | ||
<Form.Control | ||
type="number" | ||
placeholder="Enter rating" | ||
min={1} | ||
max={5} | ||
value={rating} | ||
onChange={(e) => setRating(e.target.value)} | ||
/> | ||
</Form.Group> | ||
</Modal.Body> | ||
<Modal.Footer> | ||
{!isLoading ? ( | ||
<Button variant="primary" type="submit" onClick={handleSubmit}> | ||
Rate | ||
</Button> | ||
) : ( | ||
<div style={{ margin: 0 }}> | ||
<Spinner | ||
className="spinner-border m-3" | ||
animation="border" | ||
role="status" | ||
> | ||
<span className="visually-hidden">Loading...</span> | ||
</Spinner> | ||
</div> | ||
)} | ||
</Modal.Footer> | ||
</Modal> | ||
</Form> | ||
); | ||
}; | ||
|
||
export default RatePackageForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import axios from "axios"; | ||
|
||
export const RATE_PACKAGE_REQUEST = "RATE_PACKAGE_REQUEST"; | ||
export const RATE_PACKAGE_SUCCESS = "RATE_PACKAGE_SUCCESS"; | ||
export const RATE_PACKAGE_FAILURE = "RATE_PACKAGE_FAILURE"; | ||
export const RESET_ERROR_MESSAGE = "RESET_ERROR_MESSAGE"; | ||
|
||
export const ratePackage = (data, access_token) => async (dispatch) => { | ||
let formData = new FormData(); | ||
formData.append("rating", data.rating); | ||
|
||
let packageName = data.package; | ||
let namespaceName = data.namespace; | ||
|
||
try { | ||
dispatch({ | ||
type: RATE_PACKAGE_REQUEST, | ||
}); | ||
|
||
const result = await axios({ | ||
method: "post", | ||
url: `${process.env.REACT_APP_REGISTRY_API_URL}/ratings/${namespaceName}/${packageName}`, | ||
data: formData, | ||
headers: { | ||
Authorization: `Bearer ${access_token}`, | ||
}, | ||
}); | ||
|
||
dispatch({ | ||
type: RATE_PACKAGE_SUCCESS, | ||
payload: { | ||
message: result.data.message, | ||
statuscode: result.data.code, | ||
}, | ||
}); | ||
} catch (error) { | ||
dispatch({ | ||
type: RATE_PACKAGE_FAILURE, | ||
payload: { | ||
message: error.response.data.message, | ||
statuscode: error.response.data.code, | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
export const resetErrorMessage = () => (dispatch) => { | ||
dispatch({ | ||
type: RESET_ERROR_MESSAGE, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
RATE_PACKAGE_REQUEST, | ||
RATE_PACKAGE_SUCCESS, | ||
RATE_PACKAGE_FAILURE, | ||
RESET_ERROR_MESSAGE, | ||
} from "../actions/ratePackageActions"; | ||
|
||
const initialState = { | ||
isLoading: false, | ||
error: null, | ||
message: null, | ||
statuscode: 0, | ||
}; | ||
|
||
const ratePackageReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case RATE_PACKAGE_REQUEST: | ||
return { | ||
...state, | ||
isLoading: true, | ||
}; | ||
case RATE_PACKAGE_SUCCESS: | ||
return { | ||
...state, | ||
isLoading: false, | ||
message: action.payload.message, | ||
statuscode: action.payload.statuscode, | ||
}; | ||
case RATE_PACKAGE_FAILURE: | ||
return { | ||
...state, | ||
isLoading: false, | ||
message: action.payload.message, | ||
statuscode: action.payload.statuscode, | ||
}; | ||
case RESET_ERROR_MESSAGE: | ||
return { | ||
...state, | ||
error: null, | ||
message: null, | ||
statuscode: 0, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default ratePackageReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters