19
19
<v-btn
20
20
color =" primary"
21
21
variant =" text"
22
- @click =" openCreateDialog "
22
+ @click =" editItem() "
23
23
:disabled =" !newGroupName"
24
24
>
25
25
Create Group
26
26
</v-btn >
27
27
</v-col >
28
28
</v-row >
29
29
30
- <DialogGroupEdit
31
- @save =" saveGroup"
32
- @close =" closeDialog"
33
- v-model =" dialogGroup"
34
- :title =" dialogTitle"
35
- :editedGroup =" editedGroup"
36
- :nodes =" physicalNodes"
37
- />
38
-
39
30
<v-data-table
40
31
:headers =" headers_groups"
41
32
:items =" groups"
79
70
import { mapState , mapActions } from ' pinia'
80
71
import useBaseStore from ' ../stores/base.js'
81
72
import InstancesMixin from ' ../mixins/InstancesMixin.js'
82
- import { defineAsyncComponent } from ' vue'
83
73
84
74
export default {
85
75
name: ' Groups' ,
86
76
mixins: [InstancesMixin],
87
- components: {
88
- DialogGroupEdit: defineAsyncComponent (
89
- () => import (' @/components/dialogs/DialogGroupEdit.vue' ),
90
- ),
91
- },
92
- watch: {
93
- dialogGroup (val ) {
94
- val || this .closeDialog ()
95
- },
96
- },
77
+ components: {},
97
78
computed: {
98
79
... mapState (useBaseStore, [' nodes' ]),
99
80
physicalNodes () {
100
81
// Only return physical nodes (not virtual ones)
101
82
return this .nodes .filter ((node ) => ! node .virtual )
102
83
},
103
- dialogTitle () {
104
- return this .editedIndex === - 1 ? ' New Group' : ' Edit Group'
105
- },
106
84
},
107
85
data () {
108
86
return {
109
- dialogGroup: false ,
110
87
groups: [],
111
88
newGroupName: ' ' ,
112
- editedGroup: {},
113
- editedIndex: - 1 ,
114
89
headers_groups: [
115
90
{ title: ' ID' , key: ' id' },
116
91
{ title: ' Name' , key: ' name' },
@@ -140,52 +115,57 @@ export default {
140
115
this .groups = response .result || []
141
116
}
142
117
},
143
- openCreateDialog () {
144
- this .editedIndex = - 1
145
- this .editedGroup = {
146
- name: this .newGroupName ,
147
- nodeIds: [],
148
- }
149
- this .dialogGroup = true
150
- },
151
- editItem (item ) {
152
- this .editedIndex = this .groups .indexOf (item)
153
- this .editedGroup = {
154
- ... item,
155
- nodeIds: [... item .nodeIds ], // Create a copy of the array
156
- }
157
- this .dialogGroup = true
158
- },
159
- async deleteItem (group ) {
160
- if (
161
- await this .app .confirm (
162
- ' Attention' ,
163
- ` Are you sure you want to delete group "${ group .name } "? This will also remove the virtual node.` ,
164
- ' alert' ,
165
- )
166
- ) {
167
- const response = await this .app .apiRequest (' _deleteGroup' , [
168
- group .id ,
169
- ])
118
+ async editItem (existingGroup ) {
119
+ const isEdit = !! existingGroup
120
+
121
+ let inputs = [
122
+ {
123
+ type: ' text' ,
124
+ label: ' Group Name' ,
125
+ required: true ,
126
+ key: ' name' ,
127
+ hint: ' Enter a descriptive name for this multicast group' ,
128
+ default: isEdit ? existingGroup .name : this .newGroupName ,
129
+ },
130
+ {
131
+ type: ' list' ,
132
+ label: ' Nodes' ,
133
+ required: true ,
134
+ key: ' nodeIds' ,
135
+ multiple: true ,
136
+ items: this .physicalNodes .map (node => ({
137
+ title: node .name || ` Node ${ node .id } ` ,
138
+ value: node .id
139
+ })),
140
+ hint: ' Select at least 1 node for the multicast group' ,
141
+ default: isEdit ? existingGroup .nodeIds : [],
142
+ rules: [(value ) => {
143
+ if (! value || value .length === 0 ) {
144
+ return ' Please select at least one node'
145
+ }
146
+ return true
147
+ }],
148
+ },
149
+ ]
170
150
171
- if (response .success ) {
172
- this .showSnackbar (' Group deleted' , ' success' )
173
- this .refreshGroups ()
174
- }
151
+ let result = await this .app .confirm (
152
+ isEdit ? ' Edit Group' : ' New Group' ,
153
+ ' ' ,
154
+ ' info' ,
155
+ {
156
+ confirmText: isEdit ? ' Update' : ' Create' ,
157
+ width: 500 ,
158
+ inputs,
159
+ },
160
+ )
161
+
162
+ // cancelled
163
+ if (! result || Object .keys (result).length === 0 ) {
164
+ return
175
165
}
176
- },
177
- closeDialog () {
178
- this .dialogGroup = false
179
- setTimeout (() => {
180
- this .editedGroup = {}
181
- this .editedIndex = - 1
182
- this .newGroupName = ' '
183
- }, 300 )
184
- },
185
- async saveGroup () {
186
- const group = this .editedGroup
187
166
188
- if (! group .name || ! group .nodeIds || group .nodeIds .length === 0 ) {
167
+ // Validate inputs
168
+ if (! result .name || ! result .nodeIds || result .nodeIds .length === 0 ) {
189
169
this .showSnackbar (
190
170
' Please provide a name and select at least one node' ,
191
171
' error' ,
@@ -194,26 +174,47 @@ export default {
194
174
}
195
175
196
176
let response
197
- if (this .editedIndex === - 1 ) {
198
- // Create new group
199
- response = await this .app .apiRequest (' _createGroup' , [
200
- group .name ,
201
- group .nodeIds ,
202
- ])
203
- } else {
177
+ if (isEdit) {
204
178
// Update existing group
205
179
response = await this .app .apiRequest (' _updateGroup' , [
206
- group .id ,
207
- group .name ,
208
- group .nodeIds ,
180
+ existingGroup .id ,
181
+ result .name ,
182
+ result .nodeIds ,
183
+ ])
184
+ } else {
185
+ // Create new group
186
+ response = await this .app .apiRequest (' _createGroup' , [
187
+ result .name ,
188
+ result .nodeIds ,
209
189
])
210
190
}
211
191
212
192
if (response .success ) {
213
- const action = this . editedIndex === - 1 ? ' created ' : ' updated '
193
+ const action = isEdit ? ' updated ' : ' created '
214
194
this .showSnackbar (` Group ${ action} ` , ' success' )
215
195
this .refreshGroups ()
216
- this .closeDialog ()
196
+ // Clear the input field after successful creation
197
+ if (! isEdit) {
198
+ this .newGroupName = ' '
199
+ }
200
+ }
201
+ },
202
+ async deleteItem (group ) {
203
+ if (
204
+ await this .app .confirm (
205
+ ' Attention' ,
206
+ ` Are you sure you want to delete group "${ group .name } "? This will also remove the virtual node.` ,
207
+ ' alert' ,
208
+ )
209
+ ) {
210
+ const response = await this .app .apiRequest (' _deleteGroup' , [
211
+ group .id ,
212
+ ])
213
+
214
+ if (response .success ) {
215
+ this .showSnackbar (' Group deleted' , ' success' )
216
+ this .refreshGroups ()
217
+ }
217
218
}
218
219
},
219
220
},
0 commit comments