Rev 5829 | Autoría | Ultima modificación | Ver Log |
/* eslint-disable react/prop-types */
import React, { useState } from 'react'
import { axios } from '../../../../utils'
import { Spinner } from 'react-bootstrap'
import { useDispatch } from 'react-redux'
import { addNotification } from '../../../../redux/notification/notification.actions'
const ApplicationSidebar = ({
jobId,
isJobApplied,
showModal,
toggleApply,
}) => {
const [loading, setLoading] = useState(false)
const dispatch = useDispatch()
const handleClick = () => {
if (isJobApplied) {
handleWithdrawApply()
return
}
showModal()
}
const handleWithdrawApply = async () => {
setLoading(true)
axios
.post(`/job/remove-apply-job/${jobId}`)
.then(({ data: response }) => {
const { data, success } = response
if (!success) {
const errorMessage =
typeof data === 'string'
? data
: 'Ha ocurrido un error, por favor intente más tarde'
dispatch(addNotification({ styled: 'danger', msg: errorMessage }))
}
toggleApply()
})
.finally(() => setLoading(false))
}
return (
<>
<div id="div-apply-job" className={`applicationSidebar bid-place`}>
<button
type="button"
className="btn btn-primary btn-apply"
onClick={handleClick}
disabled={loading}
>
{loading ? (
<Spinner animation="border" variant="dark" size="sm" />
) : isJobApplied ? (
'Quitar aplicación'
) : (
'Aplicar'
)}
</button>
</div>
</>
)
}
export default ApplicationSidebar