|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { toast } from 'react-toastify'; |
| 3 | +import axios from 'axios'; |
| 4 | +import Modal from './ParentModal'; |
| 5 | +import { actionType as T } from '../../reducer'; |
| 6 | +import './contributeDetails.css'; |
| 7 | + |
| 8 | +const ContributeDetails = ({ superState, dispatcher }) => { |
| 9 | + const closeModal = () => { |
| 10 | + dispatcher({ type: T.SET_CONTRIBUTE_MODAL, payload: false }); |
| 11 | + }; |
| 12 | + const [study, setStudy] = useState(''); |
| 13 | + const [path, setPath] = useState(''); |
| 14 | + const [auth, setAuth] = useState(''); |
| 15 | + const [title, setTitle] = useState(''); |
| 16 | + const [desc, setDesc] = useState(''); |
| 17 | + const [branch, setBranch] = useState(''); |
| 18 | + const [showAdvanceOptions, setShowAdvanceOptions] = useState(false); |
| 19 | + const submit = async (e) => { |
| 20 | + if (study === '' || path === '' || auth === '') { |
| 21 | + toast.info('Please Provide necessary inputs'); |
| 22 | + return; |
| 23 | + } |
| 24 | + const id = toast.loading('Processing your Request.Please wait...'); |
| 25 | + try { |
| 26 | + e.preventDefault(); |
| 27 | + const result = await axios.post('http://127.0.0.1:5000/contribute', { |
| 28 | + study, |
| 29 | + auth, |
| 30 | + desc, |
| 31 | + title, |
| 32 | + path, |
| 33 | + branch, |
| 34 | + }); |
| 35 | + toast.success(result.data?.message); |
| 36 | + } catch (error) { |
| 37 | + if (error?.response?.status === 400) { |
| 38 | + toast.info(error?.response?.data?.message); |
| 39 | + } else { |
| 40 | + toast.error(error?.response.data.message); |
| 41 | + } |
| 42 | + } |
| 43 | + toast.dismiss(id); |
| 44 | + closeModal(); |
| 45 | + }; |
| 46 | + const toggleOptions = () => { |
| 47 | + setShowAdvanceOptions(!showAdvanceOptions); |
| 48 | + }; |
| 49 | + return ( |
| 50 | + <Modal |
| 51 | + ModelOpen={superState.contributeModal} |
| 52 | + title="Contribute" |
| 53 | + closeModal={closeModal} |
| 54 | + > |
| 55 | + <form className="contribute-details"> |
| 56 | + <span>Study Name</span> |
| 57 | + <input |
| 58 | + required |
| 59 | + value={study} |
| 60 | + onChange={(e) => setStudy(e.target.value)} |
| 61 | + /> |
| 62 | + <span>Study Path</span> |
| 63 | + <input |
| 64 | + placeholder="Enter Full Directory Path of Study" |
| 65 | + value={path} |
| 66 | + onChange={(e) => setPath(e.target.value)} |
| 67 | + required |
| 68 | + /> |
| 69 | + <span>Author Name</span> |
| 70 | + <input |
| 71 | + value={auth} |
| 72 | + onChange={(e) => setAuth(e.target.value)} |
| 73 | + required |
| 74 | + /> |
| 75 | + {showAdvanceOptions && ( |
| 76 | + <> |
| 77 | + <span>Branch Name</span> |
| 78 | + <input |
| 79 | + value={branch} |
| 80 | + onChange={(e) => setBranch(e.target.value)} |
| 81 | + /> |
| 82 | + <span>Title of Study</span> |
| 83 | + <input |
| 84 | + value={title} |
| 85 | + onChange={(e) => setTitle(e.target.value)} |
| 86 | + /> |
| 87 | + <span>Description of Study</span> |
| 88 | + <textarea |
| 89 | + value={desc} |
| 90 | + onChange={(e) => setDesc(e.target.value)} |
| 91 | + /> |
| 92 | + </> |
| 93 | + )} |
| 94 | + <button type="button" className="btn btn-secondary" onClick={toggleOptions}> |
| 95 | + {showAdvanceOptions ? 'Hide ' : 'Show '} |
| 96 | + Advance Options |
| 97 | + </button> |
| 98 | + <div className="expand"> |
| 99 | + <button type="submit" className="btn btn-primary" onClick={submit}>Generate PR</button> |
| 100 | + </div> |
| 101 | + </form> |
| 102 | + </Modal> |
| 103 | + ); |
| 104 | +}; |
| 105 | +export default ContributeDetails; |
0 commit comments