Skip to content

Commit

Permalink
Merge pull request #195 from vietredweb/Issue-194
Browse files Browse the repository at this point in the history
fixes #194 Save & Close save data when save fail
  • Loading branch information
lunguyenhat authored Feb 3, 2023
2 parents 9c14d2c + 0c8a207 commit 16b12e4
Show file tree
Hide file tree
Showing 22 changed files with 295 additions and 265 deletions.
355 changes: 233 additions & 122 deletions src/components/Fields/index.jsx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import React, { Component } from 'react';
import { Col, Nav, Row, Tab } from 'react-bootstrap';
import { withTranslation } from 'react-i18next';
// import ProductAsset from './ProductAsset';
import FieldStore from 'containers/FieldsPage/FieldStore/FieldStore';
import FieldViewModel from 'containers/FieldsPage/FieldViewModel/FieldViewModel';
import Spinner from 'components/Spinner';
import PAGE_STATUS from 'constants/PageStatus';
import { observer } from 'mobx-react';
import { FieldViewModelContextProvider } from 'containers/FieldsPage/FieldViewModel/FieldViewModelContextProvider';
import FieldsList from 'components/Fields';
Expand All @@ -24,19 +21,8 @@ const CategoryTab = observer(
};
}

async componentDidMount() {
this.fieldListViewModel.handleFilter({
'filter[type_id]': 65,
'filter[published]': 1,
});
this.fieldListViewModel.handleFilterList({ limit: 0 });
await this.fieldListViewModel.initializeDataCustom();
this.forceUpdate();
}

componentDidUpdate(prevProps) {
if (this.props.requiredField !== prevProps.requiredField) {
console.log('testneeeeeee');
this.handleActiveTabRequiredField();
}
}
Expand All @@ -53,9 +39,6 @@ const CategoryTab = observer(
const { t, validator } = this.props;
return (
<div className="p-24 bg-white rounded-1 shadow-sm h-100 mt-24">
{this.fieldListViewModel.formStatus === PAGE_STATUS.LOADING && (
<Spinner className="spinner-overlay" />
)}
<Tab.Container
id="left-tabs-fields"
activeKey={`${this.state.defaultActive}`}
Expand Down Expand Up @@ -88,13 +71,14 @@ const CategoryTab = observer(
<CategoryInformation validator={validator} />
</Tab.Pane>
<Tab.Pane eventKey="customFields">
<div className="row" key={this.fieldListViewModel?.items.length}>
<div className="row">
<FieldsList
detailViewModal={this.detailViewModal}
formPropsData={this.detailViewModal.categoryDetailViewModel.formPropsData}
validator={validator}
fieldClass={'col-lg-12'}
requiredField={this.props.requiredField}
typeId={65}
/>
</div>
</Tab.Pane>
Expand Down
10 changes: 5 additions & 5 deletions src/containers/CategoriesPage/CategoryEdit/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ const EditCategory = observer(
handle: async () => {
this.handleAliasFormPropsData();
if (this.validator.allValid()) {
if (this.isEdit) {
await this.categoryDetailViewModel.update();
} else {
await this.categoryDetailViewModel.create();
const result = this.isEdit
? await this.categoryDetailViewModel.update()
: await this.categoryDetailViewModel.create();
if (result !== 0) {
history.push(`/categories`);
}
history.push(`/categories`);
} else {
this.handleValidateForm();
}
Expand Down
2 changes: 2 additions & 0 deletions src/containers/CategoriesPage/CategoryStore/CategoryStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ export default class CategoryStore {
callbackOnError(resultOnSave);
});
}
return resultOnSave?.result;
} catch (error) {
runInAction(() => {
callbackOnError(error?.response?.data);
});
return 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CategoryDetailViewModel {

update = async () => {
this.formStatus = PAGE_STATUS.LOADING;
await this.categoryStore.updateCategory(
return await this.categoryStore.updateCategory(
this.categoryDetailViewModel.formPropsData,
this.callbackOnSuccessHandler,
this.callbackOnErrorHandler
Expand Down
10 changes: 5 additions & 5 deletions src/containers/DebtorGroupPage/DebtorGroupEdit/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ const EditDebtorGroup = observer(
title: t('txt_save_close'),
handle: async () => {
if (this.validator.allValid()) {
if (this.isEdit) {
await this.debtorGroupDetailViewModel.update();
} else {
await this.debtorGroupDetailViewModel.create();
const result = this.isEdit
? await this.debtorGroupDetailViewModel.update()
: await this.debtorGroupDetailViewModel.create();
if (result !== 0) {
history.push(`/debtor-group`);
}
history.push(`/debtor-group`);
} else {
this.validator.showMessages();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ export default class DebtorGroupStore {
callbackOnError(resultOnSave);
});
}
return resultOnSave?.result;
} catch (error) {
runInAction(() => {
callbackOnError(error?.response?.data);
});
return 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DebtorGroupDetailViewModel {

update = async () => {
this.formStatus = PAGE_STATUS.LOADING;
await this.debtorGroupStore.update(
return await this.debtorGroupStore.update(
this.debtorGroupDetailViewModel.formPropsData,
this.callbackOnSuccessHandler,
this.callbackOnErrorHandler
Expand Down
10 changes: 5 additions & 5 deletions src/containers/FieldsGroupPage/FieldGroupEdit/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ const EditFieldGroup = observer(
handle: async () => {
this.handleAliasFormPropsData();
if (this.validator.allValid()) {
if (this.isEdit) {
await this.fieldGroupDetailViewModel.update();
} else {
await this.fieldGroupDetailViewModel.create();
const result = this.isEdit
? await this.fieldGroupDetailViewModel.update()
: await this.fieldGroupDetailViewModel.create();
if (result !== 0) {
history.push(`/fields-group`);
}
history.push(`/fields-group`);
} else {
this.validator.showMessages();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ export default class FieldGroupStore {
callbackOnError(resultOnSave);
});
}
return resultOnSave?.result;
} catch (error) {
runInAction(() => {
callbackOnError(error?.response?.data);
});
return 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FieldGroupDetailViewModel {

update = async () => {
this.formStatus = PAGE_STATUS.LOADING;
await this.fieldGroupStore.update(
return await this.fieldGroupStore.update(
this.fieldGroupDetailViewModel.formPropsData,
this.callbackOnSuccessHandler,
this.callbackOnErrorHandler
Expand Down
10 changes: 5 additions & 5 deletions src/containers/FieldsPage/FieldEdit/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ const EditField = observer(
title: t('txt_save_close'),
handle: async () => {
if (this.validator.allValid()) {
if (this.isEdit) {
await this.fieldDetailViewModel.update();
} else {
await this.fieldDetailViewModel.create();
const result = this.isEdit
? await this.fieldDetailViewModel.update()
: await this.fieldDetailViewModel.create();
if (result !== 0) {
history.push(`/fields`);
}
history.push(`/fields`);
} else {
this.validator.showMessages();
}
Expand Down
1 change: 1 addition & 0 deletions src/containers/FieldsPage/FieldStore/FieldStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class FieldStore {
runInAction(() => {
callbackOnError(error?.response?.data);
});
return 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FieldDetailViewModel {

update = async () => {
this.formStatus = PAGE_STATUS.LOADING;
await this.fieldStore.update(
return await this.fieldStore.update(
this.fieldDetailViewModel.formPropsData,
this.callbackOnSuccessHandler,
this.callbackOnErrorHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ class FieldListViewModel {
this.callbackOnSuccessHandler,
this.callbackOnErrorHandler
);

this.successResponse.state = true;
runInAction(() => {
this.successResponse.state = true;
});
};

initializeDataCustom = async () => {
Expand Down
10 changes: 5 additions & 5 deletions src/containers/ProductPricesPage/ProductPriceEdit/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ const EditProductPrice = observer(
title: t('txt_save_close'),
handle: async () => {
if (this.validator.allValid()) {
if (this.isEdit) {
await this.productPriceDetailViewModel.update();
} else {
await this.productPriceDetailViewModel.create();
const result = this.isEdit
? await this.productPriceDetailViewModel.update()
: await this.productPriceDetailViewModel.create();
if (result !== 0) {
history.push(`/prices`);
}
history.push(`/prices`);
} else {
this.validator.showMessages();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ export default class ProductPriceStore {
callbackOnError(resultOnSave);
});
}
return resultOnSave?.result;
} catch (error) {
runInAction(() => {
callbackOnError(error?.response?.data);
});
return 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ProductPriceDetailViewModel {

update = async () => {
this.formStatus = PAGE_STATUS.LOADING;
await this.productPriceStore.update(
return await this.productPriceStore.update(
this.productPriceDetailViewModel.formPropsData,
this.callbackOnSuccessHandler,
this.callbackOnErrorHandler
Expand Down
97 changes: 10 additions & 87 deletions src/containers/ProductsPage/ProductEdit/Component/Fields/index.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import React, { Component } from 'react';
import { Col, Nav, Row, Tab } from 'react-bootstrap';
import { withTranslation } from 'react-i18next';
import FieldStore from 'containers/FieldsPage/FieldStore/FieldStore';
import FieldViewModel from 'containers/FieldsPage/FieldViewModel/FieldViewModel';
import Spinner from 'components/Spinner';
import PAGE_STATUS from 'constants/PageStatus';
import { observer } from 'mobx-react';
import { FieldViewModelContextProvider } from 'containers/FieldsPage/FieldViewModel/FieldViewModelContextProvider';

import FieldsList from 'components/Fields';
import { PIM_FIELD_DETAIL_FIELD_KEY } from 'aesirx-dma-lib';

const fieldStore = new FieldStore();
const fieldViewModel = new FieldViewModel(fieldStore);
Expand All @@ -24,93 +20,20 @@ const FieldsTab = observer(
};
}

async componentDidMount() {
this.fieldListViewModel.handleFilter({ 'filter[type_id]': 59, 'filter[published]': 1 });
this.fieldListViewModel.handleFilterList({ limit: 0 });
await this.fieldListViewModel.initializeDataCustom();
await this.fieldListViewModel.getGroupList();
this.setState({
defaultActive: 'group-' + this.fieldListViewModel.groupList[0]?.id,
});
}

componentDidUpdate(prevProps) {
if (this.props.requiredField !== prevProps.requiredField) {
this.handleActiveTabRequiredField();
}
}

handleActiveTabRequiredField() {
if (this.props.requiredField) {
let requiredFields = Object.keys(this.props.validator.fields).find(
(key) => this.props.validator.fields[key] === false
);
let groupRequired = this.fieldListViewModel.items.find(
(o) => o[PIM_FIELD_DETAIL_FIELD_KEY.NAME] === requiredFields
)?.field_group_id;
if (this.state.defaultActive !== 'group-' + groupRequired) {
this.setState({
defaultActive: 'group-' + groupRequired,
});
}
}
}

render() {
const { t, detailViewModal, formPropsData, validator } = this.props;
const { detailViewModal, formPropsData, validator } = this.props;
return (
<div className="p-24 bg-white rounded-1 shadow-sm h-100 mt-24">
{this.fieldListViewModel.formStatus === PAGE_STATUS.LOADING && (
<Spinner className="spinner-overlay" />
)}
<FieldViewModelContextProvider viewModel={fieldViewModel}>
<Tab.Container
id="left-tabs-fields"
activeKey={`${this.state.defaultActive}`}
onSelect={(key) => {
this.setState({
defaultActive: key,
});
}}
>
<Row className="gx-24">
<Col lg={3}>
<div className="fs-14 pb-16 mb-1 border-bottom fw-semibold">
{t('txt_field_group')}
</div>
<Nav variant="tabs" className="flex-column">
{this.fieldListViewModel.groupList?.map((group, key) => {
return (
<Nav.Item key={key}>
<Nav.Link eventKey={`group-${group.id}`}>{group.label}</Nav.Link>
</Nav.Item>
);
})}
</Nav>
</Col>
<Col lg={9}>
<Tab.Content>
{this.fieldListViewModel.groupList?.map((group, key) => {
return (
<Tab.Pane eventKey={`group-${group.id}`} key={key}>
<h3 className="mb-24 fw-bold">{group.label}</h3>
<div className="row">
<FieldsList
detailViewModal={detailViewModal}
formPropsData={formPropsData}
validator={validator}
groupID={group.id}
fieldClass={'col-lg-6'}
requiredField={this.props.requiredField}
/>
</div>
</Tab.Pane>
);
})}
</Tab.Content>
</Col>
</Row>
</Tab.Container>
<FieldsList
detailViewModal={detailViewModal}
formPropsData={formPropsData}
validator={validator}
fieldClass={'col-lg-6'}
requiredField={this.props.requiredField}
typeId={59}
fieldByGroup={true}
/>
</FieldViewModelContextProvider>
</div>
);
Expand Down
Loading

0 comments on commit 16b12e4

Please sign in to comment.