@@ -14,6 +14,8 @@ import OrgSkeleton from '../Skeletons/Organization.skeleton';
1414import { DeleteOrganization } from '../Mutations/OrganisationMutations' ;
1515import { RegisterNewOrganization } from '../Mutations/OrganisationMutations' ;
1616import { AddOrganization } from '../Mutations/OrganisationMutations' ;
17+ import jwtDecode from 'jwt-decode' ;
18+ import { useSearchParams , useNavigate } from 'react-router-dom' ;
1719
1820export interface Admin {
1921 id : string ;
@@ -145,6 +147,23 @@ const Organizations = () => {
145147 refetch : Function ;
146148 } = useQuery ( getOrganizations ) ;
147149
150+ const ApproveNewOrganization = async ( token :string ) => {
151+ try {
152+ const decodedToken :any = await jwtDecode ( token ) ;
153+ if ( ! decodedToken ) throw new Error ( "Failed to decode token" )
154+ const { nm :name , desc :description , email} = decodedToken ;
155+ const approvalResult = await ApproveOrganization ( { name, description, email } ) ;
156+
157+ if ( approvalResult && approvalResult . success ) {
158+ toast . success ( `${ name } organization has been approved.` ) ;
159+ } else {
160+ toast . error ( `${ name } organization approval failed.` ) ;
161+ }
162+ } catch ( error :any ) {
163+ toast . error ( `An error occurred, Try again` ) ;
164+ }
165+ }
166+
148167 const [ createOrganizationModel , setCreateOrganizationModel ] = useState ( false ) ;
149168 const [ deleteOrganizationModel , setDeleteOrganizationModel ] = useState ( false ) ;
150169 const [ showActions , setShowActions ] = useState ( false ) ;
@@ -162,6 +181,20 @@ const Organizations = () => {
162181 description : '' ,
163182 } ) ;
164183
184+ const [ searchParams ] = useSearchParams ( )
185+ const navigate = useNavigate ( ) ;
186+
187+
188+ useEffect ( ( ) => {
189+ const newOrgToken = searchParams . get ( "newOrgToken" ) ;
190+ if ( newOrgToken ) {
191+ ApproveNewOrganization ( newOrgToken ) ;
192+ searchParams . delete ( 'newOrgToken' ) ;
193+ navigate ( `?${ searchParams . toString ( ) } ` , { replace : true } ) ;
194+
195+ }
196+ } , [ ] ) ;
197+
165198 const handleShowActions = ( ) => {
166199 setShowActions ( ! showActions ) ;
167200 } ;
@@ -230,9 +263,19 @@ const Organizations = () => {
230263 }
231264
232265 async function ApproveOrganization ( data : any ) {
233- await RegisterOrganizationMutation ( {
234- variables : { organizationInput : data , action : 'approve' } ,
235- } ) ;
266+ try {
267+ const { data : mutationResult } = await RegisterOrganizationMutation ( {
268+ variables : { organizationInput : data , action : 'approve' } ,
269+ } ) ;
270+
271+ if ( mutationResult ) {
272+ return { success : true } ;
273+ } else {
274+ return { success : false } ;
275+ }
276+ } catch ( error :any ) {
277+ toast . error ( `An error occurred, Try again` ) ;
278+ }
236279 }
237280
238281 async function RejectOrganization ( data : any ) {
0 commit comments