Passed
Push — 6.4 ( a2ab34...70d76f )
by Christian
13:23 queued 14s
created

index.ts ➔ salesChannelId   A

Complexity

Conditions 5

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 2
dl 0
loc 2
rs 9.3333
c 0
b 0
f 0
1
import type { PropType } from 'vue';
2
import type CriteriaType from 'src/core/data/criteria.data';
3
4
import template from './sw-order-create-options.html.twig';
5
import './sw-order-create-options.scss';
6
7
import type {
8
    ContextSwitchParameters,
9
    Customer,
10
    Currency,
11
    Cart,
12
    CartDelivery,
13
} from '../../order.types';
14
15
const { Component, State } = Shopware;
16
const { Criteria } = Shopware.Data;
17
18
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
19
Component.register('sw-order-create-options', {
20
    template,
21
22
    props: {
23
        promotionCodes: {
24
            type: Array as PropType<string[]>,
25
            required: true,
26
        },
27
28
        disabledAutoPromotion: {
29
            type: Boolean,
30
            required: true,
31
        },
32
33
        context: {
34
            type: Object as PropType<ContextSwitchParameters>,
35
            required: true,
36
        },
37
    },
38
39
    data(): {
40
        shippingCost: number,
41
        promotionCodeTags: string[],
42
        isSameAsBillingAddress: boolean,
43
        } {
44
        return {
45
            shippingCost: 0,
46
            isSameAsBillingAddress: false,
47
            promotionCodeTags: [],
48
        };
49
    },
50
51
    computed: {
52
        salesChannelId(): string {
53
            return State.get('swOrder').context?.salesChannel?.id ?? '';
54
        },
55
56
        salesChannelCriteria(): CriteriaType {
57
            const criteria = new Criteria();
58
59
            if (this.salesChannelId) {
60
                criteria.addFilter(Criteria.equals('salesChannels.id', this.salesChannelId));
61
            }
62
63
            return criteria;
64
        },
65
66
        shippingMethodCriteria(): CriteriaType {
67
            const criteria = new Criteria();
68
            criteria.addFilter(Criteria.equals('active', 1));
69
70
            if (this.salesChannelId) {
71
                criteria.addFilter(Criteria.equals('salesChannels.id', this.salesChannelId));
72
            }
73
74
            return criteria;
75
        },
76
77
        paymentMethodCriteria(): CriteriaType {
78
            const criteria = new Criteria();
79
            criteria.addFilter(Criteria.equals('active', 1));
80
            criteria.addFilter(Criteria.equals('afterOrderEnabled', 1));
81
82
            if (this.salesChannelId) {
83
                criteria.addFilter(Criteria.equals('salesChannels.id', this.salesChannelId));
84
            }
85
86
            return criteria;
87
        },
88
89
        customer(): Customer | null {
90
            return State.get('swOrder').customer;
91
        },
92
93
        currency(): Currency {
94
            return State.get('swOrder').context.currency;
95
        },
96
97
        cart(): Cart {
98
            return State.get('swOrder').cart;
99
        },
100
101
        cartDelivery(): CartDelivery | null {
102
            return this.cart?.deliveries[0] as CartDelivery | null;
103
        },
104
    },
105
106
    watch: {
107
        cartDelivery: {
108
            immediate: true,
109
            handler(value): void {
110
                // eslint-disable-next-line max-len
111
                // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
112
                this.shippingCost = value?.shippingCosts?.totalPrice ?? 0;
113
            },
114
        },
115
116
        'context.currencyId': {
117
            async handler(): Promise<void> {
118
                // await this.getCurrency();
119
                await this.updateCartContext();
120
            },
121
        },
122
123
        'context.shippingAddressId': {
124
            handler(): void {
125
                this.updateSameAsBillingAddressToggle();
126
            },
127
        },
128
129
        'context.billingAddressId': {
130
            handler(): void {
131
                this.updateSameAsBillingAddressToggle();
132
            },
133
        },
134
135
        'context.shippingMethodId': {
136
            async handler(): Promise<void> {
137
                await this.updateCartContext();
138
            },
139
        },
140
141
        isSameAsBillingAddress(value): void {
142
            if (!value) {
143
                return;
144
            }
145
146
            this.context.shippingAddressId = this.context.billingAddressId;
147
        },
148
    },
149
150
    created() {
151
        this.createdComponent();
152
    },
153
154
    methods: {
155
        updateSameAsBillingAddressToggle(): void {
156
            this.isSameAsBillingAddress = this.context.shippingAddressId === this.context.billingAddressId;
157
        },
158
159
        createdComponent(): void {
160
            this.promotionCodeTags = [...this.promotionCodes];
161
            this.isSameAsBillingAddress = this.context.shippingAddressId === this.context.billingAddressId;
162
        },
163
164
        validatePromotions(searchTerm: string): boolean {
165
            const promotionCode = searchTerm.trim();
166
167
            if (promotionCode.length <= 0) {
168
                return false;
169
            }
170
171
            const isExist = this.promotionCodes.find((code: string) => code === promotionCode);
172
            return !isExist;
173
        },
174
175
        onToggleAutoPromotion(value: boolean): void {
176
            this.$emit('auto-promotion-toggle', value);
177
        },
178
179
        changePromotionCodes(value: string[]): void {
180
            this.$emit('promotions-change', value);
181
        },
182
183
        async updateCartContext(): Promise<void> {
184
            if (!this.salesChannelId) {
185
                return;
186
            }
187
188
            await this.updateOrderContext();
189
            await this.loadCart();
190
        },
191
192
        updateOrderContext(): Promise<void> {
193
            // eslint-disable-next-line @typescript-eslint/no-unsafe-return
194
            return State.dispatch('swOrder/updateOrderContext', {
195
                context: this.context,
196
                salesChannelId: this.salesChannelId,
197
                contextToken: this.cart.token,
198
            });
199
        },
200
201
        loadCart(): Promise<void> {
202
            // eslint-disable-next-line @typescript-eslint/no-unsafe-return
203
            return State.dispatch('swOrder/getCart', {
204
                salesChannelId: this.salesChannelId,
205
                contextToken: this.cart.token,
206
            });
207
        },
208
209
        onChangeShippingCost(value: number): void {
210
            this.$emit('shipping-cost-change', value);
211
        },
212
    },
213
});
214