11'use client' ;
22
33import { useEffect , useState } from 'react' ;
4+ import { useParams } from 'next/navigation' ;
45import { graphql } from '@/gql' ;
5- import { AddUserToOrganizationInput } from '@/gql/generated/graphql' ;
6+ import {
7+ AddRemoveUserToOrganizationInput ,
8+ AssignOrganizationRoleInput ,
9+ } from '@/gql/generated/graphql' ;
610import { useMutation , useQuery } from '@tanstack/react-query' ;
711import { Button , Dialog , Label , Select , toast } from 'opub-ui' ;
812
@@ -11,7 +15,7 @@ import { toTitleCase } from '@/lib/utils';
1115import { FetchUsers } from '../usecases/edit/[id]/contributors/query' ;
1216
1317const addUserDoc : any = graphql ( `
14- mutation addUserToOrganization($input: AddUserToOrganizationInput !) {
18+ mutation addUserToOrganization($input: AddRemoveUserToOrganizationInput !) {
1519 addUserToOrganization(input: $input) {
1620 __typename
1721 ... on TypeOrganizationMembership {
@@ -33,22 +37,34 @@ const allRolesDoc: any = graphql(`
3337 }
3438` ) ;
3539
40+ const updateUser : any = graphql ( `
41+ mutation assignOrganizationRole($input: AssignOrganizationRoleInput!) {
42+ assignOrganizationRole(input: $input) {
43+ success
44+ message
45+ }
46+ }
47+ ` ) ;
48+
3649const AddUser = ( {
3750 setIsOpen,
3851 selectedUser,
39- title,
4052 isOpen,
41- refetchUsers,
4253 isEdit,
54+ setRefetch,
4355} : {
4456 setIsOpen : ( isOpen : boolean ) => void ;
4557 selectedUser : any ;
46- title : string ;
4758 isOpen : boolean ;
48- refetchUsers : any ;
4959 isEdit : boolean ;
60+ setRefetch : ( refetch : boolean ) => void ;
5061} ) => {
5162 const [ searchValue , setSearchValue ] = useState ( '' ) ;
63+ const params = useParams < {
64+ entityType : string ;
65+ entitySlug : string ;
66+ id : string ;
67+ } > ( ) ;
5268
5369 const Users : { data : any ; isLoading : boolean ; refetch : any } = useQuery (
5470 [ `fetch_users_list` ] ,
@@ -67,11 +83,16 @@ const AddUser = ({
6783 }
6884 ) ;
6985
70-
71-
7286 const RolesList : { data : any ; isLoading : boolean ; refetch : any } = useQuery (
7387 [ `fetch_UseCaseData` ] ,
74- ( ) => GraphQL ( allRolesDoc , { } , [ ] )
88+ ( ) =>
89+ GraphQL (
90+ allRolesDoc ,
91+ {
92+ [ params . entityType ] : params . entitySlug ,
93+ } ,
94+ [ ]
95+ )
7596 ) ;
7697
7798 useEffect ( ( ) => {
@@ -88,8 +109,14 @@ const AddUser = ({
88109 } , [ selectedUser ] ) ;
89110
90111 const { mutate, isLoading : addUserLoading } = useMutation (
91- ( input : { input : AddUserToOrganizationInput } ) =>
92- GraphQL ( addUserDoc , { } , input ) ,
112+ ( input : { input : AddRemoveUserToOrganizationInput } ) =>
113+ GraphQL (
114+ addUserDoc ,
115+ {
116+ [ params . entityType ] : params . entitySlug ,
117+ } ,
118+ input
119+ ) ,
93120 {
94121 onSuccess : ( res : any ) => {
95122 toast ( 'User added successfully' ) ;
@@ -99,14 +126,36 @@ const AddUser = ({
99126 userId : '' ,
100127 roleId : '' ,
101128 } ) ;
102- refetchUsers ( ) ;
129+ setRefetch ( true ) ;
103130 } ,
104131 onError : ( err : any ) => {
105132 toast ( 'Failed to add user' ) ;
106133 } ,
107134 }
108135 ) ;
109136
137+ const { mutate : updateMutate , isLoading : updateUserLoading } = useMutation (
138+ ( input : { input : AssignOrganizationRoleInput } ) =>
139+ GraphQL ( updateUser , {
140+ [ params . entityType ] : params . entitySlug ,
141+ } , input ) ,
142+ {
143+ onSuccess : ( res : any ) => {
144+ toast ( 'User updated successfully' ) ;
145+ // Optionally, reset form or perform other actions
146+ setIsOpen ( false ) ;
147+ setFormData ( {
148+ userId : '' ,
149+ roleId : '' ,
150+ } ) ;
151+ setRefetch ( true ) ;
152+ } ,
153+ onError : ( err : any ) => {
154+ toast ( 'Failed to update user' ) ;
155+ } ,
156+ }
157+ ) ;
158+
110159 const [ formData , setFormData ] = useState ( {
111160 userId : '' ,
112161 roleId : '' ,
@@ -134,13 +183,12 @@ const AddUser = ({
134183 setIsDropdownOpen ( false ) ; // Close dropdown
135184 } ;
136185
137-
138186 return (
139187 < div >
140188 < Dialog open = { isOpen } onOpenChange = { setIsOpen } >
141189 { isOpen && (
142190 < Dialog . Content
143- title = { isEdit ? " Edit Admin / Member" : " Add Admin / Member" }
191+ title = { isEdit ? ' Edit Admin / Member' : ' Add Admin / Member' }
144192 className = "h-72 overflow-y-auto"
145193 >
146194 < div className = "m-auto mb-6 flex flex-col gap-6" >
@@ -151,6 +199,7 @@ const AddUser = ({
151199 id = "combobox"
152200 disabled = { isEdit }
153201 value = { searchValue }
202+ autoComplete = 'off'
154203 onChange = { handleInputChange }
155204 className = "border border-gray-100 placeholder:text-sm mt-1 block w-full px-3 py-1"
156205 placeholder = { 'Select user' }
@@ -159,7 +208,7 @@ const AddUser = ({
159208 < div className = "border border-gray-300 rounded-md shadow-lg absolute left-0 right-0 z-1 mt-2 max-h-60 overflow-y-auto rounded-2 bg-white px-1 py-2 shadow-basicXl" >
160209 { filteredOptions . map ( ( option : any ) => (
161210 < div
162- key = { option . value }
211+ key = { option . id }
163212 className = "cursor-pointer rounded-2 px-4 py-2 hover:bg-baseGraySlateSolid3"
164213 onClick = { ( ) => handleSelectOption ( option ) }
165214 >
@@ -192,10 +241,10 @@ const AddUser = ({
192241 className = "m-auto"
193242 onClick = { ( ) => {
194243 setIsOpen ( false ) ;
195- mutate ( { input : formData } ) ;
244+ isEdit ? updateMutate ( { input : formData } ) : mutate ( { input : formData } ) ;
196245 } }
197246 >
198- { isEdit ? " Update" : " Add" }
247+ { isEdit ? ' Update' : ' Add' }
199248 </ Button >
200249 </ div >
201250 </ Dialog . Content >
0 commit comments