Skip to content

Commit

Permalink
UIOR-562 fix callout exceptions, previous username (#861)
Browse files Browse the repository at this point in the history
* UIOR-562 fix callout exceptions, previous username
  • Loading branch information
aliaksei-chumakou authored May 13, 2020
1 parent ec7f169 commit 4225771
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 90 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## (IN PROGRESS)

## [2.0.5](https://github.com/folio-org/ui-orders/tree/v2.0.5) (2020-05-13)
[Full Changelog](https://github.com/folio-org/ui-orders/compare/v2.0.4...v2.0.5)

### Bug fixes
* [UIOR-562](https://issues.folio.org/browse/UIOR-562) Created By shows last clicked User name

## [2.0.4](https://github.com/folio-org/ui-orders/tree/v2.0.4) (2020-04-24)
[Full Changelog](https://github.com/folio-org/ui-orders/compare/v2.0.3...v2.0.4)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@folio/orders",
"version": "2.0.4",
"version": "2.0.5",
"description": "Description for orders",
"main": "src/index.js",
"repository": "",
Expand Down
81 changes: 44 additions & 37 deletions src/components/LayerCollection/LayerPO.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { get } from 'lodash';
import SafeHTMLMessage from '@folio/react-intl-safe-html';
import ReactRouterPropTypes from 'react-router-prop-types';
import { getFormValues } from 'redux-form';

import { LoadingPane } from '@folio/stripes/components';
import {
CalloutContext,
stripesConnect,
Expand Down Expand Up @@ -40,7 +40,11 @@ import { UpdateOrderErrorModal } from '../PurchaseOrder/UpdateOrderErrorModal';
class LayerPO extends Component {
static contextType = CalloutContext;
static manifest = Object.freeze({
order: ORDER,
order: {
...ORDER,
accumulate: true,
fetch: false,
},
addresses: ADDRESSES,
users: {
...USERS,
Expand Down Expand Up @@ -74,33 +78,41 @@ class LayerPO extends Component {
mutator: PropTypes.object.isRequired,
};

constructor(props) {
super(props);
constructor(props, context) {
super(props, context);
this.state = {
createdByName: '',
assignedToUser: '',
updateOrderError: null,
patchedOrder: {},
};
}

componentDidMount() {
this.setUserFields();
}

getOrder = () => this.props.resources?.order?.records[0];

setUserFields = () => {
const { mutator } = this.props;
const { assignedTo, metadata } = this.getOrder() || {};

if (metadata) {
getUserNameById(mutator.users, get(metadata, 'createdByUserId'))
.then(userName => this.setState({ createdByName: userName }));
}

if (assignedTo) {
getUserNameById(mutator.users, assignedTo)
.then(userName => this.setState({ assignedToUser: userName }));
const { match: { params: { id } }, mutator } = this.props;

if (id) {
this.props.mutator.order.GET()
.then(o => Promise.all([
o,
getUserNameById(mutator.users, o.metadata?.createdByUserId),
getUserNameById(mutator.users, o.assignedTo),
]), () => {
setTimeout(() => this.context?.sendCallout({
message: `Unable to load the order ${id}`,
type: 'error',
timeout: 0,
}));

return Promise.reject();
})
.then(([o, createdByName, assignedToUser]) => {
this.setState({
patchedOrder: {
...o,
createdByName,
assignedToUser,
},
});
});
}
}

Expand Down Expand Up @@ -138,9 +150,11 @@ class LayerPO extends Component {

return saveFn(order, mutator.order)
.then(({ id, poNumber }) => {
this.context.sendCallout({
message: <SafeHTMLMessage id="ui-orders.order.save.success" values={{ orderNumber: poNumber }} />,
});
if (this.context) {
this.context.sendCallout({
message: <SafeHTMLMessage id="ui-orders.order.save.success" values={{ orderNumber: poNumber }} />,
});
}
setTimeout(() => history.push({
pathname: `/orders/view/${id}`,
search: location.search,
Expand All @@ -159,17 +173,10 @@ class LayerPO extends Component {
stripes,
} = this.props;
const id = match.params.id;
const order = id
? this.getOrder()
: {};

if (!order) return null;
const { updateOrderError, createdByName, assignedToUser } = this.state;
const patchedOrder = {
...order,
createdByName,
assignedToUser,
};
const { updateOrderError, patchedOrder } = this.state;

if (id && patchedOrder.id !== id) return <LoadingPane dismissible defaultWidth="fill" onClose={this.goToOrders} />;

const formValues = getFormValues(PO_FORM_NAME)(stripes.store.getState());

return (
Expand Down
43 changes: 25 additions & 18 deletions src/components/LayerCollection/LayerPOLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class LayerPOLine extends Component {
query: {},
});

constructor(props) {
super(props);
constructor(props, context) {
super(props, context);

this.state = {
isLinesLimitExceededModalOpened: false,
Expand All @@ -115,6 +115,8 @@ class LayerPOLine extends Component {
});
};

sendCallout = (args) => this.context?.sendCallout(args);

handleErrorResponse = async (e, line) => {
let response;

Expand All @@ -130,13 +132,13 @@ class LayerPOLine extends Component {
} else {
const messageCode = get(ERROR_CODES, response.errors[0].code, 'orderLineGenericError');

this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id={`ui-orders.errors.${messageCode}`} />,
type: 'error',
});
}
} else {
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.errors.orderLineGenericError" />,
type: 'error',
});
Expand All @@ -149,19 +151,24 @@ class LayerPOLine extends Component {

delete newLine.template;

poLines.POST(newLine)
.then(() => this.openOrder(saveAndOpen))
return poLines.POST(newLine)
.then(
() => {
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.line.create.success" />,
type: 'success',
});

return this.openOrder(saveAndOpen);
},
e => this.handleErrorResponse(e, line),
)
.then(() => {
this.context.sendCallout({
message: <SafeHTMLMessage id="ui-orders.line.create.success" />,
type: 'success',
});
history.push({
setTimeout(() => history.push({
pathname: `/orders/view/${id}`,
search: location.search,
});
})
.catch(e => this.handleErrorResponse(e, line));
}), e => this.handleErrorResponse(e, line));
});
};

getOrder = () => get(this.props, 'resources.order.records.0');
Expand All @@ -186,7 +193,7 @@ class LayerPOLine extends Component {
layer: null,
});
} catch (e) {
this.context.sendCallout({
this.sendCallout({
message: <FormattedMessage id="ui-orders.errors.noCreatedOrder" />,
type: 'error',
});
Expand All @@ -202,13 +209,13 @@ class LayerPOLine extends Component {
return saveAndOpen
? updateOrderResource(order, mutator.order, { workflowStatus: WORKFLOW_STATUS.open })
.then(() => {
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.order.open.success" values={{ orderNumber: order.poNumber }} />,
type: 'success',
});
})
.catch(() => {
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.errors.openOrder" values={{ orderNumber: order.poNumber }} />,
type: 'error',
});
Expand All @@ -225,7 +232,7 @@ class LayerPOLine extends Component {
return mutator.poLines.PUT(line)
.then(() => this.openOrder(saveAndOpen))
.then(() => {
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.line.update.success" values={{ lineNumber: line.poLineNumber }} />,
type: 'success',
});
Expand Down
8 changes: 5 additions & 3 deletions src/components/POLine/POLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,23 @@ class POLine extends Component {
return lines.find(u => u.id === lineId);
}

sendCallout = (args) => this.context?.sendCallout(args);

deleteLine = () => {
const { mutator, poURL } = this.props;
const line = this.getLine();
const lineNumber = line.poLineNumber;

mutator.poLine.DELETE(line)
.then(() => {
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.line.delete.success" values={{ lineNumber }} />,
type: 'success',
});
mutator.query.update({ _path: poURL });
})
.catch(async errorResponse => {
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.errors.lineWasNotDeleted" />,
type: 'error',
});
Expand All @@ -125,7 +127,7 @@ class POLine extends Component {
} catch (e) {}

if (message) {
this.context.sendCallout({
this.sendCallout({
message,
timeout: 0,
type: 'error',
Expand Down
24 changes: 13 additions & 11 deletions src/components/PurchaseOrder/PO.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class PO extends Component {
resources: PropTypes.object.isRequired,
};

constructor(props) {
super(props);
constructor(props, context) {
super(props, context);
this.state = {
sections: {
purchaseOrder: true,
Expand All @@ -106,6 +106,8 @@ class PO extends Component {
this.setState(prevState => ({ isCloneConfirmation: !prevState.isCloneConfirmation }));
};

sendCallout = (args) => this.context?.sendCallout(args);

deletePO = () => {
const { mutator } = this.props;
const order = this.getOrder();
Expand All @@ -114,7 +116,7 @@ class PO extends Component {
this.unmountDeleteOrderConfirm();
mutator.order.DELETE(order)
.then(() => {
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.order.delete.success" values={{ orderNumber }} />,
type: 'success',
});
Expand All @@ -124,7 +126,7 @@ class PO extends Component {
});
})
.catch(() => {
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.errors.orderWasNotDeleted" />,
type: 'error',
});
Expand Down Expand Up @@ -173,8 +175,8 @@ class PO extends Component {
};

updateOrderResource(order, mutator.order, closeOrderProps)
.then(() => this.context.sendCallout({ message: <SafeHTMLMessage id="ui-orders.closeOrder.success" /> }))
.catch(() => this.context.sendCallout({
.then(() => this.context?.sendCallout({ message: <SafeHTMLMessage id="ui-orders.closeOrder.success" /> }))
.catch(() => this.context?.sendCallout({
message: <SafeHTMLMessage id="ui-orders.closeOrder.error" />,
type: 'error',
}))
Expand All @@ -188,7 +190,7 @@ class PO extends Component {

updateOrderResource(order, mutator.order, { approved: true })
.then(() => {
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.order.approved.success" values={{ orderNumber }} />,
});
})
Expand All @@ -206,7 +208,7 @@ class PO extends Component {

try {
await updateOrderResource(order, mutator.order, openOrderProps);
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.order.open.success" values={{ orderNumber: order.poNumber }} />,
type: 'success',
});
Expand All @@ -226,7 +228,7 @@ class PO extends Component {

try {
await updateOrderResource(order, mutator.order, openOrderProps);
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.order.reopen.success" values={{ orderNumber: order.poNumber }} />,
type: 'success',
});
Expand All @@ -243,7 +245,7 @@ class PO extends Component {
try {
const newOrder = await cloneOrder(order, mutator.order, order.compositePoLines);

this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.order.clone.success" />,
type: 'success',
});
Expand Down Expand Up @@ -373,7 +375,7 @@ class PO extends Component {
);

if (hasError && !this.hasError) {
this.context.sendCallout({
this.sendCallout({
message: <SafeHTMLMessage id="ui-orders.errors.orderNotLoaded" />,
type: 'error',
});
Expand Down
Loading

0 comments on commit 4225771

Please sign in to comment.