Skip to content

Commit

Permalink
OM-93: voucher assignment page, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
olewandowski1 committed Dec 4, 2023
1 parent 851df05 commit cec7ca0
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 14 deletions.
62 changes: 62 additions & 0 deletions src/components/AssignmentVoucherForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';

import { Grid, Divider } from '@material-ui/core';

import { PublishedComponent, TextInput } from '@openimis/fe-core';

function AssignmentVoucherForm({
classes, readOnly = false, edited, onEditedChange, formatMessage,
}) {
return (
<>
<Grid container direction="row">
<Grid item xs={12} className={classes.item}>
<PublishedComponent
module="workerVoucher"
pubRef="workerVoucher.WorkerMultiplePicker"
readOnly={readOnly}
required
classes={classes}
value={edited?.workers ?? []}
onChange={(workers) => onEditedChange({ ...edited, workers })}
previousWorkers
/>
</Grid>
</Grid>
<Divider style={{ margin: '12px 0' }} />
<Grid container xs={12}>
<Grid item xs={3} className={classes.item}>
<TextInput
module="workerVoucher"
label="workerVoucher.employer.code"
value={edited?.employer?.code ?? formatMessage('workerVoucher.WorkerDateRangePicker.notAvailable')}
readOnly
/>
</Grid>
<Grid item xs={3} className={classes.item}>
<TextInput
module="workerVoucher"
label="workerVoucher.employer.tradename"
value={edited?.employer?.code ?? formatMessage('workerVoucher.WorkerDateRangePicker.notAvailable')}
readOnly
/>
</Grid>
</Grid>
<Divider style={{ margin: '12px 0' }} />
<Grid container xs={12}>
<PublishedComponent
module="workerVoucher"
pubRef="workerVoucher.WorkerDateRangePicker"
readOnly={readOnly}
required
classes={classes}
value={edited?.dateRanges ?? []}
onChange={(dateRanges) => onEditedChange({ ...edited, dateRanges })}
/>
</Grid>
<Divider style={{ margin: '12px 0' }} />
</>
);
}

export default AssignmentVoucherForm;
8 changes: 4 additions & 4 deletions src/components/VoucherAcquirementGenericVoucher.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function VoucherAcquirementGenericVoucher() {
const [voucherAcquirement, setVoucherAcquirement] = useState({});
const [isPaymentModalOpen, setIsPaymentModalOpen] = useState(false);

const canAcquire = (voucherAcquirement) => !voucherAcquirement?.quantity
const acquirementBlocked = (voucherAcquirement) => !voucherAcquirement?.quantity
|| voucherAcquirement?.quantity > VOUCHER_QUANTITY_THRESHOLD;

const onVoucherAcquire = () => {
Expand Down Expand Up @@ -58,16 +58,16 @@ function VoucherAcquirementGenericVoucher() {
<Grid xs={12}>
<Grid container className={classes.paperHeaderTitle}>
<Typography variant="h5">{formatMessage('workerVoucher.acquirement.method.GENERIC_VOUCHER')}</Typography>
<Tooltip title={canAcquire(voucherAcquirement)
? formatMessage('workerVoucher.acquire.vouchers.required')
<Tooltip title={acquirementBlocked(voucherAcquirement)
? formatMessage('workerVoucher.vouchers.required')
: formatMessage('workerVoucher.acquire.vouchers')}
>
<span>
<Button
variant="outlined"
style={{ border: 0 }}
onClick={onVoucherAcquire}
disabled={canAcquire(voucherAcquirement)}
disabled={acquirementBlocked(voucherAcquirement)}
>
<Typography variant="subtitle1">{formatMessage('workerVoucher.acquire.voucher')}</Typography>
</Button>
Expand Down
2 changes: 1 addition & 1 deletion src/components/VoucherAcquirementPaymentModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function VoucherAcquirementPaymentModal({
<Divider style={{ margin: '12px 0' }} />
<DialogActions>
<Button onClick={onClose} className={classes.secondaryButton}>
{formatMessage('workerVoucher.VoucherAcquirementPaymentModal.close')}
{formatMessage('workerVoucher.close')}
</Button>
{acquireButtonDisabled ? (
<Tooltip title={formatMessage('workerVoucher.VoucherAcquirementPaymentModal.confirm.tooltip')}>
Expand Down
8 changes: 4 additions & 4 deletions src/components/VoucherAcquirementSpecificWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function VoucherAcquirementSpecificWorker() {
const [voucherAcquirement, setVoucherAcquirement] = useState({});
const [isPaymentModalOpen, setIsPaymentModalOpen] = useState(false);

const canAcquire = (voucherAcquirement) => !voucherAcquirement?.workers?.length
const acquirementBlocked = (voucherAcquirement) => !voucherAcquirement?.workers?.length
|| !voucherAcquirement?.dateRanges?.length;

const onVoucherAcquire = () => {
Expand Down Expand Up @@ -58,16 +58,16 @@ function VoucherAcquirementSpecificWorker() {
<Grid xs={12}>
<Grid container className={classes.paperHeaderTitle}>
<Typography variant="h5">{formatMessage('workerVoucher.acquirement.method.SPECIFIC_WORKER')}</Typography>
<Tooltip title={canAcquire(voucherAcquirement)
? formatMessage('workerVoucher.acquire.vouchers.required')
<Tooltip title={acquirementBlocked(voucherAcquirement)
? formatMessage('workerVoucher.vouchers.required')
: formatMessage('workerVoucher.acquire.vouchers')}
>
<span>
<Button
variant="outlined"
style={{ border: 0 }}
onClick={onVoucherAcquire}
disabled={canAcquire(voucherAcquirement)}
disabled={acquirementBlocked(voucherAcquirement)}
>
<Typography variant="subtitle1">{formatMessage('workerVoucher.acquire.voucher')}</Typography>
</Button>
Expand Down
105 changes: 105 additions & 0 deletions src/components/VoucherAssignmentConfirmModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import React, { useState } from 'react';

import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
Divider,
CircularProgress,
Grid,
Typography,
Checkbox,
FormControlLabel,
Tooltip,
} from '@material-ui/core';
import { makeStyles } from '@material-ui/styles';

import { useTranslations, useModulesManager, NumberInput } from '@openimis/fe-core';
import { MODULE_NAME } from '../constants';

export const useStyles = makeStyles((theme) => ({
primaryButton: theme.dialog.primaryButton,
secondaryButton: theme.dialog.secondaryButton,
item: theme.paper.item,
}));

function VoucherAssignmentConfirmModal({
openState,
onClose,
onConfirm,
isLoading,
error,
assignmentSummary,
readOnly = true,
}) {
const classes = useStyles();
const modulesManager = useModulesManager();
const { formatMessage } = useTranslations(MODULE_NAME, modulesManager);
const [acceptAssignment, setAcceptAssignment] = useState(false);
const acquireButtonDisabled = !acceptAssignment || isLoading || error;

const renderContent = () => {
if (error) {
return <Typography color="error">{error}</Typography>;
}

if (isLoading) {
return <CircularProgress />;
}

return (
<Grid container>
<Grid xs={4} className={classes.item}>
<NumberInput
module="workerVoucher"
label="workerVoucher.acquire.vouchersQuantity"
value={assignmentSummary?.vouchers}
readOnly={readOnly}
/>
</Grid>
<FormControlLabel
style={{ margin: '12px 0 0 0' }}
control={(
<Checkbox
color="primary"
checked={acceptAssignment}
onChange={(e) => setAcceptAssignment(e.target.checked)}
/>
)}
label={formatMessage('workerVoucher.assign.confirmation')}
/>
</Grid>
);
};

return (
<Dialog open={openState} onClose={onClose}>
<DialogTitle>{formatMessage('workerVoucher.VoucherAssignmentConfirmModal.title')}</DialogTitle>
<Divider />
<DialogContent>{renderContent()}</DialogContent>
<Divider style={{ margin: '12px 0' }} />
<DialogActions>
<Button onClick={onClose} className={classes.secondaryButton}>
{formatMessage('workerVoucher.close')}
</Button>
{acquireButtonDisabled ? (
<Tooltip title={formatMessage('workerVoucher.VoucherAssignmentConfirmModal.confirm.tooltip')}>
<span>
<Button onClick={onConfirm} autoFocus className={classes.primaryButton} disabled={acquireButtonDisabled}>
{formatMessage('workerVoucher.VoucherAssignmentConfirmModal.confirm')}
</Button>
</span>
</Tooltip>
) : (
<Button onClick={onConfirm} autoFocus className={classes.primaryButton} disabled={acquireButtonDisabled}>
{formatMessage('workerVoucher.VoucherAssignmentConfirmModal.confirm')}
</Button>
)}
</DialogActions>
</Dialog>
);
}

export default VoucherAssignmentConfirmModal;
107 changes: 107 additions & 0 deletions src/components/VoucherAssignmentForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import React, { useState, useEffect } from 'react';

import {
Divider, Grid, Paper, Typography, Button, Tooltip,
} from '@material-ui/core';
import { makeStyles } from '@material-ui/styles';

import { useModulesManager, useTranslations } from '@openimis/fe-core';
import { MODULE_NAME, USER_ECONOMIC_UNIT_STORAGE_KEY } from '../constants';
import AssignmentVoucherForm from './AssignmentVoucherForm';
import VoucherAssignmentConfirmModal from './VoucherAssignmentConfirmModal';

export const useStyles = makeStyles((theme) => ({
paper: { ...theme.paper.paper, margin: '10px 0 0 0' },
paperHeaderTitle: {
...theme.paper.title,
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
},
tableTitle: theme.table.title,
item: theme.paper.item,
}));

function VoucherAssignmentForm() {
const modulesManager = useModulesManager();
const classes = useStyles();
const { formatMessage } = useTranslations(MODULE_NAME, modulesManager);
const [voucherAssignment, setVoucherAssignment] = useState({});
const [isConfirmationModalOpen, setIsConfirmationModalOpen] = useState(false);

const assignmentBlocked = (voucherAssignment) => !voucherAssignment?.workers?.length
|| !voucherAssignment?.dateRanges?.length;

const onVoucherAssign = () => {
setIsConfirmationModalOpen((prevState) => !prevState);
// TODO: Fetch info about assignment (assignmentSummary)
};

const onAssignmentConfirmation = () => {
// TODO: After summary fetch, assign vouchers to the Workers.
setIsConfirmationModalOpen((prevState) => !prevState);
console.log('Assign Vouchers to the Workers');
};

useEffect(() => {
const storedUserEconomicUnit = localStorage.getItem(USER_ECONOMIC_UNIT_STORAGE_KEY);

if (storedUserEconomicUnit) {
const userEconomicUnit = JSON.parse(storedUserEconomicUnit);

setVoucherAssignment((prevState) => ({ ...prevState, employer: userEconomicUnit }));
}
}, [setVoucherAssignment]);

return (
<Grid container>
<Grid xs={12}>
<Paper className={classes.paper}>
<Grid xs={12}>
<Grid container className={classes.paperHeaderTitle}>
<Typography variant="h5">{formatMessage('workerVoucher.menu.voucherAssignment')}</Typography>
<Tooltip
title={
assignmentBlocked(voucherAssignment)
? formatMessage('workerVoucher.vouchers.required')
: formatMessage('workerVoucher.assign.vouchers')
}
>
<span>
<Button
variant="outlined"
style={{ border: 0 }}
onClick={onVoucherAssign}
disabled={assignmentBlocked(voucherAssignment)}
>
<Typography variant="subtitle1">{formatMessage('workerVoucher.assign.voucher')}</Typography>
</Button>
</span>
</Tooltip>
</Grid>
</Grid>
<Divider />
<AssignmentVoucherForm
edited={voucherAssignment}
onEditedChange={setVoucherAssignment}
formatMessage={formatMessage}
classes={classes}
/>
<VoucherAssignmentConfirmModal
openState={isConfirmationModalOpen}
onClose={() => setIsConfirmationModalOpen((prevState) => !prevState)}
onConfirm={onAssignmentConfirmation}
// TODO: Change after BE implementation
isLoading={false}
error={false}
assignmentSummary={{
vouchers: 100,
}}
/>
</Paper>
</Grid>
</Grid>
);
}

export default VoucherAssignmentForm;
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const VOUCHER_RIGHT_SEARCH = 204001;
export const EMPLOYER_RIGHT_SEARCH = 204001;
export const MODULE_NAME = 'workerVoucher';

export const REF_ROUTE_WORKER_VOUCHER = 'workerVoucher.route.workerVoucher';
Expand Down
25 changes: 22 additions & 3 deletions src/pages/VoucherAssignmentPage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import React from 'react';
import { useSelector } from 'react-redux';

import { makeStyles } from '@material-ui/styles';

import { Helmet, useModulesManager, useTranslations } from '@openimis/fe-core';
import { EMPLOYER_RIGHT_SEARCH, MODULE_NAME } from '../constants';
import VoucherAssignmentForm from '../components/VoucherAssignmentForm';

export const useStyles = makeStyles((theme) => ({
page: theme.page,
}));

function VoucherAssignmentPage() {
const modulesManager = useModulesManager();
const classes = useStyles();
const { formatMessage } = useTranslations(MODULE_NAME, modulesManager);
const rights = useSelector((state) => state.core?.user?.i_user?.rights ?? []);

return (
<div>
VoucherAssignmentPage
</div>
rights.includes(EMPLOYER_RIGHT_SEARCH) && (
<div className={classes.page}>
<Helmet title={formatMessage('workerVoucher.menu.voucherAssignment')} />
<VoucherAssignmentForm />
</div>
)
);
}

Expand Down
Loading

0 comments on commit cec7ca0

Please sign in to comment.