|
1
|
|
|
import template from './sw-error-summary.html'; |
|
2
|
|
|
import './sw-error-summary.scss'; |
|
3
|
|
|
|
|
4
|
|
|
const { Component } = Shopware; |
|
5
|
|
|
const { hasOwnProperty } = Shopware.Utils.object; |
|
6
|
|
|
|
|
7
|
|
|
type error = { |
|
8
|
|
|
_code: string, |
|
9
|
|
|
_detail: string, |
|
10
|
|
|
selfLink: string, |
|
11
|
|
|
}; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @private |
|
15
|
|
|
*/ |
|
16
|
|
|
Component.register('sw-error-summary', { |
|
17
|
|
|
template, |
|
18
|
|
|
|
|
19
|
|
|
computed: { |
|
20
|
|
|
errors(): { [key: string]: number } { |
|
21
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access |
|
22
|
|
|
const allErrors = (Shopware.State.getters['error/getAllApiErrors']() || []) as Array<unknown>; |
|
23
|
|
|
|
|
24
|
|
|
// Helper function to recursively get all error objects |
|
25
|
|
|
const extractErrorObjects = (errors: Array<unknown>) => { |
|
26
|
|
|
return errors.reduce((acc: Array<unknown>, error: unknown) => { |
|
27
|
|
|
if (error === null || typeof error !== 'object') { |
|
28
|
|
|
return acc; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (error.hasOwnProperty('selfLink') && error.hasOwnProperty('_code') && |
|
32
|
|
|
error.hasOwnProperty('_detail')) { |
|
33
|
|
|
acc.push(error); |
|
34
|
|
|
|
|
35
|
|
|
return acc; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
acc.push(...extractErrorObjects(Object.values(error))); |
|
39
|
|
|
|
|
40
|
|
|
return acc; |
|
41
|
|
|
}, []); |
|
42
|
|
|
}; |
|
43
|
|
|
|
|
44
|
|
|
// Retrieve all error objects and remap them to objects just containing a message |
|
45
|
|
|
const errorObjects = (extractErrorObjects(allErrors) as Array<error>).map((error): { message: string } => { |
|
46
|
|
|
let message = error._detail; |
|
47
|
|
|
|
|
48
|
|
|
if (this.$te(`global.error-codes.${error._code}`)) { |
|
49
|
|
|
message = this.$tc(`global.error-codes.${error._code}`); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return { |
|
53
|
|
|
message, |
|
54
|
|
|
}; |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
// Count the number of errors for each message |
|
58
|
|
|
return errorObjects.reduce((acc: { [key: string]: number }, error: { message: string }) => { |
|
59
|
|
|
if (!hasOwnProperty(acc, error.message)) { |
|
60
|
|
|
acc[error.message] = 1; |
|
61
|
|
|
} else { |
|
62
|
|
|
acc[error.message] += 1; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return acc; |
|
66
|
|
|
}, {}); |
|
67
|
|
|
}, |
|
68
|
|
|
|
|
69
|
|
|
errorEntries(): Array<{ message: string, count: number }> { |
|
70
|
|
|
return Object.entries(this.errors).map(([message, count]) => ({ |
|
71
|
|
|
message, |
|
72
|
|
|
count, |
|
73
|
|
|
})); |
|
74
|
|
|
}, |
|
75
|
|
|
|
|
76
|
|
|
errorCount(): number { |
|
77
|
|
|
return Object.values(this.errors).reduce((accumulator, value) => { |
|
78
|
|
|
return accumulator + value; |
|
79
|
|
|
}, 0); |
|
80
|
|
|
}, |
|
81
|
|
|
}, |
|
82
|
|
|
}); |
|
83
|
|
|
|