@@ -61,6 +61,7 @@ const fields: Field[] = [
61
61
{ name : "users" , label : "Users (one email per line)" , type : "textarea" } ,
62
62
] ;
63
63
64
+
64
65
export default function GroupsPage ( ) {
65
66
const params = useParams ( ) ;
66
67
const orgId = params . orgId as string ;
@@ -132,39 +133,56 @@ export default function GroupsPage() {
132
133
133
134
const saveGroup = async ( group : Group ) => {
134
135
try {
136
+ // Create a copy of the group object to avoid mutating the original
137
+ const groupToSave = { ...group } ;
138
+
139
+ // Convert users from string to array of strings (splitting at newlines)
140
+ if ( typeof groupToSave . users === 'string' ) {
141
+ groupToSave . users = groupToSave . users
142
+ . split ( '\n' )
143
+ . map ( user => user . trim ( ) )
144
+ . filter ( user => user . length > 0 ) ; // Remove empty lines
145
+ }
146
+
147
+ console . log ( "groups" , groupToSave ) ;
148
+
135
149
const url = selectedGroup
136
150
? `/api/orgs/${ orgId } /groups/${ group . nameId } `
137
151
: `/api/orgs/${ orgId } /groups` ;
138
-
152
+
139
153
const response = await fetch ( url , {
140
154
method : selectedGroup ? "PATCH" : "POST" ,
141
155
headers : {
142
156
"Content-Type" : "application/json" ,
143
157
} ,
144
- body : JSON . stringify ( group ) ,
158
+ body : JSON . stringify ( groupToSave ) ,
145
159
} ) ;
146
-
160
+
147
161
if ( ! response . ok ) {
148
162
const errorData = await response . json ( ) . catch ( ( ) => ( { } ) ) ;
149
163
throw new Error ( formatValidationErrors ( errorData ) ) ;
150
164
}
151
-
165
+
152
166
const savedGroup = await response . json ( ) ;
153
-
167
+ // console.log('saved groups',savedGroup);
168
+
169
+
154
170
if ( selectedGroup ) {
155
171
setGroups ( groups . map ( ( g ) => ( g . id === savedGroup . id ? savedGroup : g ) ) ) ;
156
172
toast ( {
157
173
title : "Success" ,
158
174
description : "Group updated successfully" ,
159
175
} ) ;
160
176
} else {
177
+ console . log ( "savedgroups" , savedGroup ) ;
178
+
161
179
setGroups ( [ ...groups , savedGroup ] ) ;
162
180
toast ( {
163
181
title : "Success" ,
164
182
description : "Group created successfully" ,
165
183
} ) ;
166
184
}
167
-
185
+
168
186
setIsEditorOpen ( false ) ;
169
187
} catch ( error ) {
170
188
console . error ( "Error saving group:" , error ) ;
0 commit comments