Passed
Push — trunk ( 61b9d3...670c99 )
by Christian
12:39 queued 11s
created

index.ts ➔ showError   B

Complexity

Conditions 6

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
dl 0
loc 10
rs 8.6666
c 0
b 0
f 0
1
import type Repository from 'src/core/data/repository.data';
2
import type { Cart, Customer, PaymentMethod, PromotionCodeTag } from '../../order.types';
3
import swOrderState from '../../state/order.store';
4
import template from './sw-order-create.html.twig';
5
import './sw-order-create.scss';
6
7
const { Context, Component, State, Mixin } = Shopware;
8
const { Criteria } = Shopware.Data;
9
10
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
11
Component.register('sw-order-create', {
12
    template,
13
14
    inject: ['repositoryFactory', 'feature'],
15
16
    mixins: [
17
        Mixin.getByName('notification'),
18
    ],
19
20
    data(): {
21
        isLoading: boolean,
22
        isSaveSuccessful: boolean,
23
        showInvalidCodeModal: boolean,
24
        showRemindPaymentModal: boolean,
25
        remindPaymentModalLoading: boolean,
26
        orderId: string | null,
27
        orderTransaction: { id: string, paymentMethodId: string } | null,
28
        paymentMethodName: string,
29
        } {
30
        return {
31
            isLoading: false,
32
            isSaveSuccessful: false,
33
            showInvalidCodeModal: false,
34
            showRemindPaymentModal: false,
35
            remindPaymentModalLoading: false,
36
            orderId: null,
37
            orderTransaction: null,
38
            paymentMethodName: '',
39
        };
40
    },
41
42
    computed: {
43
        customer(): Customer | null {
44
            return State.get('swOrder').customer;
45
        },
46
47
        cart(): Cart {
48
            return State.get('swOrder').cart;
49
        },
50
51
        invalidPromotionCodes(): PromotionCodeTag[] {
52
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
53
            return State.getters['swOrder/invalidPromotionCodes'] as PromotionCodeTag[];
54
        },
55
56
        isSaveOrderValid(): boolean {
57
            return (this.customer &&
58
                this.cart.token &&
59
                this.cart.lineItems.length &&
60
                !this.invalidPromotionCodes.length) as boolean;
61
        },
62
63
        paymentMethodRepository(): Repository {
64
            return this.repositoryFactory.create('payment_method');
65
        },
66
67
        showInitialModal(): boolean {
68
            return this.$route.name === 'sw.order.create.initial';
69
        },
70
    },
71
72
    beforeCreate(): void {
73
        State.registerModule('swOrder', swOrderState);
74
    },
75
76
    created(): void {
77
        this.createdComponent();
78
    },
79
80
    beforeDestroy(): void {
81
        this.unregisterModule();
82
    },
83
84
    methods: {
85
        createdComponent(): void {
86
            // set language to system language
87
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
88
            if (!State.getters['context/isSystemDefaultLanguage']) {
89
                State.commit('context/resetLanguageToDefault');
90
            }
91
        },
92
93
        unregisterModule(): void {
94
            State.unregisterModule('swOrder');
95
        },
96
97
        redirectToOrderList(): void {
98
            this.$router.push({ name: 'sw.order.index' });
99
        },
100
101
        saveFinish(): void {
102
            if (!this.orderId) {
103
                return;
104
            }
105
106
            this.isSaveSuccessful = false;
107
            this.$router.push({ name: 'sw.order.detail', params: { id: this.orderId } });
108
        },
109
110
        onSaveOrder(): Promise<void> {
111
            if (this.isSaveOrderValid) {
112
                this.isLoading = true;
113
                this.isSaveSuccessful = false;
114
115
                return State
116
                    .dispatch('swOrder/saveOrder', {
117
                        salesChannelId: this.customer?.salesChannelId,
118
                        contextToken: this.cart.token,
119
                    })
120
                    .then((response) => {
121
                        // eslint-disable-next-line max-len
122
                        // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
123
                        this.orderId = response?.data?.id;
124
                        // eslint-disable-next-line max-len
125
                        // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
126
                        this.orderTransaction = response?.data?.transactions?.[0];
127
128
                        if (!this.orderTransaction) {
129
                            return;
130
                        }
131
132
                        void this.paymentMethodRepository.get(
133
                            this.orderTransaction.paymentMethodId,
134
                            Context.api,
135
                            new Criteria(1, 1),
136
                            // @ts-expect-error
137
                        ).then((paymentMethod: PaymentMethod | null) => {
138
                            this.paymentMethodName = paymentMethod?.translated.distinguishableName ?? '';
139
                        });
140
141
                        this.showRemindPaymentModal = true;
142
                    })
143
                    // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
144
                    .catch((error) => this.showError(error))
145
                    .finally(() => {
146
                        this.isLoading = false;
147
                    });
148
            }
149
150
            if (this.invalidPromotionCodes.length > 0) {
151
                this.openInvalidCodeModal();
152
            } else {
153
                this.showError();
154
            }
155
156
            return Promise.resolve();
157
        },
158
159
        onCancelOrder() {
160
            if (this.customer === null || this.cart === null) {
161
                this.redirectToOrderList();
162
                return;
163
            }
164
165
            void State
166
                .dispatch('swOrder/cancelCart', {
167
                    salesChannelId: this.customer.salesChannelId,
168
                    contextToken: this.cart.token,
169
                })
170
                .then(() => this.redirectToOrderList());
171
        },
172
173
        showError(error: unknown = null) {
174
            // @ts-expect-error
175
            // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment
176
            const errorMessage = error?.response?.data?.errors?.[0]?.detail || null;
177
178
            // eslint-disable-next-line @typescript-eslint/no-unsafe-call
179
            this.createNotificationError({
180
                // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
181
                message: errorMessage || this.$tc('sw-order.create.messageSaveError'),
182
            });
183
        },
184
185
        openInvalidCodeModal() {
186
            this.showInvalidCodeModal = true;
187
        },
188
189
        closeInvalidCodeModal() {
190
            this.showInvalidCodeModal = false;
191
        },
192
193
        removeInvalidCode() {
194
            State.commit('swOrder/removeInvalidPromotionCodes');
195
            this.closeInvalidCodeModal();
196
        },
197
198
        onRemindPaymentModalClose() {
199
            this.isSaveSuccessful = true;
200
201
            this.showRemindPaymentModal = false;
202
        },
203
204
        onRemindCustomer() {
205
            this.remindPaymentModalLoading = true;
206
207
            void State.dispatch('swOrder/remindPayment', {
208
                orderTransactionId: this.orderTransaction?.id,
209
            }).then(() => {
210
                this.remindPaymentModalLoading = false;
211
212
                this.onRemindPaymentModalClose();
213
            });
214
        },
215
    },
216
});
217