Skip to content

Commit

Permalink
feat(composables): set default addresses (SWF-184)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanilowicz authored and patzick committed Nov 15, 2022
1 parent 77891f0 commit 16ee1d5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .changeset/good-lizards-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"vue-demo-store": minor
"@shopware-pwa/composables-next": minor
---

set default user addresses
11 changes: 10 additions & 1 deletion packages/composables/src/useUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export type UseUserReturn = {
isGuestSession: ComputedRef<boolean>;
country: Ref<Country | null>;
salutation: Ref<Salutation | null>;
defaultBillingAddressId: ComputedRef<string | null>;
defaultShippingAddressId: ComputedRef<string | null>;
refreshUser: () => Promise<void>;
logout: () => Promise<void>;
loadCountry: (countryId: string) => Promise<void>;
Expand Down Expand Up @@ -128,7 +130,12 @@ export function useUser(): UseUserReturn {
): Promise<void> {
await setDefaultCustomerPaymentMethod(paymentMethodId);
}

const defaultBillingAddressId = computed(
() => user.value?.defaultBillingAddressId || null
);
const defaultShippingAddressId = computed(
() => user.value?.defaultShippingAddressId || null
);
const isLoggedIn = computed(() => !!user.value?.id && !!user.value.active);
const isCustomerSession = computed(
() => !!user.value?.id && !user.value.guest
Expand All @@ -152,5 +159,7 @@ export function useUser(): UseUserReturn {
loadCountry,
country,
setUser,
defaultBillingAddressId,
defaultShippingAddressId,
};
}
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 22 additions & 9 deletions templates/vue-demo-store/components/account/AccountAddressCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { SharedModal } from "~~/components/shared/SharedModal.vue";
const { pushSuccess, pushError } = useNotifications();
const { setDefaultCustomerShippingAddress, setDefaultCustomerBillingAddress } =
useAddress();
const { defaultBillingAddressId, defaultShippingAddressId } = useUser();
const { refreshSessionContext } = useSessionContext();
const modal = inject<SharedModal>("modal") as SharedModal;
const props = withDefaults(
Expand All @@ -20,15 +21,23 @@ const props = withDefaults(
);
const setDefaultShippingAddress = async () => {
(await setDefaultCustomerShippingAddress(props.address.id))
? pushSuccess("Set default shipping address successfully")
: pushError("Set default shipping address error");
try {
await setDefaultCustomerShippingAddress(props.address.id);
refreshSessionContext();
pushSuccess("Set default shipping address successfully");
} catch (error) {
pushError("Set default shipping address error");
}
};
const setDefaultBillingAddress = async () => {
(await setDefaultCustomerBillingAddress(props.address.id))
? pushSuccess("Set default billing address successfully")
: pushError("Set default billing address error");
try {
await setDefaultCustomerBillingAddress(props.address.id);
refreshSessionContext();
pushSuccess("Set default billing address successfully");
} catch (error) {
pushError("Set default billing address error");
}
};
</script>

Expand Down Expand Up @@ -59,14 +68,18 @@ const setDefaultBillingAddress = async () => {
</div>
<div v-if="canSetDefault">
<a
href="#"
v-if="defaultShippingAddressId !== address.id"
role="button"
tabindex="0"
class="block text-sm mt-4 font-medium text-blue-600 hover:underline"
@click="setDefaultShippingAddress()"
>
Set as default shipping address
</a>
<a
href="#"
v-if="defaultBillingAddressId !== address.id"
role="button"
tabindex="0"
class="block text-sm mt-2 font-medium text-blue-600 hover:underline"
@click="setDefaultBillingAddress()"
>
Expand Down

0 comments on commit 16ee1d5

Please sign in to comment.