1
1
import api , { getResponseCount } from '@/store/api' ;
2
2
import i18n from '@/i18n' ;
3
- const SnmpAlertsStore = {
4
- namespaced : true ,
5
- state : {
3
+ import { defineStore } from 'pinia' ;
4
+
5
+ export const SnmpAlertsStore = defineStore ( 'snmpAlerts' , {
6
+ state : ( ) => ( {
6
7
allSnmpDetails : [ ] ,
7
- } ,
8
+ } ) ,
8
9
getters : {
9
- allSnmpDetails ( state ) {
10
- return state . allSnmpDetails ;
11
- } ,
12
- } ,
13
- mutations : {
14
- setSnmpDetails ( state , allSnmpDetails ) {
15
- state . allSnmpDetails = allSnmpDetails ;
16
- } ,
10
+ allSnmpDetailsGetter : ( state ) => state . allSnmpDetails ,
17
11
} ,
18
12
actions : {
19
13
async getSnmpAlertUrl ( ) {
@@ -24,8 +18,8 @@ const SnmpAlertsStore = {
24
18
. then ( ( response ) => response . data [ '@odata.id' ] )
25
19
. catch ( ( error ) => console . log ( 'Error' , error ) ) ;
26
20
} ,
27
- async getSnmpDetails ( { commit , dispatch } ) {
28
- const snmpAlertUrl = await dispatch ( ' getSnmpAlertUrl' ) ;
21
+ async getSnmpDetails ( ) {
22
+ const snmpAlertUrl = await this . getSnmpAlertUrl ( ) ;
29
23
return await api
30
24
. get ( snmpAlertUrl )
31
25
. then ( ( response ) =>
@@ -37,27 +31,31 @@ const SnmpAlertsStore = {
37
31
const snmpDetailsDataFiltered = snmpDetailsData . filter (
38
32
( item ) => item . SubscriptionType === 'SNMPTrap' ,
39
33
) ;
40
- commit ( 'setSnmpDetails' , snmpDetailsDataFiltered ) ;
34
+ const finalSNmpData = snmpDetailsDataFiltered . map ( ( singleData ) => {
35
+ singleData . isSelected = false ;
36
+ return singleData ;
37
+ } )
38
+ this . allSnmpDetails = finalSNmpData ;
41
39
} )
42
40
. catch ( ( error ) => {
43
41
console . log ( error ) ;
44
- const message = i18n . t ( 'pageSnmpAlerts.toast.errorLoadSnmpDetails' ) ;
42
+ const message = i18n . global . t ( 'pageSnmpAlerts.toast.errorLoadSnmpDetails' ) ;
45
43
throw new Error ( message ) ;
46
44
} ) ;
47
45
} ,
48
- async deleteDestination ( { dispatch } , id ) {
49
- const snmpAlertUrl = await dispatch ( ' getSnmpAlertUrl' ) ;
46
+ async deleteDestination ( id ) {
47
+ const snmpAlertUrl = await this . getSnmpAlertUrl ( ) ;
50
48
return await api
51
49
. delete ( `${ snmpAlertUrl } /${ id } ` )
52
- . then ( ( ) => dispatch ( ' getSnmpDetails' ) )
50
+ . then ( ( ) => this . getSnmpDetails ( ) )
53
51
. then ( ( ) =>
54
- i18n . t ( 'pageSnmpAlerts.toast.successDeleteDestination' , {
52
+ i18n . global . t ( 'pageSnmpAlerts.toast.successDeleteDestination' , {
55
53
id,
56
54
} ) ,
57
55
)
58
56
. catch ( ( error ) => {
59
57
console . log ( error ) ;
60
- const message = i18n . t (
58
+ const message = i18n . global . t (
61
59
'pageSnmpAlerts.toast.errorDeleteDestination' ,
62
60
{
63
61
id,
@@ -66,8 +64,8 @@ const SnmpAlertsStore = {
66
64
throw new Error ( message ) ;
67
65
} ) ;
68
66
} ,
69
- async deleteMultipleDestinations ( { dispatch } , destination ) {
70
- const snmpAlertUrl = await dispatch ( ' getSnmpAlertUrl' ) ;
67
+ async deleteMultipleDestinations ( destination ) {
68
+ const snmpAlertUrl = await this . getSnmpAlertUrl ( ) ;
71
69
const promises = destination . map ( ( { id } ) => {
72
70
return api . delete ( `${ snmpAlertUrl } /${ id } ` ) . catch ( ( error ) => {
73
71
console . log ( error ) ;
@@ -77,22 +75,22 @@ const SnmpAlertsStore = {
77
75
return await api
78
76
. all ( promises )
79
77
. then ( ( response ) => {
80
- dispatch ( ' getSnmpDetails' ) ;
78
+ this . getSnmpDetails ( ) ;
81
79
return response ;
82
80
} )
83
81
. then (
84
82
api . spread ( ( ...responses ) => {
85
83
const { successCount, errorCount } = getResponseCount ( responses ) ;
86
84
let toastMessages = [ ] ;
87
85
if ( successCount ) {
88
- const message = i18n . tc (
86
+ const message = i18n . global . t (
89
87
'pageSnmpAlerts.toast.successBatchDelete' ,
90
88
successCount ,
91
89
) ;
92
90
toastMessages . push ( { type : 'success' , message } ) ;
93
91
}
94
92
if ( errorCount ) {
95
- const message = i18n . tc (
93
+ const message = i18n . global . t (
96
94
'pageSnmpAlerts.toast.errorBatchDelete' ,
97
95
errorCount ,
98
96
) ;
@@ -102,18 +100,18 @@ const SnmpAlertsStore = {
102
100
} ) ,
103
101
) ;
104
102
} ,
105
- async addDestination ( { dispatch } , { data } ) {
106
- const snmpAlertUrl = await dispatch ( ' getSnmpAlertUrl' ) ;
103
+ async addDestination ( { data } ) {
104
+ const snmpAlertUrl = await this . getSnmpAlertUrl ( ) ;
107
105
return await api
108
106
. post ( snmpAlertUrl , data )
109
- . then ( ( ) => dispatch ( ' getSnmpDetails' ) )
110
- . then ( ( ) => i18n . t ( 'pageSnmpAlerts.toast.successAddDestination' ) )
107
+ . then ( ( ) => this . getSnmpDetails ( ) )
108
+ . then ( ( ) => i18n . global . t ( 'pageSnmpAlerts.toast.successAddDestination' ) )
111
109
. catch ( ( error ) => {
112
110
console . log ( error ) ;
113
- const message = i18n . t ( 'pageSnmpAlerts.toast.errorAddDestination' ) ;
111
+ const message = i18n . global . t ( 'pageSnmpAlerts.toast.errorAddDestination' ) ;
114
112
throw new Error ( message ) ;
115
113
} ) ;
116
114
} ,
117
115
} ,
118
- } ;
116
+ } ) ;
119
117
export default SnmpAlertsStore ;
0 commit comments