Skip to content

Commit

Permalink
Leve/trello/1066 weight size product (#809)
Browse files Browse the repository at this point in the history
* intermediate

* intermediate

* add weight and size functionality in product form

* intermediate

* metrics with delivery
  • Loading branch information
levenecav authored and isaldin committed Dec 14, 2018
1 parent decd0c9 commit fa3416c
Show file tree
Hide file tree
Showing 20 changed files with 762 additions and 88 deletions.
4 changes: 4 additions & 0 deletions src/components/Modal/Modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
justify-content: center;
align-items: center;
margin: 15rem 5rem;

@media (max-width: #{$sm - 1px}) {
margin: 10rem 2rem;
}
}

.body {
Expand Down
18 changes: 12 additions & 6 deletions src/components/common/Input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PropsType = {
search: boolean,
align?: 'center' | 'left' | 'right',
disabled?: boolean,
limitHidden?: boolean,
};

type StateType = {
Expand Down Expand Up @@ -77,7 +78,11 @@ class Input extends Component<PropsType, StateType> {
input: ?HTMLInputElement;

handleChange = (e: SyntheticInputEvent<HTMLInputElement>) => {
const { onChange } = this.props;
const { onChange, limit } = this.props;
const { value } = e.target;
if (limit != null && value.length > limit) {
return;
}
onChange(e);
};

Expand All @@ -101,7 +106,6 @@ class Input extends Component<PropsType, StateType> {

renderInput() {
const {
onChange,
inputRef,
isAutocomplete,
id,
Expand All @@ -115,9 +119,9 @@ class Input extends Component<PropsType, StateType> {
<input
type="text"
ref={inputRef}
value={this.props.value || ''}
value={value || ''}
disabled={disabled || false}
onChange={onChange}
onChange={this.handleChange}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onKeyDown={this.props.onKeyDown}
Expand All @@ -133,7 +137,7 @@ class Input extends Component<PropsType, StateType> {
type={!isNil(type) ? type : 'text'}
value={value || ''}
disabled={disabled || false}
onChange={onChange}
onChange={this.handleChange}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onKeyDown={this.props.onKeyDown}
Expand All @@ -156,6 +160,7 @@ class Input extends Component<PropsType, StateType> {
postfix,
inline,
search,
limitHidden,
} = this.props;
const { labelFloat, isFocus } = this.state;
return (
Expand Down Expand Up @@ -204,7 +209,8 @@ class Input extends Component<PropsType, StateType> {
)}
{isFocus &&
!isUrl &&
!isNil(limit) && (
!isNil(limit) &&
limitHidden !== true && (
<div
styleName={classNames('valueLength', {
maxValueLength: value && value.length === limit,
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/Input/Input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@

.valueLength {
position: absolute;
bottom: -3.25rem;
bottom: -3rem;
right: 0;
color: $color_grey_minor;
font-size: 12px;
Expand Down
110 changes: 110 additions & 0 deletions src/components/common/InputNumber/InputNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// @flow strict

import React, { Component } from 'react';
import { omit } from 'ramda';

import { Input } from 'components/common/Input';

type StateType = {
value: string,
};

type PropsType = {
onChange: (value: number) => void,
onFocus?: () => void,
onBlur?: () => void,
value: number,
};

class InputNumber extends Component<PropsType, StateType> {
static getDerivedStateFromProps(nextProps: PropsType, prevState: StateType) {
const value = `${nextProps.value}`;
if (Number(value) !== Number(prevState.value)) {
return { ...prevState, value };
}
return null;
}

constructor(props: PropsType) {
super(props);
this.state = {
value: props.value ? `${props.value}` : '0',
};
}

handleOnChange = (e: SyntheticInputEvent<HTMLInputElement>) => {
const {
target: { value },
} = e;
const { onChange } = this.props;
const regexp = /(^[0-9]*[.,]?[0-9]*$)/;
if (regexp.test(value)) {
this.setState({
value:
value
.replace(/^0+/, '0')
.replace(/^[.,]/, '0.')
.replace(/^0([0-9])/, '$1')
.replace(/,/, '.') || '0',
});
onChange(
Number(
value
.replace(/[.,]$/, '')
.replace(/^0([0-9])/, '$1')
.replace(/(^0\.[0-9])0+$/, '$1'),
),
);
return;
}
if (value === '') {
this.setState({ value: '0' }, () => {
onChange(0);
});
}
};

handleOnFocus = () => {
const { onFocus } = this.props;
if (onFocus) {
onFocus();
}
};

handleOnBlur = () => {
const value = `${this.state.value}`;
if (Number(value) === 0) {
this.setState({
value: '0',
});
} else {
this.setState({
value: value
.replace(/\.$/, '')
.replace(/^0([0-9])/, '$1')
.replace(/\.0+$/, '')
.replace(/(^0\.[0-9])0+$/, '$1'),
});
}
const { onBlur } = this.props;
if (onBlur) {
onBlur();
}
};

render() {
const { value } = this.state;
const props = omit(['value', 'onChange', 'onFocus', 'onBlur'], this.props);
return (
<Input
{...props}
onChange={this.handleOnChange}
onFocus={this.handleOnFocus}
onBlur={this.handleOnBlur}
value={value}
/>
);
}
}

export default InputNumber;
5 changes: 3 additions & 2 deletions src/components/common/InputPrice/InputPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ class InputPrice extends Component<PropsType, StateType> {
return;
}
if (value === '') {
this.setState({ price: '0' });
onChangePrice(0);
this.setState({ price: '0' }, () => {
onChangePrice(0);
});
}
};

Expand Down
1 change: 1 addition & 0 deletions src/components/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export { default as Input } from './Input/Input';
export { default as Textarea } from './Textarea/Textarea';
export { default as InputPrice } from './InputPrice/InputPrice';
export { default as InputNumber } from './InputNumber/InputNumber';
export { default as InputSlug } from './InputSlug/InputSlug';
export { default as Select } from './Select/Select';
export { default as Button } from './Button/Button';
Expand Down
84 changes: 62 additions & 22 deletions src/pages/Manage/Store/Products/Product/EditProduct/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,34 @@ class EditProduct extends Component<PropsType, StateType> {
formErrors: {},
isLoading: false,
availablePackages: null,
isLoadingPackages: true,
isLoadingPackages: false,
isLoadingShipping: false,
shippingData: null,
customAttributes: newCustomAttributes,
};
}

componentDidMount() {
this.handleFetchPackages();
}

setLoadingPackages = (value: boolean) => {
this.setState({ isLoadingPackages: value });
};

handleFetchPackages = (metrics?: {
lengthCm: number,
widthCm: number,
heightCm: number,
weightG: number,
}) => {
this.setState({ isLoadingPackages: true });
const size = metrics
? metrics.lengthCm * metrics.widthCm * metrics.heightCm
: 0;
const weight = metrics ? metrics.weightG : 0;
// $FlowIgnore
const baseProduct = pathOr(null, ['me', 'baseProduct'], this.props);
// $FlowIgnore
const warehouses = pathOr(
null,
Expand All @@ -109,8 +129,8 @@ class EditProduct extends Component<PropsType, StateType> {
this.setLoadingPackages(true);
const variables = {
countryCode,
size: 0,
weight: 0,
size: metrics ? size : baseProduct.volumeCubicCm || 0,
weight: metrics ? weight : baseProduct.weightG || 0,
};

fetchPackages(this.props.environment, variables)
Expand All @@ -135,17 +155,16 @@ class EditProduct extends Component<PropsType, StateType> {
} else {
this.handlerOffLoadingPackages();
}
}

setLoadingPackages = (value: boolean) => {
this.setState({ isLoadingPackages: value });
};

handlerOffLoadingPackages = () => {
this.setState({ isLoadingPackages: false });
};

handleSave = (form: FormType & { currency: string }) => {
handleSave = (
form: FormType & { currency: string },
withSavingShipping?: boolean,
) => {
this.setState({ formErrors: {} });
const baseProduct = path(['me', 'baseProduct'], this.props);
if (!baseProduct || !baseProduct.id) {
Expand All @@ -164,6 +183,7 @@ class EditProduct extends Component<PropsType, StateType> {
shortDescription,
longDescription,
currency,
metrics,
} = form;
this.setState(() => ({ isLoading: true }));
UpdateBaseProductMutation.commit({
Expand All @@ -179,6 +199,10 @@ class EditProduct extends Component<PropsType, StateType> {
? [{ lang: 'EN', text: seoDescription }]
: null,
currency,
lengthCm: metrics.lengthCm,
widthCm: metrics.widthCm,
heightCm: metrics.heightCm,
weightG: metrics.weightG,
environment: this.props.environment,
onCompleted: (response: ?Object, errors: ?Array<any>) => {
this.setState({ isLoading: false });
Expand Down Expand Up @@ -213,19 +237,22 @@ class EditProduct extends Component<PropsType, StateType> {
}

if (form && form.rawIdMainVariant) {
this.handleUpdateVariant({
idMainVariant: form.idMainVariant,
rawIdMainVariant: form.rawIdMainVariant,
photoMain: form.photoMain,
photos: form.photos,
vendorCode: form.vendorCode,
price: form.price,
cashback: form.cashback,
discount: form.discount,
preOrderDays: form.preOrderDays,
preOrder: form.preOrder,
attributeValues: form.attributeValues,
});
this.handleUpdateVariant(
{
idMainVariant: form.idMainVariant,
rawIdMainVariant: form.rawIdMainVariant,
photoMain: form.photoMain,
photos: form.photos,
vendorCode: form.vendorCode,
price: form.price,
cashback: form.cashback,
discount: form.discount,
preOrderDays: form.preOrderDays,
preOrder: form.preOrder,
attributeValues: form.attributeValues,
},
withSavingShipping,
);
}
},
onError: (error: Error) => {
Expand All @@ -248,7 +275,10 @@ class EditProduct extends Component<PropsType, StateType> {
});
};

handleUpdateVariant = (variantData: VariantType) => {
handleUpdateVariant = (
variantData: VariantType,
withSavingShipping?: boolean,
) => {
if (!variantData.idMainVariant) {
this.props.showAlert({
type: 'danger',
Expand Down Expand Up @@ -353,6 +383,10 @@ class EditProduct extends Component<PropsType, StateType> {
text: t.productUpdated,
link: { text: '' },
});

if (withSavingShipping) {
this.handleSaveShipping();
}
},
onError: (error: Error) => {
this.setState(() => ({ isLoading: false }));
Expand Down Expand Up @@ -549,6 +583,7 @@ class EditProduct extends Component<PropsType, StateType> {
router={router}
match={match}
isLoadingShipping={isLoadingShipping}
onFetchPackages={this.handleFetchPackages}
/>
</div>
)}
Expand Down Expand Up @@ -701,6 +736,11 @@ export default createFragmentContainer(
lang
text
}
lengthCm
widthCm
heightCm
weightG
volumeCubicCm
}
}
`,
Expand Down
Loading

0 comments on commit fa3416c

Please sign in to comment.