Skip to content

Commit

Permalink
Merge branch 'main' into fix/GH-279
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucmus authored Jun 26, 2023
2 parents 199496a + 55db3a6 commit 5ff23a6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-spiders-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/composables-next": minor
---

Add errorMessageBuilder to the useAddress composable
5 changes: 5 additions & 0 deletions .changeset/purple-dodos-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vue-demo-store": minor
---

Add error notification for add new address form
23 changes: 23 additions & 0 deletions packages/composables/src/useAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ClientApiError,
CustomerAddress,
ShopwareSearchParams,
Error,
} from "@shopware-pwa/types";
import { useUser } from "./useUser";

Expand Down Expand Up @@ -50,6 +51,12 @@ export type UseAddressReturn = {
* Sets the address for given ID as default shipping address
*/
setDefaultCustomerShippingAddress(addressId: string): Promise<string>;
/**
* Returns formatted error message
*
* @param {Error} error
*/
errorMessageBuilder(error: Error): string | null;
};

/**
Expand Down Expand Up @@ -135,6 +142,21 @@ export function useAddress(): UseAddressReturn {
return await apiSetDefaultCustomerShippingAddress(addressId, apiInstance);
}

/**
* Returns formatted error message
*
* @param {error} error
* @returns {string | null}
*/
function errorMessageBuilder(error: Error): string | null {
switch (error.code) {
case "VIOLATION::IS_BLANK_ERROR":
return `${error?.source?.pointer.slice(1)} - ${error.detail}`;
default:
return null;
}
}

return {
customerAddresses: computed(() => _storeCustomerAddresses.value || []),
loadCustomerAddresses,
Expand All @@ -143,5 +165,6 @@ export function useAddress(): UseAddressReturn {
deleteCustomerAddress,
setDefaultCustomerBillingAddress,
setDefaultCustomerShippingAddress,
errorMessageBuilder,
};
}
3 changes: 3 additions & 0 deletions packages/types/shopware-6-client/response/Error.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type Error = {
detail: string;
meta: unknown;
trace?: ErrorTrace[];
source?: {
pointer: string;
};
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script setup lang="ts">
import { CustomerAddress } from "@shopware-pwa/types";
import { CustomerAddress, Error } from "@shopware-pwa/types";
const { createCustomerAddress, updateCustomerAddress } = useAddress();
const { createCustomerAddress, updateCustomerAddress, errorMessageBuilder } =
useAddress();
const emits = defineEmits<{
(e: "success"): void;
Expand All @@ -21,6 +22,8 @@ const props = withDefaults(
const { getCountries } = useCountries();
const { getSalutations } = useSalutations();
const { t } = useI18n();
const { pushError } = useNotifications();
const formData = reactive<CustomerAddress>({
countryId: props.address?.countryId ?? "",
Expand All @@ -40,8 +43,10 @@ const invokeSave = async (): Promise<void> => {
: createCustomerAddress;
await saveAddress(formData);
emits("success");
} catch (error) {
console.error("error save address", error);
} catch (errors: Error[]) {
errors.messages.forEach((element: Error) => {
pushError(errorMessageBuilder(element) || t("messages.error"));
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion templates/vue-demo-store/pages/account/address.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ onBeforeMount(async () => {
{{ $t("account.addressAddNew") }}
</button>
<SharedModal :controller="addAddressModalController">
<AccountAddressForm />
<AccountAddressForm @success="addAddressModalController.close" />
</SharedModal>
</div>
</template>

0 comments on commit 5ff23a6

Please sign in to comment.