-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OM-325: add assignment progress tracker (#83)
* OM-325: add assignment progress tracker * OM-325: sync with be * OM-3325: fix sonar
- Loading branch information
1 parent
943957e
commit d7ed8f1
Showing
9 changed files
with
225 additions
and
47 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
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,77 @@ | ||
import React, { | ||
useEffect, useRef, useState, useCallback, | ||
} from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import _ from 'lodash'; | ||
import _debounce from 'lodash/debounce'; | ||
|
||
import { CircularProgress, Grid, Typography } from '@material-ui/core'; | ||
import CheckCircleIcon from '@material-ui/icons/CheckCircle'; | ||
import { makeStyles } from '@material-ui/styles'; | ||
|
||
import { useToast, useTranslations } from '@openimis/fe-core'; | ||
import { createOrUpdateVoucherDraftForm } from '../actions'; | ||
import { DEFAULT_DEBOUNCE_TIME, MODULE_NAME } from '../constants'; | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
container: { | ||
display: 'flex', | ||
alignItems: 'center', | ||
justifyContent: 'start', | ||
gap: '4px', | ||
padding: theme.spacing(1), | ||
}, | ||
})); | ||
|
||
function VoucherAssignmentProgressTracker({ voucherAssignment }) { | ||
const prevVoucherAssignment = useRef(); | ||
const dispatch = useDispatch(); | ||
const classes = useStyles(); | ||
const { showError } = useToast(); | ||
const { formatMessage } = useTranslations(MODULE_NAME); | ||
const [isSaving, setIsSaving] = useState(false); | ||
|
||
const saveAssignmentDraft = useCallback( | ||
_debounce(async (voucherAssignment) => { | ||
try { | ||
await dispatch(createOrUpdateVoucherDraftForm(voucherAssignment, 'Save Assignment Draft')); | ||
} catch (error) { | ||
showError('[ERROR]: Error while saving the progress.'); | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
} finally { | ||
setIsSaving(false); | ||
} | ||
}, DEFAULT_DEBOUNCE_TIME * 2), | ||
[dispatch, showError], | ||
); | ||
|
||
useEffect(() => { | ||
if ( | ||
!_.isEqual(voucherAssignment, prevVoucherAssignment.current) | ||
&& !_.isEmpty(voucherAssignment) | ||
) { | ||
setIsSaving(true); | ||
prevVoucherAssignment.current = voucherAssignment; | ||
saveAssignmentDraft(voucherAssignment); | ||
} | ||
}, [voucherAssignment, saveAssignmentDraft]); | ||
|
||
return ( | ||
<Grid className={classes.container}> | ||
{isSaving ? ( | ||
<> | ||
<CircularProgress size={16} thickness={5} /> | ||
<Typography variant="body2">{formatMessage('VoucherAssignmentProgressTracker.saving')}</Typography> | ||
</> | ||
) : ( | ||
<> | ||
<CheckCircleIcon color="primary" fontSize="small" /> | ||
<Typography variant="body2">{formatMessage('VoucherAssignmentProgressTracker.upToDate')}</Typography> | ||
</> | ||
)} | ||
</Grid> | ||
); | ||
} | ||
|
||
export default VoucherAssignmentProgressTracker; |
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
Oops, something went wrong.