@@ -4,6 +4,7 @@ import { GenericListing, ColumnDef } from "@/mint/generic-listing";
44import { GenericEditor , Field } from "@/mint/generic-editor" ;
55import { Group , mockGroups } from "./mockData" ;
66import { useEffect , useState } from "react" ;
7+ import { useParams } from "next/navigation" ;
78import { z } from "zod" ;
89
910const columns : ColumnDef < Group > [ ] = [
@@ -58,43 +59,82 @@ const fields: Field[] = [
5859] ;
5960
6061export default function GroupsPage ( ) {
62+ const params = useParams ( ) ;
63+ const orgId = params . orgId as string ;
64+
6165 const [ groups , setGroups ] = useState < Group [ ] > ( [ ] ) ;
6266 const [ selectedGroup , setSelectedGroup ] = useState < Group | null > ( null ) ;
6367 const [ isEditorOpen , setIsEditorOpen ] = useState ( false ) ;
6468
6569 useEffect ( ( ) => {
66- setGroups ( injectUsersCount ( mockGroups ) ) ;
67- } , [ ] ) ;
70+ const fetchGroups = async ( ) => {
71+ try {
72+ const response = await fetch ( `/api/orgs/${ orgId } /groups` ) ;
73+ if ( ! response . ok ) {
74+ throw new Error ( "Failed to fetch groups" ) ;
75+ }
76+ const data = await response . json ( ) ;
77+ setGroups ( injectUsersCount ( data ) ) ;
78+ } catch ( error ) {
79+ console . error ( "Error fetching groups:" , error ) ;
80+ // Fallback to mock data in case of error
81+ setGroups ( injectUsersCount ( mockGroups ) ) ;
82+ }
83+ } ;
84+ fetchGroups ( ) ;
85+ } , [ orgId ] ) ;
6886
6987 const deleteGroup = async ( group : Group ) => {
7088 try {
71- // Simulate API call
72- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
89+ const response = await fetch ( `/api/orgs/${ orgId } /groups/${ group . nameId } ` , {
90+ method : "DELETE" ,
91+ } ) ;
92+
93+ if ( ! response . ok ) {
94+ throw new Error ( "Failed to delete group" ) ;
95+ }
7396
74- // Update the state after successful API call
7597 setGroups ( ( prevGroups ) => prevGroups . filter ( ( g ) => g . id !== group . id ) ) ;
7698 return Promise . resolve ( ) ;
7799 } catch ( error ) {
100+ console . error ( "Error deleting group:" , error ) ;
78101 return Promise . reject ( error ) ;
79102 }
80103 } ;
81104
82105 const saveGroup = async ( group : Group ) => {
83- if ( selectedGroup ) {
84- // Update existing group
85- setGroups (
86- injectUsersCount ( groups . map ( ( g ) => ( g . id === group . id ? group : g ) ) ) ,
87- ) ;
88- } else {
89- // Add new group
90- setGroups (
91- injectUsersCount ( [
92- ...groups ,
93- { ...group , id : Date . now ( ) , createdAt : new Date ( ) . toISOString ( ) } ,
94- ] ) ,
95- ) ;
106+ try {
107+ const url = selectedGroup
108+ ? `/api/orgs/${ orgId } /groups/${ group . nameId } `
109+ : `/api/orgs/${ orgId } /groups` ;
110+
111+ const response = await fetch ( url , {
112+ method : selectedGroup ? "PATCH" : "POST" ,
113+ headers : {
114+ "Content-Type" : "application/json" ,
115+ } ,
116+ body : JSON . stringify ( group ) ,
117+ } ) ;
118+
119+ if ( ! response . ok ) {
120+ throw new Error (
121+ `Failed to ${ selectedGroup ? "update" : "create" } group` ,
122+ ) ;
123+ }
124+
125+ const savedGroup = await response . json ( ) ;
126+
127+ if ( selectedGroup ) {
128+ setGroups ( groups . map ( ( g ) => ( g . id === savedGroup . id ? savedGroup : g ) ) ) ;
129+ } else {
130+ setGroups ( [ ...groups , savedGroup ] ) ;
131+ }
132+
133+ setIsEditorOpen ( false ) ;
134+ } catch ( error ) {
135+ console . error ( "Error saving group:" , error ) ;
136+ throw error ;
96137 }
97- setIsEditorOpen ( false ) ;
98138 } ;
99139
100140 return (
0 commit comments