Skip to content

Commit

Permalink
fix: refresh addresses explicitly in a component (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucmus authored Jul 10, 2023
1 parent 5521638 commit 8c6ff0a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-hairs-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vue-demo-store": patch
---

Refresh addresses on address save/edit
5 changes: 5 additions & 0 deletions .changeset/long-houses-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/composables-next": patch
---

Remove implicit refresh action on address edit
8 changes: 7 additions & 1 deletion apps/docs/src/packages/composables/useAddress.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ loadCustomerAddresses();

```vue{4}
<script setup lang="ts">
const { createCustomerAddress } = useAddress();
const { createCustomerAddress, updateCustomerAddress } = useAddress();
const newAddress: CustomerAddress = await createCustomerAddress({
city: "Berlin",
Expand All @@ -39,6 +39,9 @@ const newAddress: CustomerAddress = await createCustomerAddress({
street: "Karl-Marx-Allee 1",
zipcode: "10178",
});
// refresh store with customer addresses (optionally if you want to see updated list of addresses)
updateCustomerAddress();
</script>
```

Expand All @@ -53,6 +56,9 @@ const updatedAddress = await updateCustomerAddress(
{ firstName: "Johnny" } // change the firstName value
)
);
// refresh store with customer addresses (optionally if you want to see updated list of addresses)
updateCustomerAddress();
```

### Delete an address
Expand Down
4 changes: 0 additions & 4 deletions packages/composables/src/useAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export type UseAddressReturn = {
*/
export function useAddress(): UseAddressReturn {
const { apiInstance } = useShopwareContext();
const { isLoggedIn, isGuestSession } = useUser();

const _storeCustomerAddresses: Ref<CustomerAddress[]> = inject(
"swCustomerAddresses",
Expand Down Expand Up @@ -100,7 +99,6 @@ export function useAddress(): UseAddressReturn {
customerAddress: Omit<CustomerAddress, "id" | "salutation">
): Promise<CustomerAddress> {
const result = await apiCreateCustomerAddress(customerAddress, apiInstance);
await loadCustomerAddresses();
return result;
}

Expand All @@ -111,7 +109,6 @@ export function useAddress(): UseAddressReturn {
customerAddress: CustomerAddress
): Promise<CustomerAddress> {
const result = await apiUpdateCustomerAddress(customerAddress, apiInstance);
await loadCustomerAddresses();
return result;
}

Expand All @@ -120,7 +117,6 @@ export function useAddress(): UseAddressReturn {
*/
async function deleteCustomerAddress(addressId: string): Promise<void> {
const result = apiDeleteCustomerAddress(addressId, apiInstance);
await loadCustomerAddresses();
return result;
}

Expand Down
14 changes: 2 additions & 12 deletions packages/types/shopware-6-client/errors/ApiError.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { AxiosResponse, AxiosError } from "axios";

import { Error } from "../response/Error";
/**
* API error structure for incoming errors
*
* @public
*/
export type ShopwareError = {
status: string;
code: string;
title: string;
detail: string;
source?: {
pointer: string;
};
meta: unknown;
};

export type ShopwareError = Error;
/**
* API Error response from Shopware backend
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
setDefaultCustomerShippingAddress,
setDefaultCustomerBillingAddress,
deleteCustomerAddress,
loadCustomerAddresses,
} = useAddress();
const { defaultBillingAddressId, defaultShippingAddressId } = useUser();
const { refreshSessionContext } = useSessionContext();
Expand Down Expand Up @@ -58,6 +59,8 @@ const removeAddress = async (addressId: string) => {
pushSuccess(t("account.messages.addressDeletedSuccess"));
} catch (error) {
pushError(t("account.messages.addressDeletedError"));
} finally {
loadCustomerAddresses();
}
};
Expand Down
20 changes: 11 additions & 9 deletions templates/vue-demo-store/components/account/AccountAddressForm.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script setup lang="ts">
import {
ClientApiError,
CustomerAddress,
ShopwareError,
} from "@shopware-pwa/types";
import { ClientApiError, CustomerAddress } from "@shopware-pwa/types";
const { createCustomerAddress, updateCustomerAddress, errorMessageBuilder } =
useAddress();
const {
createCustomerAddress,
updateCustomerAddress,
errorMessageBuilder,
loadCustomerAddresses,
} = useAddress();
const emits = defineEmits<{
(e: "success"): void;
Expand Down Expand Up @@ -46,9 +46,11 @@ const invokeSave = async (): Promise<void> => {
? updateCustomerAddress
: createCustomerAddress;
await saveAddress(formData);
loadCustomerAddresses();
emits("success");
} catch (errors) {
(errors as ClientApiError).messages.forEach((element: ShopwareError) => {
} catch (e) {
const errors = e as ClientApiError;
errors?.messages?.forEach((element) => {
pushError(errorMessageBuilder(element) || t("messages.error"));
});
}
Expand Down

2 comments on commit 8c6ff0a

@vercel
Copy link

@vercel vercel bot commented on 8c6ff0a Jul 10, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

frontends-demo – ./templates/vue-demo-store

frontends-demo-shopware-frontends.vercel.app
frontends-demo.vercel.app
frontends-demo-git-main-shopware-frontends.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 8c6ff0a Jul 10, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.