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

Action bar - order and payment [last subscription cycle] #163

Open
wants to merge 8 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
11 changes: 11 additions & 0 deletions vtex-my-subscriptions-3/messages/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@
"details-page.action-bar.label.changePayment": "Zahlungsfehler",
"details-page.action-bar.text.changePayment": "Bitte ändern Sie Ihre Zahlungsmethode",
"details-page.action-bar.button.changePayment": "Veränderung",
"details-page.action-bar.label.orderDispatched": "TODO",
"details-page.action-bar.text.orderDeliveryDate": "TODO",
"details-page.action-bar.text.orderDeliveryDetail": "TODO",
"details-page.action-bar.button.trackOrder": "TODO",
"details-page.action-bar.button.viewOrder": "TODO",
"details-page.action-bar.label.nextPurchase": "TODO",
"details-page.action-bar.text.nextPurchase": "TODO",
"details-page.action-bar.button.nextPurchase": "TODO",
"details-page.action-bar.label.printBankSlip": "TODO",
"details-page.action-bar.text.printBankSlip": "TODO",
"details-page.action-bar.button.printBankSlip": "TODO",
"details-page.preferences.title": "Einstellungen",
"display-address.label": "Lieferanschrift",
"display-address.deleted-error-message": "Ihre Adresse wurde entfernt, bitte wählen Sie eine andere aus.",
Expand Down
11 changes: 11 additions & 0 deletions vtex-my-subscriptions-3/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@
"details-page.action-bar.label.changePayment": "Payment Error",
"details-page.action-bar.text.changePayment": "Please, change your payment method",
"details-page.action-bar.button.changePayment": "Change",
"details-page.action-bar.label.orderDispatched": "Your order is already on the way",
"details-page.action-bar.text.orderDeliveryDate": "Arrives in until {numberOfDaysNextDelivery, plural, one {1 day} other {# days}}",
"details-page.action-bar.text.orderDeliveryDetail": "See details on the order page",
"details-page.action-bar.button.trackOrder": "Track order",
"details-page.action-bar.button.viewOrder": "View order",
"details-page.action-bar.label.nextPurchase": "Next purchase in {numberOfDaysNextPurchase, plural, one {1 day} other {# days}}",
"details-page.action-bar.text.nextPurchase": "{nextPurchaseDate}",
"details-page.action-bar.button.nextPurchase": "Change date",
"details-page.action-bar.label.printBankSlip": "Bank slip generated",
"details-page.action-bar.text.printBankSlip": "Awaiting payment confirmation",
"details-page.action-bar.button.printBankSlip": "Print bank slip",
"details-page.preferences.title": "Preferences",
"display-address.label": "Shipping address",
"display-address.deleted-error-message": "Your address was removed, please select another one.",
Expand Down
11 changes: 11 additions & 0 deletions vtex-my-subscriptions-3/messages/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@
"details-page.action-bar.label.changePayment": "Error en el pago",
"details-page.action-bar.text.changePayment": "Por favor, cambie su método de pago",
"details-page.action-bar.button.changePayment": "Cambio",
"details-page.action-bar.label.orderDispatched": "Tu pedido ya está en camino",
"details-page.action-bar.text.orderDeliveryDate": "Llega hasta {numberOfDaysNextDelivery, plural, one {1 día} other {# dias}}",
"details-page.action-bar.text.orderDeliveryDetail": "Ver detalles en la página de pedido",
"details-page.action-bar.button.trackOrder": "Rastrear pedido",
"details-page.action-bar.button.viewOrder": "Ver pedido",
"details-page.action-bar.label.nextPurchase": "Próxima compra en {numberOfDaysNextPurchase, plural, one {1 día} other {# dias}}",
"details-page.action-bar.text.nextPurchase": "{nextPurchaseDate}",
"details-page.action-bar.button.nextPurchase": "Cambiar fecha",
"details-page.action-bar.label.printBankSlip": "Recibo gerado",
"details-page.action-bar.text.printBankSlip": "Esperando la confirmación del pago",
"details-page.action-bar.button.printBankSlip": "Imprimir recibo",
"details-page.preferences.title": "Preferencias",
"display-address.label": "Dirección de Envío",
"display-address.deleted-error-message": "Su dirección fue eliminada, seleccione otra.",
Expand Down
11 changes: 11 additions & 0 deletions vtex-my-subscriptions-3/messages/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@
"details-page.action-bar.label.changePayment": "Erreur de paiement",
"details-page.action-bar.text.changePayment": "Veuillez changer votre mode de paiement",
"details-page.action-bar.button.changePayment": "Changer",
"details-page.action-bar.label.orderDispatched": "TODO",
"details-page.action-bar.text.orderDeliveryDate": "TODO",
"details-page.action-bar.text.orderDeliveryDetail": "TODO",
"details-page.action-bar.button.trackOrder": "TODO",
"details-page.action-bar.button.viewOrder": "TODO",
"details-page.action-bar.label.nextPurchase": "TODO",
"details-page.action-bar.text.nextPurchase": "TODO",
"details-page.action-bar.button.nextPurchase": "TODO",
"details-page.action-bar.label.printBankSlip": "TODO",
"details-page.action-bar.text.printBankSlip": "TODO",
"details-page.action-bar.button.printBankSlip": "TODO",
"details-page.preferences.title": "Préférences",
"display-address.label": "Adresse de livraison",
"display-address.deleted-error-message": "Votre adresse a été supprimée, veuillez en sélectionner une autre.",
Expand Down
11 changes: 11 additions & 0 deletions vtex-my-subscriptions-3/messages/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@
"details-page.action-bar.label.changePayment": "Errore di pagamento",
"details-page.action-bar.text.changePayment": "Per favore, cambia il tuo metodo di pagamento",
"details-page.action-bar.button.changePayment": "Modificare",
"details-page.action-bar.label.orderDispatched": "TODO",
"details-page.action-bar.text.orderDeliveryDate": "TODO",
"details-page.action-bar.text.orderDeliveryDetail": "TODO",
"details-page.action-bar.button.trackOrder": "TODO",
"details-page.action-bar.button.viewOrder": "TODO",
"details-page.action-bar.label.nextPurchase": "TODO",
"details-page.action-bar.text.nextPurchase": "TODO",
"details-page.action-bar.button.nextPurchase": "TODO",
"details-page.action-bar.label.printBankSlip": "TODO",
"details-page.action-bar.text.printBankSlip": "TODO",
"details-page.action-bar.button.printBankSlip": "TODO",
"details-page.preferences.title": "Preferenze",
"display-address.label": "Indirizzo di spedizione",
"display-address.deleted-error-message": "Il tuo indirizzo è stato rimosso, selezionane un altro.",
Expand Down
11 changes: 11 additions & 0 deletions vtex-my-subscriptions-3/messages/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@
"details-page.action-bar.label.changePayment": "Erro de pagamento",
"details-page.action-bar.text.changePayment": "Por favor, altere seu método de pagamento",
"details-page.action-bar.button.changePayment": "Alterar",
"details-page.action-bar.label.orderDispatched": "Seu pedido já está a caminho",
"details-page.action-bar.text.orderDeliveryDate": "Chega em até {numberOfDaysNextDelivery, plural, one {1 dia} other {# dias}}",
"details-page.action-bar.text.orderDeliveryDetail": "Veja os detalhes na página de pedidos",
"details-page.action-bar.button.trackOrder": "Rastrear pedido",
"details-page.action-bar.button.viewOrder": "Ver pedido",
"details-page.action-bar.label.nextPurchase": "Próxima compra em {numberOfDaysNextPurchase, plural, one {1 dia} other {# dias}}",
"details-page.action-bar.text.nextPurchase": "{nextPurchaseDate}",
"details-page.action-bar.button.nextPurchase": "Alterar data",
"details-page.action-bar.label.printBankSlip": "Boleto gerado",
"details-page.action-bar.text.printBankSlip": "Aguardando confirmação de pagamento",
"details-page.action-bar.button.printBankSlip": "Imprimir boleto",
"details-page.preferences.title": "Preferências",
"display-address.label": "Endereço de entrega",
"display-address.deleted-error-message": "Seu endereço foi removido, por favor selecione outro.",
Expand Down
11 changes: 11 additions & 0 deletions vtex-my-subscriptions-3/messages/ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@
"details-page.action-bar.label.changePayment": "Eroare de plata",
"details-page.action-bar.text.changePayment": "Vă rugăm, schimbați metoda de plată",
"details-page.action-bar.button.changePayment": "Schimbare",
"details-page.action-bar.label.orderDispatched": "TODO",
"details-page.action-bar.text.orderDeliveryDate": "TODO",
"details-page.action-bar.text.orderDeliveryDetail": "TODO",
"details-page.action-bar.button.trackOrder": "TODO",
"details-page.action-bar.button.viewOrder": "TODO",
"details-page.action-bar.label.nextPurchase": "TODO",
"details-page.action-bar.text.nextPurchase": "TODO",
"details-page.action-bar.button.nextPurchase": "TODO",
"details-page.action-bar.label.printBankSlip": "TODO",
"details-page.action-bar.text.printBankSlip": "TODO",
"details-page.action-bar.button.printBankSlip": "TODO",
"details-page.preferences.title": "Preferințe",
"display-address.label": "Adresa de transport",
"display-address.deleted-error-message": "Adresa dvs. a fost eliminată, vă rugăm să selectați alta.",
Expand Down
135 changes: 129 additions & 6 deletions vtex-my-subscriptions-3/react/components/DetailsPage/ActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,75 @@ defineMessages({
changePaymentButton: {
id: 'details-page.action-bar.button.changePayment',
},
orderDispatchedLabel: {
id: 'details-page.action-bar.label.orderDispatched',
},
orderDeliveryDateBody: {
id: 'details-page.action-bar.text.orderDeliveryDate',
},
orderDeliveryDetailBody: {
id: 'details-page.action-bar.text.orderDeliveryDetail',
},
trackOrderButton: {
id: 'details-page.action-bar.button.trackOrder',
},
viewOrderButton: {
id: 'details-page.action-bar.button.viewOrder',
},
nextPurchaseLabel: {
id: 'details-page.action-bar.label.nextPurchase',
},
nextPurchaseText: {
id: 'details-page.action-bar.text.nextPurchase',
},
nextPurchaseButton: {
id: 'details-page.action-bar.button.nextPurchase',
},
printBankSlipLabel: {
id: 'details-page.action-bar.label.printBankSlip',
},
printBankSlipText: {
id: 'details-page.action-bar.text.printBankSlip',
},
printBankSlipButton: {
id: 'details-page.action-bar.button.printBankSlip',
},
})

class ActionBarContainer extends Component<Props> {
private calculateDaysUntilSpecificDate = (specificDate: string) => {
const today = new Date()
const diffTime = Math.abs(+new Date(specificDate) - +today)
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))

return diffDays
}

private getOrderDispatchedAction = (
deliveryDate: string | undefined,
trackingUrl: string | undefined
rrbambokian marked this conversation as resolved.
Show resolved Hide resolved
) => {
const orderDispatchedBody = deliveryDate
? 'orderDeliveryDate'
: 'orderDeliveryDetail'
const orderDispatchedButton = trackingUrl ? 'trackOrder' : 'viewOrder'

return { body: orderDispatchedBody, button: orderDispatchedButton }
}

private getSuggestedAction = () => {
const { status, address, isSkipped, payment, onUpdateAction } = this.props
const {
status,
address,
isSkipped,
payment,
orderStatus,
orderDeliveryDate,
orderTrackingUrl,
nextPurchaseDate,
bankSlipUrl,
onUpdateAction,
} = this.props

let action: SubscriptionAction | null = null
let buttonVariation: 'primary' | 'secondary' = 'primary'
Expand All @@ -57,11 +121,18 @@ class ActionBarContainer extends Component<Props> {
action = 'restore'
} else if (!address) {
action = 'changeAddress'
} else if (!payment) {
} else if (!payment || orderStatus === 'payment-denied') {
action = 'changePayment'
} else if (isSkipped) {
action = 'unskip'
buttonVariation = 'secondary'
} else if (!!orderDeliveryDate || !!orderTrackingUrl) {
action = 'orderDispatched'
} else if (bankSlipUrl) {
action = 'printBankSlip'
} else if (nextPurchaseDate) {
action = 'nextPurchase'
buttonVariation = 'secondary'
}

if (action === 'changeAddress' || action === 'changePayment') {
Expand All @@ -71,7 +142,15 @@ class ActionBarContainer extends Component<Props> {
let onClick
if (
action &&
['restore', 'unskip', 'changeAddress', 'changePayment'].includes(action)
[
'restore',
'unskip',
'changeAddress',
'changePayment',
'orderDispatched',
'nextPurchase',
'printBankSlip',
].includes(action)
) {
onClick = () => onUpdateAction(action as SubscriptionAction)
}
Expand All @@ -97,12 +176,30 @@ class ActionBarContainer extends Component<Props> {
displayDanger ? 'c-danger fw5' : 'c-muted-1'
}`}
>
<FormattedMessage id={`details-page.action-bar.label.${action}`} />
<FormattedMessage
id={`details-page.action-bar.label.${action}`}
values={{
numberOfDaysNextPurchase:
this.props.nextPurchaseDate &&
this.calculateDaysUntilSpecificDate(
this.props.nextPurchaseDate
),
}}
/>
</div>
<div className="flex items-center flex-wrap justify-between">
<div className="t-heading-4 w-100 w-60-ns">
<FormattedMessage
id={`details-page.action-bar.text.${action}`}
id={
action === 'orderDispatched'
? `details-page.action-bar.text.${
this.getOrderDispatchedAction(
this.props.orderDeliveryDate,
this.props.orderTrackingUrl
).body
}`
: `details-page.action-bar.text.${action}`
}
values={{
day: (
<FormattedDate
Expand All @@ -111,13 +208,35 @@ class ActionBarContainer extends Component<Props> {
day="2-digit"
/>
),
nextPurchaseDate: (
<FormattedDate
value={this.props.nextPurchaseDate}
month="long"
day="2-digit"
weekday="long"
/>
),
numberOfDaysNextDelivery:
this.props.orderDeliveryDate &&
this.calculateDaysUntilSpecificDate(
this.props.orderDeliveryDate
),
}}
/>
</div>
<div className="mw5-ns w-100 mt4 w-40-ns mt0-ns pl0 pl6-ns">
<Button variation={buttonVariation} onClick={onClick} block>
<FormattedMessage
id={`details-page.action-bar.button.${action}`}
id={
action === 'orderDispatched'
? `details-page.action-bar.button.${
this.getOrderDispatchedAction(
this.props.orderDeliveryDate,
this.props.orderTrackingUrl
).button
}`
: `details-page.action-bar.button.${action}`
}
/>
</Button>
</div>
Expand All @@ -134,6 +253,10 @@ type Props = {
nextPurchaseDate: string
address: Subscription['shippingAddress']
payment: Subscription['purchaseSettings']['paymentMethod']
orderStatus: string | undefined
orderDeliveryDate: string | undefined
orderTrackingUrl: string | undefined
bankSlipUrl: string | undefined
Comment on lines +256 to +259
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe passing the full order to this guy is a better approach instead of multiple props.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

checking DetailsPage, the ActionBar is called like this:

<ActionBar
  status={subscription.status}
  isSkipped={subscription.isSkipped}
  address={subscription.shippingAddress}
  payment={subscription.purchaseSettings.paymentMethod}
  onUpdateAction={this.handleUpdateAction}
  nextPurchaseDate={subscription.nextPurchaseDate}
  orderStatus={subscription.lastExecution?.order?.status}
  orderDeliveryDate={
    subscription.lastExecution?.order?.status === 'invoiced'
      ? orderLogisticsInfo.length > 0 &&
        orderLogisticsInfo[0].shippingEstimateDate
      : undefined
  }
  orderTrackingUrl={
    subscription.lastExecution?.order?.status === 'invoiced'
      ? orderPackages.length > 0 && orderPackages[0].trackingUrl
      : undefined
  }
  bankSlipUrl={orderTransactions?.[0].payments?.[0]?.url}
/>

there are conditions in some parameters.. do you think it's better this way or passes the entire subscription object to handle it inside the action bar?

onUpdateAction: (action: SubscriptionAction) => void
}

Expand Down
48 changes: 47 additions & 1 deletion vtex-my-subscriptions-3/react/components/DetailsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,34 @@ class SubscriptionsDetailsContainer extends Component<Props, State> {
}

private handleUpdateAction = (action: SubscriptionAction) => {
const { history, subscription } = this.props

const orderPackages =
subscription?.lastExecution?.order?.packageAttachment.packages
const orderTrackingUrl =
orderPackages.length > 0 && orderPackages[0].trackingUrl

const orderTransactions =
subscription?.lastExecution?.order?.paymentData.transactions
const orderPayments =
orderTransactions.length > 0 && orderTransactions[0].payments
const bankSlipUrl = orderPayments.length > 0 && orderPayments[0].url

if (action === 'changeAddress' || action === 'changePayment') {
this.setState({ isEditMode: true })
goToElement({ id: PREFERENCES_ID })
} else if (action === 'orderDispatched' && !!orderTrackingUrl) {
window.open(orderTrackingUrl)
Comment on lines +115 to +116
Copy link
Contributor

Choose a reason for hiding this comment

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

Displaying an anchor tag is a better approach instead of calling through javascript and you can do it on the ActionBar component itself instead of doing it on the parent component

Copy link
Contributor Author

@rrbambokian rrbambokian Aug 17, 2021

Choose a reason for hiding this comment

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

hmm I get it.. but based on the generic structure defined in this handleUpdateAction method, that the effects are generated from the action param, isn't it preferable to keep the navigations that way?

} else if (
action === 'orderDispatched' &&
!orderTrackingUrl &&
subscription?.lastExecution?.order?.orderId
) {
history.push(`/orders/${subscription.lastExecution.order.orderId}`)
} else if (action === 'nextPurchase') {
this.handleChangeEdit(true)
Comment on lines +123 to +124
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this do?

} else if (action === 'printBankSlip' && !!bankSlipUrl) {
window.open(bankSlipUrl)
} else {
this.setState({ isModalOpen: true, actionType: action })
}
Expand Down Expand Up @@ -189,6 +214,13 @@ class SubscriptionsDetailsContainer extends Component<Props, State> {
errorMessage,
})

const orderPackages =
subscription.lastExecution?.order?.packageAttachment.packages
const orderLogisticsInfo =
subscription.lastExecution?.order?.shippingData.logisticsInfo
const orderTransactions =
subscription?.lastExecution?.order?.paymentData?.transactions

return (
<div id={DETAILS_ID}>
<History
Expand Down Expand Up @@ -219,6 +251,19 @@ class SubscriptionsDetailsContainer extends Component<Props, State> {
payment={subscription.purchaseSettings.paymentMethod}
onUpdateAction={this.handleUpdateAction}
nextPurchaseDate={subscription.nextPurchaseDate}
orderStatus={subscription.lastExecution?.order?.status}
orderDeliveryDate={
subscription.lastExecution?.order?.status === 'invoiced'
? orderLogisticsInfo.length > 0 &&
orderLogisticsInfo[0].shippingEstimateDate
: undefined
}
orderTrackingUrl={
subscription.lastExecution?.order?.status === 'invoiced'
? orderPackages.length > 0 && orderPackages[0].trackingUrl
: undefined
}
bankSlipUrl={orderTransactions?.[0].payments?.[0]?.url}
/>
<Products
subscriptionId={subscription.id}
Expand Down Expand Up @@ -275,7 +320,8 @@ type Props = {
updateStatus: (args: Variables<UpdateStatusArgs>) => Promise<MutationResult>
} & InjectedRuntimeContext &
WrappedComponentProps &
ChildProps
ChildProps &
RouteComponentProps

type InputProps = RouteComponentProps<{ subscriptionId: string }>

Expand Down
Loading