Skip to content

Commit

Permalink
feat: #56 (*) fix transition to order
Browse files Browse the repository at this point in the history
  • Loading branch information
Baroshem committed Aug 24, 2021
1 parent 495c3cd commit 09fc67e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
13 changes: 10 additions & 3 deletions packages/api-client/src/api/updateAddressDetails/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import setOrderBillingAddressMutation from './setOrderBillingAddressMutation';
import setOrderShippingAddressMutation from './setOrderShippingAddressMutation';
import { CustomQuery } from '@vue-storefront/core';
import gql from 'graphql-tag';
import { Context, UpdateAddressDetailsParams, UpdateAddressDetailsResponse } from '../../types';
import { BILLING_TYPE } from '../../helpers/constants';
import { Context, Order, UpdateAddressDetailsParams, UpdateAddressDetailsResponse } from '../../types';
import { ARRANGING_PAYMENT_STATE, BILLING_TYPE } from '../../helpers';
import { isCustomerDataFilled } from '../../helpers';
import transitionOrderToState from '../transitionOrderToState';

const updateAddressDetails = async (context: Context, params: UpdateAddressDetailsParams, customQuery?: CustomQuery): Promise<UpdateAddressDetailsResponse> => {
const { type, input } = params;
const updateAddressDetailsVariables = { input };
const isBilling = type === BILLING_TYPE;

const updateAddressDetailsQuery = type === BILLING_TYPE ? setOrderBillingAddressMutation : setOrderShippingAddressMutation;
const updateAddressDetailsQuery = isBilling ? setOrderBillingAddressMutation : setOrderShippingAddressMutation;

const { updateAddressDetails } = context.extendQuery(
customQuery, { updateAddressDetails: { query: updateAddressDetailsQuery, variables: updateAddressDetailsVariables } }
Expand All @@ -21,6 +24,10 @@ const updateAddressDetails = async (context: Context, params: UpdateAddressDetai
fetchPolicy: 'no-cache'
}) as UpdateAddressDetailsResponse;

if (isBilling && isCustomerDataFilled(request?.data?.setOrderBillingAddress as Order)) {
await transitionOrderToState(context, { state: ARRANGING_PAYMENT_STATE })
}

return request;
};

Expand Down
5 changes: 5 additions & 0 deletions packages/api-client/src/fragments/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const CartFragment = `
code
state
active
customer {
firstName
lastName
emailAddress
}
lines {
id
featuredAsset {
Expand Down
6 changes: 6 additions & 0 deletions packages/api-client/src/helpers/checkout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Order } from "../types";

export const isCustomerDataFilled = (order: Order): boolean => {
if (!order) return;
return Boolean(order?.customer?.emailAddress && order?.billingAddress?.streetLine1 && order?.shippingAddress?.streetLine1);
}
1 change: 1 addition & 0 deletions packages/api-client/src/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const VENDURE_AUTH_TOKEN_NAME = 'vendure-auth-token';
export const BILLING_TYPE = 'billing';
export const ARRANGING_PAYMENT_STATE = 'ArrangingPayment';
2 changes: 2 additions & 0 deletions packages/api-client/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './constants';
export * from './checkout';
8 changes: 1 addition & 7 deletions packages/composables/src/composables/useBilling/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ const params: UseBillingParams<OrderAddress, AddParams> = {
// OrderAddress has one property optional which is required in CreateAddressInput.
const response = await context.$vendure.api.updateAddressDetails({ input: billingDetails as CreateAddressInput, type: BILLING_TYPE }, customQuery);

const newOrder = (response?.data?.setOrderBillingAddress as Order)?.billingAddress

if (newOrder) {
await context.$vendure.api.transitionOrderToState({ state: 'ArrangingPayment' });
}

return newOrder;
return (response?.data?.setOrderBillingAddress as Order)?.billingAddress;
}
};

Expand Down
15 changes: 2 additions & 13 deletions packages/theme/pages/Checkout/Customer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
slim
>
<SfInput
v-e2e="'shipping-firstName'"
v-e2e="'customer-firstName'"
v-model="form.firstName"
label="First name"
name="firstName"
Expand All @@ -32,7 +32,7 @@
slim
>
<SfInput
v-e2e="'shipping-lastName'"
v-e2e="'customer-lastName'"
v-model="form.lastName"
label="Last name"
name="lastName"
Expand Down Expand Up @@ -73,17 +73,6 @@
</SfButton>
</div>
</div>
<!-- <div class="form__action">
<SfButton
v-e2e="'continue-to-shipping'"
class="form__action-button"
type="button"
@click=""
:disabled="!(form.firstName && form.lastName && form.emailAddress)"
>
{{ $t('Continue to shipping') }}
</SfButton>
</div> -->
</form>
</ValidationObserver>
</template>
Expand Down

0 comments on commit 09fc67e

Please sign in to comment.