-
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.
* feat: added the UI for report form * feat: connected ui to the report package api * fix: show correct responses from the server * refactor: post malicous api and remove stale conditions
- Loading branch information
1 parent
adb929e
commit 4485488
Showing
6 changed files
with
219 additions
and
22 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
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,99 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { Form, Button, Modal, Spinner } from "react-bootstrap"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { | ||
reportPackage, | ||
resetErrorMessage, | ||
} from "../store/actions/reportPackageActions"; | ||
import { toast, ToastContainer } from "react-toastify"; | ||
import { reset } from "../store/actions/accountActions"; | ||
|
||
const ReportPackageForm = (props) => { | ||
const dispatch = useDispatch(); | ||
const [reason, setReason] = useState(""); | ||
const accessToken = useSelector((state) => state.auth.accessToken); | ||
const isLoading = useSelector((state) => state.reportPackage.isLoading); | ||
const statusCode = useSelector((state) => state.reportPackage.statuscode); | ||
const message = useSelector((state) => state.reportPackage.message); | ||
|
||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
dispatch( | ||
reportPackage( | ||
{ reason: reason, namespace: props.namespace, package: props.package }, | ||
accessToken | ||
) | ||
); | ||
}; | ||
|
||
useEffect(() => { | ||
console.log("Inside use effect"); | ||
if (statusCode === 200) { | ||
toast.success(message); | ||
} else { | ||
toast.error(message); | ||
} | ||
|
||
dispatch(resetErrorMessage()); | ||
}, [statusCode]); | ||
|
||
return ( | ||
<Form onSubmit={handleSubmit}> | ||
<Modal {...props} size="lg"> | ||
<Modal.Header closeButton> | ||
<Modal.Title>Report 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="formReportPackage" | ||
id="namespace-description-textfield" | ||
> | ||
<Form.Label>Reason for reporting package</Form.Label> | ||
<Form.Control | ||
type="text" | ||
placeholder="Describe the reason for reporting the package" | ||
as="textarea" | ||
name="report_description" | ||
value={reason} | ||
onChange={(e) => setReason(e.target.value)} | ||
/> | ||
<Form.Text className="text-muted"> | ||
Write a brief description of the reason for reporting the package. | ||
</Form.Text> | ||
</Form.Group> | ||
</Modal.Body> | ||
<Modal.Footer> | ||
{!isLoading ? ( | ||
<Button variant="primary" type="submit" onClick={handleSubmit}> | ||
Submit | ||
</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 ReportPackageForm; |
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,52 @@ | ||
import axios from "axios"; | ||
|
||
export const REPORT_PACKAGE_REQUEST = "REPORT_PACKAGE_REQUEST"; | ||
export const REPORT_PACKAGE_SUCCESS = "REPORT_PACKAGE_SUCCESS"; | ||
export const REPORT_PACKAGE_FAILURE = "REPORT_PACKAGE_FAILURE"; | ||
|
||
export const RESET_ERROR_MESSAGE = "RESET_ERROR_MESSAGE"; | ||
|
||
export const reportPackage = (data, access_token) => async (dispatch) => { | ||
let formData = new FormData(); | ||
formData.append("reason", data.reason); | ||
|
||
let package_name = data.package; | ||
let namespace_name = data.namespace; | ||
|
||
try { | ||
dispatch({ | ||
type: REPORT_PACKAGE_REQUEST, | ||
}); | ||
|
||
const result = await axios({ | ||
method: "post", | ||
url: `${process.env.REACT_APP_REGISTRY_API_URL}/report/${namespace_name}/${package_name}`, | ||
data: formData, | ||
headers: { | ||
Authorization: `Bearer ${access_token}`, | ||
}, | ||
}); | ||
|
||
dispatch({ | ||
type: REPORT_PACKAGE_SUCCESS, | ||
payload: { | ||
message: result.data.message, | ||
statuscode: result.data.code, | ||
}, | ||
}); | ||
} catch (error) { | ||
dispatch({ | ||
type: REPORT_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 { | ||
REPORT_PACKAGE_REQUEST, | ||
REPORT_PACKAGE_SUCCESS, | ||
REPORT_PACKAGE_FAILURE, | ||
RESET_ERROR_MESSAGE, | ||
} from "../actions/reportPackageActions"; | ||
|
||
const initialState = { | ||
isLoading: false, | ||
error: null, | ||
message: null, | ||
statuscode: 0, | ||
}; | ||
|
||
const reportPackageReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case REPORT_PACKAGE_REQUEST: | ||
return { | ||
...state, | ||
isLoading: true, | ||
}; | ||
case REPORT_PACKAGE_SUCCESS: | ||
return { | ||
...state, | ||
isLoading: false, | ||
message: action.payload.message, | ||
statuscode: action.payload.statuscode, | ||
}; | ||
case REPORT_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 reportPackageReducer; |
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