Skip to content

Commit

Permalink
feat(composables): useDefaultOrderAssociations for accessing associat…
Browse files Browse the repository at this point in the history
…ions (#1194)
  • Loading branch information
patzick authored Aug 5, 2024
1 parent 6070039 commit aa8f5a4
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-teachers-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/composables-next": minor
---

New `useDefaultOrderAssociations` composable to be used or overriden separately in user project. This composable just returns default associations object.
1 change: 1 addition & 0 deletions packages/composables/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export * from "./useContext";
export * from "./useCountries";
export * from "./useCustomerOrders";
export * from "./useCustomerPassword";
export * from "./useDefaultOrderAssociations";
export * from "./useInternationalization";
export * from "./useLandingSearch";
export * from "./useListing";
Expand Down
42 changes: 42 additions & 0 deletions packages/composables/src/useDefaultOrderAssociations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";
import { useSetup } from "./_test";
import { useDefaultOrderAssociations } from "./useDefaultOrderAssociations";

describe("useDefaultOrderAssociations", () => {
it("returns default associations object", async () => {
const { vm } = useSetup(() => useDefaultOrderAssociations());

expect(vm).toMatchInlineSnapshot(`
{
"associations": {
"addresses": {},
"deliveries": {
"associations": {
"shippingMethod": {},
"shippingOrderAddress": {},
"stateMachineState": {},
},
},
"lineItems": {
"associations": {
"cover": {},
"downloads": {
"associations": {
"media": {},
},
},
},
},
"stateMachineState": {},
"transactions": {
"associations": {
"paymentMethod": {},
"stateMachineState": {},
},
},
},
"checkPromotion": true,
}
`);
});
});
45 changes: 45 additions & 0 deletions packages/composables/src/useDefaultOrderAssociations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Schemas } from "#shopware";

export type UseDefaultOrderAssociationsReturn = Schemas["Criteria"] & {
checkPromotion?: boolean;
};

/**
* Returns default order associations. You can override this composable in your project.
* @public
*/
export function useDefaultOrderAssociations(): UseDefaultOrderAssociationsReturn {
const orderAssociations: Schemas["Criteria"] & { checkPromotion?: boolean } =
{
associations: {
stateMachineState: {},
lineItems: {
associations: {
cover: {},
downloads: {
associations: {
media: {},
},
},
},
},
addresses: {},
deliveries: {
associations: {
shippingMethod: {},
shippingOrderAddress: {},
stateMachineState: {},
},
},
transactions: {
associations: {
paymentMethod: {},
stateMachineState: {},
},
},
},
checkPromotion: true,
};

return orderAssociations;
}
38 changes: 3 additions & 35 deletions packages/composables/src/useOrderDetails.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
import { computed, ref, inject, provide } from "vue";
import type { ComputedRef, Ref } from "vue";
import { defu } from "defu";
import { useShopwareContext } from "#imports";
import { useDefaultOrderAssociations, useShopwareContext } from "#imports";
import type { Schemas } from "#shopware";

/**
* Data for api requests to fetch all necessary data
*/
const orderAssociations: Schemas["Criteria"] & { checkPromotion?: boolean } = {
associations: {
stateMachineState: {},
lineItems: {
associations: {
cover: {},
downloads: {
associations: {
media: {},
},
},
},
},
addresses: {},
deliveries: {
associations: {
shippingMethod: {},
shippingOrderAddress: {},
stateMachineState: {},
},
},
transactions: {
associations: {
paymentMethod: {},
stateMachineState: {},
},
},
},
checkPromotion: true,
};

export type UseOrderDetailsReturn = {
/**
* {@link Schemas['Order']} object
Expand Down Expand Up @@ -171,6 +137,8 @@ export function useOrderDetails(
);
provide("swOrderDetails", _sharedOrder);

const orderAssociations = useDefaultOrderAssociations();

const paymentMethod = computed(
() => _sharedOrder.value?.transactions?.[0]?.paymentMethod,
);
Expand Down

0 comments on commit aa8f5a4

Please sign in to comment.