-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jan
committed
Jan 31, 2024
1 parent
845daec
commit 0e567e5
Showing
6 changed files
with
218 additions
and
31 deletions.
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
src/components/payroll/PayrollPaymentDataUploadDialog.js
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,188 @@ | ||
import React, { useState } from 'react'; | ||
import { Input, Grid } from '@material-ui/core'; | ||
import { injectIntl } from 'react-intl'; | ||
import Button from '@material-ui/core/Button'; | ||
import Dialog from '@material-ui/core/Dialog'; | ||
import DialogActions from '@material-ui/core/DialogActions'; | ||
import DialogContent from '@material-ui/core/DialogContent'; | ||
import DialogTitle from '@material-ui/core/DialogTitle'; | ||
import { | ||
apiHeaders, | ||
baseApiUrl, | ||
formatMessage, | ||
} from '@openimis/fe-core'; | ||
import { withTheme, withStyles } from '@material-ui/core/styles'; | ||
import { connect } from 'react-redux'; | ||
import { bindActionCreators } from 'redux'; | ||
|
||
const styles = (theme) => ({ | ||
item: theme.paper.item, | ||
}); | ||
|
||
function PayrollPaymentDataUploadDialog({ | ||
intl, | ||
classes, | ||
payrollUuid, | ||
}) { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [forms, setForms] = useState({}); | ||
|
||
const handleOpen = () => { | ||
setIsOpen(true); | ||
}; | ||
|
||
const handleClose = () => { | ||
setForms({}); | ||
setIsOpen(false); | ||
}; | ||
|
||
const handleFieldChange = (formName, fieldName, value) => { | ||
setForms({ | ||
...forms, | ||
[formName]: { | ||
...(forms[formName] ?? {}), | ||
[fieldName]: value, | ||
}, | ||
}); | ||
}; | ||
|
||
const onSubmit = async (values) => { | ||
const fileFormat = values.file.type; | ||
const formData = new FormData(); | ||
|
||
formData.append('file', values.file); | ||
|
||
let urlImport; | ||
if (fileFormat.includes('/csv')) { | ||
formData.append('payroll', payrollUuid); | ||
urlImport = `${baseApiUrl}/payroll/import_payment_data/`; | ||
} | ||
|
||
try { | ||
const response = await fetch(urlImport, { | ||
headers: apiHeaders, | ||
body: formData, | ||
method: 'POST', | ||
credentials: 'same-origin', | ||
}); | ||
|
||
await response.json(); | ||
|
||
if (response.status >= 400) { | ||
handleClose(); | ||
return; | ||
} | ||
handleClose(); | ||
} catch (error) { | ||
handleClose(); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button | ||
onClick={handleOpen} | ||
variant="outlined" | ||
color="#DFEDEF" | ||
className={classes.button} | ||
style={{ | ||
border: '0px', | ||
marginTop: '6px', | ||
}} | ||
> | ||
{formatMessage(intl, 'payroll', 'payroll.paymentData.upload.label')} | ||
</Button> | ||
<Dialog | ||
open={isOpen} | ||
onClose={handleClose} | ||
PaperProps={{ | ||
style: { | ||
width: 600, | ||
maxWidth: 1000, | ||
}, | ||
}} | ||
> | ||
<form noValidate> | ||
<DialogTitle | ||
style={{ | ||
marginTop: '10px', | ||
}} | ||
> | ||
{formatMessage(intl, 'payroll', 'payroll.paymentData.upload.label')} | ||
</DialogTitle> | ||
<DialogContent> | ||
<div | ||
style={{ backgroundColor: '#DFEDEF', paddingLeft: '10px', paddingBottom: '10px' }} | ||
> | ||
<Grid item> | ||
<Grid container spacing={4} direction="column"> | ||
<Grid item> | ||
<Input | ||
onChange={(event) => handleFieldChange('paymentData', 'file', event.target.files[0])} | ||
required | ||
id="import-button" | ||
inputProps={{ | ||
accept: '.csv, application/csv, text/csv', | ||
}} | ||
type="file" | ||
/> | ||
</Grid> | ||
</Grid> | ||
</Grid> | ||
</div> | ||
</DialogContent> | ||
<DialogActions | ||
style={{ | ||
display: 'inline', | ||
paddingLeft: '10px', | ||
marginTop: '25px', | ||
marginBottom: '15px', | ||
}} | ||
> | ||
<div style={{ maxWidth: '1000px' }}> | ||
<div style={{ float: 'left' }}> | ||
<Button | ||
onClick={handleClose} | ||
variant="outlined" | ||
autoFocus | ||
style={{ | ||
margin: '0 16px', | ||
marginBottom: '15px', | ||
}} | ||
> | ||
Cancel | ||
</Button> | ||
</div> | ||
<div style={{ float: 'right', paddingRight: '16px' }}> | ||
<Button | ||
variant="contained" | ||
color="primary" | ||
onClick={() => onSubmit(forms.paymentData)} | ||
disabled={!(forms.paymentData?.file && payrollUuid)} | ||
> | ||
{formatMessage(intl, 'payroll', 'payroll.paymentData.upload.label')} | ||
</Button> | ||
</div> | ||
</div> | ||
</DialogActions> | ||
</form> | ||
</Dialog> | ||
</> | ||
); | ||
} | ||
|
||
const mapStateToProps = (state) => ({ | ||
rights: !!state.core && !!state.core.user && !!state.core.user.i_user ? state.core.user.i_user.rights : [], | ||
confirmed: state.core.confirmed, | ||
}); | ||
|
||
const mapDispatchToProps = (dispatch) => bindActionCreators({ | ||
}, dispatch); | ||
|
||
export default injectIntl( | ||
withTheme( | ||
withStyles(styles)( | ||
connect(mapStateToProps, mapDispatchToProps)(PayrollPaymentDataUploadDialog), | ||
), | ||
), | ||
); |
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
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