Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout to batch modal update response #197

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/vtex-my-subscriptions-3/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"store": "0.x"
},
"mustUpdateAt": "2019-04-02",
"registries": ["smartcheckout"],
"registries": [
"smartcheckout"
],
"scripts": {
"postreleasy": "vtex publish --verbose"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const AddItemModal: FunctionComponent<Props> = ({
<div key={sku.skuId} className={`${handles.productItemWrapper} mb8`}>
<Item
id={sku.skuId}
name={sku.name}
name={product.productName === sku.name ? sku.name : `${product.productName}\n${sku.name}`}
price={sku.price}
currency={currency}
imageUrl={sku.imageUrl}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare module '*/search.gql' {
plans: string[]
}
export interface SearchProduct {
productName: string
unitMultiplier: number
measurementUnit: string
brand: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ query SearchSubscribableProducts(
) @context(provider: "vtex.subscriptions-graphql") {
list {
... on SearchProduct {
productName
unitMultiplier
measurementUnit
brand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,30 @@ class BatchModal extends Component<Props, State> {
}

promises?.then(() => {
const displayError = this.state.completed.length !== selectedIds.length

if (!displayError) {
showToast({ message: intl.formatMessage(messages.success) })
onClose()
} else {
this.setState((finalState) => {
finalState.completed.map((id) => delete finalState.selectionItems[id])

return {
selectionItems: finalState.selectionItems,
loading: false,
// If some subscription isn't on the finalState
// it means that some error has ocurred
displayError,
}
})
}
// timeout added due to state not update instantaneously
setTimeout(() => {
Copy link
Contributor

@wagnerlduarte wagnerlduarte Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you used setTimeout as a workaround to ensure that the state is updated.
Although it works, it may be better to transform the handleSuccess function into a Promise, to make this state update guarantee better.
Can you test whether removing setTimeout and changing handleSuccess to a Promise works well?

  private handleSuccess = (id: string) => {
    return new Promise<void>((resolve) => {
      this.setState(({ completed }) => {
        completed.push(id);
        return { completed };
      }, resolve);
    });
  }

// filter only unique completed ids
const uniqueCompletedIds = this.state.completed.filter((completed, index) => this.state.completed.indexOf(completed) === index);

const displayError = uniqueCompletedIds.length !== selectedIds.length

if (!displayError) {
showToast({ message: intl.formatMessage(messages.success) })
onClose()
} else {
this.setState((finalState) => {
finalState.completed.map((id) => delete finalState.selectionItems[id])

return {
selectionItems: finalState.selectionItems,
loading: false,
// If some subscription isn't on the finalState
// it means that some error has ocurred
displayError,
}
})
}
}, 100)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const ProductsListing: FunctionComponent<Props> = ({
>
<ProductListItem
isEditing={isEditing}
name={product.sku.productName}
name={product.sku.productName === product.sku.name ? product.sku.name : `${product.sku.productName}\n${product.sku.name}`}
quantity={product.quantity}
imageUrl={product.sku.imageUrl ?? undefined}
measurementUnit={product.sku.measurementUnit}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const SkuThumbnail: FunctionComponent<Props> = ({
<div className="w-100 flex flex-column flex-row-ns justify-between pl4">
<div className="w-40-ns w-100 mb0-ns mb2 pr4">
<div className="c-muted-1 fw5 ttu f7 mb2">{brandName}</div>
<div className="mb4 fw5">{name}</div>
<div className="mb4 fw5">{name.split('\n').map( (it, i) => <div key={'x'+i}>{it}</div> )}</div>
<div className="t-small c-muted-1">
{`${unitMultiplier} ${measurementUnit}`}
</div>
Expand Down