Skip to content

Commit

Permalink
OM-104: creating business config form implemente
Browse files Browse the repository at this point in the history
  • Loading branch information
olewandowski1 committed Dec 27, 2023
1 parent 66fb769 commit 617021c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 24 deletions.
52 changes: 40 additions & 12 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,43 @@ export function assignVouchers(phCode, workers, dateRanges, clientMutationLabel)
);
}

// export function manageVoucherPrice() {}

// export function fetchVoucherPrices(modulesManager, params) {
// const payload = formatPageQueryWithCount('workerVoucher', params, WORKER_VOUCHER_PROJECTION(modulesManager));
// return graphql(payload, ACTION_TYPE.SEARCH_VOUCHER_PRICES);
// }

// export const clearVoucherPrices = () => (dispatch) => {
// dispatch({
// type: CLEAR(ACTION_TYPE.SEARCH_VOUCHER_PRICES),
// });
// };
export function manageVoucherPrice(key, value, dateValidFrom, dateValidTo, clientMutationLabel) {
const mutationInput = `
${key ? `key: "${key}"` : ''}
${value ? `value: "${value}"` : ''}
${dateValidFrom ? `dateValidFrom: "${dateValidFrom}"` : ''}
${dateValidTo ? `dateValidTo: "${dateValidTo}"` : ''}
`;
const mutation = formatMutation('createBusinessConfig', mutationInput, clientMutationLabel);
const requestedDateTime = new Date();

return graphql(
mutation.payload,
[REQUEST(ACTION_TYPE.MUTATION), SUCCESS(ACTION_TYPE.MANAGE_VOUCHER_PRICE), ERROR(ACTION_TYPE.MUTATION)],
{
actionType: ACTION_TYPE.MANAGE_VOUCHER_PRICE,
clientMutationId: mutation.clientMutationId,
clientMutationLabel,
requestedDateTime,
},
);
}

export function fetchMutation(clientMutationId) {
return graphqlWithVariables(
`
query fetchMutation($clientMutationId: String!) {
mutationLogs(clientMutationId: $clientMutationId) {
edges {
node {
id
status
error
}
}
}
}
`,
{ clientMutationId },
);
}
4 changes: 2 additions & 2 deletions src/components/PriceManagementForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function PriceManagementForm({
// NOTE: maxDate cannot be passed if endDate does not exist.
// Passing any other falsy value will block months manipulation.
// eslint-disable-next-line react/jsx-props-no-spreading
{...(edited?.endDate ? { maxDate: edited.endDate } : null)}
{...(edited?.validTo ? { maxDate: edited.validTo } : null)}
/>
</Grid>
<Grid xs={3} className={classes.item}>
Expand All @@ -48,7 +48,7 @@ function PriceManagementForm({
// NOTE: minDate cannot be passed if startDate does not exist.
// Passing any other falsy value will block months manipulation.
// eslint-disable-next-line react/jsx-props-no-spreading
{...(edited?.startDate ? { minDate: edited.startDate } : null)}
{...(edited?.validFrom ? { minDate: edited.validFrom } : null)}
/>
</Grid>
</Grid>
Expand Down
52 changes: 44 additions & 8 deletions src/components/VoucherPriceManagementForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
import { makeStyles } from '@material-ui/styles';

import {
useModulesManager, useTranslations, journalize,
// coreAlert,
useModulesManager, useTranslations, journalize, parseData, coreAlert,
} from '@openimis/fe-core';
import { MODULE_NAME } from '../constants';
import PriceManagementForm from './PriceManagementForm';
import VoucherPriceSearcher from './VoucherPriceSearcher';
import { fetchMutation, manageVoucherPrice } from '../actions';
import { MODULE_NAME, VOUCHER_PRICE_MANAGEMENT_BUSINESS_KEY } from '../constants';

export const useStyles = makeStyles((theme) => ({
paper: { ...theme.paper.paper, margin: '10px 0 0 0' },
Expand All @@ -31,12 +31,15 @@ function VoucherPriceManagementForm() {
const modulesManager = useModulesManager();
const dispatch = useDispatch();
const classes = useStyles();
const { formatMessage } = useTranslations(MODULE_NAME, modulesManager);
const { formatMessage, formatMessageWithValues } = useTranslations(MODULE_NAME, modulesManager);
const [priceManagement, setPriceManagement] = useState({});
const [priceManagementLoading, setPriceManagementLoading] = useState(false);
const { mutation, submittingMutation } = useSelector((state) => state.workerVoucher);

const priceManagementBlocked = (priceManagement) => !priceManagement?.price
|| !priceManagement?.validFrom || !priceManagement?.validTo;
|| !priceManagement?.validFrom
|| !priceManagement?.validTo
|| priceManagementLoading;

const fetchVoucherPrices = async (params) => {
try {
Expand All @@ -48,12 +51,45 @@ function VoucherPriceManagementForm() {
};

const onPriceManagementChange = async () => {
setPriceManagementLoading(true);
try {
// TODO: alert if error
// < ----- >
await fetchVoucherPrices();
const { payload } = await dispatch(
manageVoucherPrice(
VOUCHER_PRICE_MANAGEMENT_BUSINESS_KEY,
priceManagement?.price,
priceManagement?.validFrom,
priceManagement?.validTo,
'Manage Voucher Price',
),
);

const { clientMutationId } = payload.data.createBusinessConfig;
const mutationResponse = await dispatch(fetchMutation(clientMutationId));
const currentMutation = parseData(mutationResponse.payload.data.mutationLogs)?.[0];

if (currentMutation.error) {
const errorDetails = JSON.parse(currentMutation.error);

dispatch(coreAlert(
formatMessage('workerVoucher.menu.priceManagement'),
formatMessage(errorDetails?.detail || 'NOT_FOUND'),
));
return;
}

dispatch(coreAlert(
formatMessage('workerVoucher.menu.priceManagement'),
formatMessageWithValues('workerVoucher.priceManagement.success', {
price: priceManagement.price,
dateFrom: priceManagement.validFrom,
dateTo: priceManagement.validTo,
}),
));
setPriceManagement({});
} catch (error) {
throw new Error(`[VOUCHER_PRICE_MANAGEMENT]: Price change failed. ${error}`);
} finally {
setPriceManagementLoading(false);
}
};

Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const CONTAINS_LOOKUP = 'Icontains';
export const WORKER_THRESHOLD = 3;
export const VOUCHER_QUANTITY_THRESHOLD = 1000;
export const USER_ECONOMIC_UNIT_STORAGE_KEY = 'userEconomicUnit';
export const VOUCHER_PRICE_MANAGEMENT_BUSINESS_KEY = 'VOUCHER_PRICE_MANAGEMENT_KEY';

const WORKER_VOUCHER_STATUS = {
AWAITING_PAYMENT: 'AWAITING_PAYMENT',
Expand Down
2 changes: 1 addition & 1 deletion src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function reducer(
case SUCCESS(ACTION_TYPE.ASSIGN_VOUCHERS):
return dispatchMutationResp(state, 'assignVouchers', action);
case SUCCESS(ACTION_TYPE.MANAGE_VOUCHER_PRICE):
return dispatchMutationResp(state, 'manageVoucherPrice', action);
return dispatchMutationResp(state, 'createBusinessConfig', action);
default:
return state;
}
Expand Down
4 changes: 3 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,7 @@
"workerVoucher.close": "Cancel",
"workerVoucher.searcher.title": "{count} Voucher Prices",
"workerVoucher.searcher.price": "Price",
"workerVoucher.filter.date": "Date"
"workerVoucher.filter.date": "Date",
"business_config.validation.date_range_overlap": "Unable to create the voucher price for the specified period. There is an existing voucher price defined within the selected date range.",
"workerVoucher.priceManagement.success": "The voucher price has been successfully set at {price}$ for the period from {dateFrom} to {dateTo}."
}

0 comments on commit 617021c

Please sign in to comment.