Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,16 @@ export interface RecentOrg {
address: string;
}

export interface PaymentsInfo {
methods: string[];
default: string;
}

export interface OrderInfo {
payerType: string;
payerData: CustomerData;
deliveryType: string;
deliveryData: Record<string, string>;
paymentType: string;
basketData: Record<string, number>;
countries: Record<number, string>;
cdekCountries: number[];
Expand Down
20 changes: 4 additions & 16 deletions components/order/Payment.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
<script setup lang="ts">
const props = defineProps<{ payerType: string; country: number }>();
import type { PaymentsInfo } from '~/common/types';

const props = defineProps<{ paymentsInfo: PaymentsInfo | null }>();

Check warning on line 4 in components/order/Payment.vue

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

components/order/Payment.vue#L4

'any' overrides all other types in this union type.
const paymentType = defineModel<string>('paymentType');

const { t } = useI18n();

const params = computed(() => ({ payerType: props.payerType, country: props.country }));
const { data: paymentMethods } = await useApi<string[]>(
'/order/payments/',
params,
{ watch: [() => props.payerType, () => props.country] },
);

const paymentTypes = computed(() =>
(paymentMethods.value!).map(method => ({
(props.paymentsInfo?.methods ?? []).map(method => ({

Check warning on line 10 in components/order/Payment.vue

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

components/order/Payment.vue#L10

Unsafe call of an `error` type typed value.

Check warning on line 10 in components/order/Payment.vue

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

components/order/Payment.vue#L10

Unsafe member access .methods on an `error` typed value.

Check warning on line 10 in components/order/Payment.vue

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

components/order/Payment.vue#L10

Unsafe return of an error typed value.
id: method,
title: t(method),
comment: t(`${method}Comment`),
}))
);

watch(paymentMethods, (methods) => {
if (!methods) return;
if (!methods.includes(paymentType.value ?? '')) {
paymentType.value = methods[0] ?? '';
}
}, { immediate: true });
</script>

<template>
Expand Down
26 changes: 22 additions & 4 deletions pages/contents/order.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts" setup>
import Loader from '~/components/Loader.vue';
import Button from '~/components/Button.vue';
import type {OrderInfo} from "~/common/types";
import type {OrderInfo, PaymentsInfo} from "~/common/types";

const { t, locale } = useI18n();

Expand Down Expand Up @@ -48,7 +48,26 @@ const deliveryData = ref(orderInfo.value!.deliveryData);
const deliveryType = ref(orderInfo.value!.deliveryType);
const country = ref(Number(orderInfo.value!.deliveryData.country));

const paymentType = ref(orderInfo.value!.paymentType);
const paymentsParams = computed(() => ({ payerType: payerType.value, country: country.value }));
// Fetch here rather than in Payment.vue: paymentType must be set before the first render, otherwise SSR hydration loses the selection.
const { data: paymentsInfo } = await useApi<PaymentsInfo>(
'/order/payments/',
paymentsParams,
{ watch: [payerType, country] },
);

const paymentType = ref(paymentsInfo.value?.default ?? '');

const payerTypeChanged = ref(false);
watch(payerType, () => { payerTypeChanged.value = true; });

watch(paymentsInfo, (info) => {
if (!info) return;
if (payerTypeChanged.value || !paymentType.value || !info.methods.includes(paymentType.value)) {
payerTypeChanged.value = false;
paymentType.value = info.default;
}
});


useHead({
Expand Down Expand Up @@ -137,8 +156,7 @@ const makeOrder = async () => {

<OrderPayment
v-model:paymentType="paymentType"
:payerType="payerType"
:country="country"
:paymentsInfo="paymentsInfo"
/>

<div v-if="orderError && !Object.keys(fieldErrors).length" class="order-error">
Expand Down
Loading