|
| 1 | +import React, {useState, useEffect} from 'react' |
| 2 | +import './tripApproval.scss' |
| 3 | +import { toast } from 'react-toastify' |
| 4 | +import { useNavigate } from 'react-router-dom' |
| 5 | +import axios from 'axios'; |
| 6 | +import { approveRequest, rejectRequest } from '../tripApproval/trip' |
| 7 | + |
| 8 | + |
| 9 | +function Approval() { |
| 10 | + const [data, setData] = useState([]); |
| 11 | + const [myStatus, setmyStatus] = useState('pending'); |
| 12 | + const [hasApproved, setHasApproved] = useState(false); |
| 13 | + const [hasRejected, setHasRejected] = useState(false); |
| 14 | + const [stat, setStat] = useState(); |
| 15 | + const [message, setMessage] = useState(''); |
| 16 | + const tripId = 10; |
| 17 | + |
| 18 | + const approve = async () => { |
| 19 | + const res = await approveRequest(tripId); |
| 20 | + setHasApproved(true); |
| 21 | + setMessage(res.data); |
| 22 | + if (res.status === 200) { |
| 23 | + setHasApproved(true); |
| 24 | + setMessage(res.data); |
| 25 | + setTimeout(() => { |
| 26 | + setMessage(''); |
| 27 | + setStat('approved'); |
| 28 | + }, 3000); |
| 29 | + |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + const reject = async () => { |
| 34 | + const res = await rejectRequest(tripId); |
| 35 | + setHasRejected(true); |
| 36 | + setMessage(res.data); |
| 37 | + if (res.status === 200) { |
| 38 | + setHasRejected(true); |
| 39 | + setMessage(res.data); |
| 40 | + setTimeout(() => { |
| 41 | + setMessage(''); |
| 42 | + setStat('rejected'); |
| 43 | + }, 3000); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + const renderState = async () => { |
| 51 | + const config = { |
| 52 | + headers: { |
| 53 | + authorization: |
| 54 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjo0LCJlbWFpbCI6Im5kaWN1bmd1eWVzdGV2ZTRAZ21haWwuY29tIiwicm9sZUlkIjoyfSwidG9rZW5JZCI6MTY1MzkyMTAyNzQ4MywiaWF0IjoxNjUzOTIxMDI3LCJleHAiOjExNjUzOTIxMDI3fQ.77CQpRirhvPUhkNBBxs4XBmZ3rPRjs8M-2kLt0d1w-0', |
| 55 | + }, |
| 56 | + }; |
| 57 | + const response = await axios.get( |
| 58 | + `http://localhost:3000/api/trip/request/${tripId}/manager`, |
| 59 | + config |
| 60 | + ); |
| 61 | + setData([response.data.response]); |
| 62 | + }; |
| 63 | + renderState(); |
| 64 | + }, []); |
| 65 | + |
| 66 | + |
| 67 | + console.log('This is response ---->>>>>>', data); |
| 68 | + |
| 69 | + const navigate = useNavigate() |
| 70 | + |
| 71 | + |
| 72 | + return ( |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + <div className='approvalPage'> |
| 77 | + |
| 78 | + { data.map((trips) => { |
| 79 | + { |
| 80 | + return ( |
| 81 | + <div key={ trips.id }> |
| 82 | + <h1>Trip Request Approval</h1> |
| 83 | + <h2 className='requesterInformation'>Requester Information</h2> |
| 84 | + <p>Names: {trips.requester.firstName} {trips.requester.lastName} </p> |
| 85 | + <p>Email: {trips.requester.email}</p> |
| 86 | + <h2 className='tripInformation'>Trip Information</h2> |
| 87 | + <p>Accommodation: { trips.Accommodation.name }</p> |
| 88 | + <p>Departure place: { trips.departurePlace.city } , { trips.departurePlace.Country.name }</p> |
| 89 | + <p>Destination place: { trips.Destination.city } , { trips.Destination.Country.name }</p> |
| 90 | + <p>Departure date: {trips.departureDate}</p> |
| 91 | + <p>Return date: {trips.returnDate}</p> |
| 92 | + <p>Travel reason: { trips.travel_reason }</p> |
| 93 | + <div className="decide"> |
| 94 | + <button className="approve" onClick={approve}>Approve</button> |
| 95 | + <button className="reject" onClick={reject}>Reject</button> |
| 96 | + </div> |
| 97 | + |
| 98 | + </div> |
| 99 | + );} |
| 100 | + })} |
| 101 | + </div> |
| 102 | + ) |
| 103 | +} |
| 104 | +export default Approval; |
| 105 | + |
| 106 | + |
| 107 | + |
0 commit comments