Skip to content

Commit

Permalink
Merge pull request #265 from Pinelab-studio/feat/shipping-method-orde…
Browse files Browse the repository at this point in the history
…r-client

Eligible Shipping Methods Store
  • Loading branch information
martijnvdbrug authored Oct 25, 2023
2 parents c6f7cbf + d7db883 commit 7fb2e89
Show file tree
Hide file tree
Showing 8 changed files with 519 additions and 418 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
"@types/sharp": "0.28.0",
"@types/tmp": "0.2.3",
"@typescript-eslint/eslint-plugin": "5.43.0",
"@vendure/admin-ui-plugin": "2.0.6",
"@vendure/asset-server-plugin": "2.0.6",
"@vendure/core": "2.0.6",
"@vendure/email-plugin": "2.0.6",
"@vendure/testing": "2.0.6",
"@vendure/ui-devkit": "2.0.6",
"@vendure/admin-ui-plugin": "2.0.9",
"@vendure/asset-server-plugin": "2.0.9",
"@vendure/core": "2.0.9",
"@vendure/email-plugin": "2.0.9",
"@vendure/testing": "2.0.9",
"@vendure/ui-devkit": "2.0.9",
"aws-sdk": "2.1099.0",
"copyfiles": "2.4.1",
"eslint": "8.0.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/vendure-order-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.1.0 (2023-09-20)

- Added loading states for currentUser and activeOrder store ([#256](https://github.com/Pinelab-studio/pinelab-vendure-plugins/pull/256))

# 2.2.0 (2023-10-03)

- Added Eligible Shipping Method store with value, loading state and error([#265](https://github.com/Pinelab-studio/pinelab-vendure-plugins/pull/265))
2 changes: 1 addition & 1 deletion packages/vendure-order-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pinelab/vendure-order-client",
"version": "2.1.0",
"version": "2.2.0",
"description": "A tiny, framework agnostic client for managing active orders and checkout with Vendure.",
"author": "Martijn van de Brug <[email protected]>",
"homepage": "https://pinelab-plugins.com/",
Expand Down
15 changes: 15 additions & 0 deletions packages/vendure-order-client/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,19 @@ export class GraphqlQueries {
}
${this.CURRENT_USER_FIELDS}
`;

GET_ELIGIBLE_SHIPPING_METHODS = gql`
query GetEligibleShippingMethods {
eligibleShippingMethods {
id
name
price
priceWithTax
code
description
metadata
customFields
}
}
`;
}
4 changes: 2 additions & 2 deletions packages/vendure-order-client/src/store-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MapStore } from 'nanostores';
import { ErrorCode, ErrorResult } from './graphql-generated-types';
import { ErrorResult } from './graphql-generated-types';

/**
* Interface defining loading and error states per store
Expand Down Expand Up @@ -57,7 +57,7 @@ export function HandleLoadingState(storeName: string) {
return result;
} catch (e: any) {
store.setKey('error', {
errorCode: ErrorCode.UnknownError,
errorCode: e?.errorCode,
message: e?.message,
});
} finally {
Expand Down
28 changes: 28 additions & 0 deletions packages/vendure-order-client/src/vendure-order-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
SetOrderShippingMethodMutation,
Success,
TransitionOrderToStateMutation,
ShippingMethodQuote,
} from './graphql-generated-types';
import { GraphqlQueries } from './queries';
import { setResult, HandleLoadingState, StateStore } from './store-helpers';
Expand All @@ -57,6 +58,7 @@ const dummyFragment = gql`
`;

export type ActiveOrder<T> = ActiveOrderFieldsFragment & T;
export type EligibleShippingMethod<T> = ShippingMethodQuote & T;
export type CurrentUser = CurrentUserFieldsFragment;

/**
Expand All @@ -81,6 +83,14 @@ export class VendureOrderClient<A = unknown> {
data: undefined,
});

$eligibleShippingMethods = map<
StateStore<Array<EligibleShippingMethod<A>> | undefined>
>({
loading: false,
error: undefined,
data: undefined,
});

/**
* The store object that holds the current logged in user
*/
Expand Down Expand Up @@ -137,6 +147,7 @@ export class VendureOrderClient<A = unknown> {
productVariantIds: [productVariantId],
quantity,
});
void this.updateEligibleShippingMehods();
return activeOrder;
}

Expand Down Expand Up @@ -179,10 +190,12 @@ export class VendureOrderClient<A = unknown> {
quantity: -adjustment, // adjustment is negative, so invert it
});
}
void this.updateEligibleShippingMehods();
return activeOrder;
}

async removeOrderLine(orderLineId: Id): Promise<ActiveOrder<A>> {
void this.updateEligibleShippingMehods();
return await this.adjustOrderLine(orderLineId, 0);
}

Expand All @@ -205,6 +218,7 @@ export class VendureOrderClient<A = unknown> {
productVariantIds: allVariantIds,
quantity: totalQuantity,
});
void this.updateEligibleShippingMehods();
return activeOrder;
}

Expand All @@ -221,6 +235,7 @@ export class VendureOrderClient<A = unknown> {
this.eventBus.emit('coupon-code-applied', {
couponCode,
});
void this.updateEligibleShippingMehods();
return activeOrder;
}

Expand All @@ -242,6 +257,7 @@ export class VendureOrderClient<A = unknown> {
this.eventBus.emit('coupon-code-removed', {
couponCode,
});
void this.updateEligibleShippingMehods();
return activeOrder;
}

Expand All @@ -257,6 +273,7 @@ export class VendureOrderClient<A = unknown> {
setCustomerForOrder as ActiveOrder<A>
);
setResult(this.$activeOrder, activeOrder);
void this.updateEligibleShippingMehods();
return activeOrder;
}

Expand All @@ -272,6 +289,7 @@ export class VendureOrderClient<A = unknown> {
setOrderShippingAddress as ActiveOrder<A>
);
setResult(this.$activeOrder, activeOrder);
void this.updateEligibleShippingMehods();
return activeOrder;
}

Expand All @@ -285,6 +303,7 @@ export class VendureOrderClient<A = unknown> {
setOrderBillingAddress as ActiveOrder<A>
);
setResult(this.$activeOrder, activeOrder);
void this.updateEligibleShippingMehods();
return activeOrder;
}

Expand All @@ -300,6 +319,7 @@ export class VendureOrderClient<A = unknown> {
setOrderShippingMethod as ActiveOrder<A>
);
setResult(this.$activeOrder, activeOrder);
void this.updateEligibleShippingMehods();
return activeOrder;
}

Expand Down Expand Up @@ -437,4 +457,12 @@ export class VendureOrderClient<A = unknown> {
throw e;
}
}

@HandleLoadingState('$eligibleShippingMethods')
private async updateEligibleShippingMehods(): Promise<void> {
const { eligibleShippingMethods } = await this.rawRequest<{
eligibleShippingMethods: ShippingMethodQuote[];
}>(this.queries.GET_ELIGIBLE_SHIPPING_METHODS);
setResult(this.$eligibleShippingMethods, eligibleShippingMethods);
}
}
Loading

0 comments on commit 7fb2e89

Please sign in to comment.