Passed
Push — trunk ( b524e1...0789a5 )
by Christian
14:27 queued 12s
created

index.ts ➔ customFieldSetCriteria   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
import type { Entity } from '@shopware-ag/admin-extension-sdk/es/data/_internals/Entity';
2
import Criteria from '@shopware-ag/admin-extension-sdk/es/data/Criteria';
3
import template from './index.html.twig';
4
import type Repository from '../../../../core/data/repository.data';
5
import { mapPropertyErrors } from '../../../../app/service/map-errors.service';
6
7
const { Component, Mixin } = Shopware;
8
9
/**
10
 * @private
11
 */
12
export default Component.wrapComponentConfig({
13
    template,
14
15
    mixins: [
16
        Mixin.getByName('notification'),
17
    ],
18
19
20
    inject: ['repositoryFactory', 'acl'],
21
22
23
    props: {
24
        /**
25
         * Either the id of the unit when in edit mode or null when in create mode.
26
         */
27
        unitId: {
28
            type: String,
29
            required: false,
30
            default: null,
31
        },
32
    },
33
34
    computed: {
35
        unitRepository(): Repository<'unit'> {
36
            return this.repositoryFactory.create('unit');
37
        },
38
39
        customFieldSetRepository(): Repository<'custom_field_set'> {
40
            return this.repositoryFactory.create('custom_field_set');
41
        },
42
43
        customFieldSetCriteria(): Criteria {
44
            const criteria = new Criteria(1, null);
45
            criteria.addFilter(Criteria.equals('relations.entityName', 'unit'));
46
47
            return criteria;
48
        },
49
50
        ...mapPropertyErrors('unit', ['name', 'shortCode']),
51
    },
52
53
    data(): {
54
        unit: Entity<'unit'>|null,
55
        isLoading: boolean,
56
        isSaveSuccessful: boolean,
57
        customFieldSets: Entity<'custom_field_set'>[]
58
        } {
59
        return {
60
            unit: null,
61
            isLoading: true,
62
            isSaveSuccessful: false,
63
            customFieldSets: [],
64
        };
65
    },
66
67
    watch: {
68
        unitId() {
69
            this.loadUnit();
70
        },
71
72
        isSaveSuccessful(newValue) {
73
            if (newValue === false) {
74
                return;
75
            }
76
77
            window.setTimeout(() => {
78
                this.isSaveSuccessful = false;
79
            }, 800);
80
        },
81
    },
82
83
    created() {
84
        this.customFieldSetRepository.search(this.customFieldSetCriteria).then((result) => {
85
            this.customFieldSets = result;
86
87
            if (this.unitId !== null) {
88
                this.loadUnit();
89
90
                return;
91
            }
92
93
            this.unit = this.unitRepository.create(Shopware.Context.api);
94
            this.isLoading = false;
95
        }).catch(() => {
96
            // eslint-disable-next-line @typescript-eslint/no-unsafe-call
97
            this.createNotificationError({
98
                message: this.$tc('sw-settings-units.notification.errorMessage'),
99
            });
100
101
            this.isLoading = false;
102
        });
103
    },
104
105
    methods: {
106
        loadUnit(): void {
107
            this.isLoading = true;
108
109
            this.unitRepository.get(this.unitId, Shopware.Context.api).then((unit) => {
110
                this.unit = unit;
111
112
                this.isLoading = false;
113
            }).catch((error: { message: string }) => {
114
                // eslint-disable-next-line @typescript-eslint/no-unsafe-call
115
                this.createNotificationError({
116
                    message: this.$tc(error.message),
117
                });
118
            });
119
        },
120
121
        onSave(): void {
122
            if (this.unit === null) {
123
                return;
124
            }
125
126
            this.isLoading = true;
127
            this.unitRepository.save(this.unit).then(() => {
128
                this.isSaveSuccessful = true;
129
130
                void this.$router.push({ name: 'sw.settings.units.detail', params: { id: this.unit?.id ?? '' } });
131
132
                this.isLoading = false;
133
            }).catch(() => {
134
                // eslint-disable-next-line @typescript-eslint/no-unsafe-call
135
                this.createNotificationError({
136
                    message: this.$tc('sw-settings-units.notification.errorMessage'),
137
                });
138
139
                this.isLoading = false;
140
            });
141
        },
142
143
        onChangeLanguage(): void {
144
            this.loadUnit();
145
        },
146
    },
147
});
148