Skip to content

Commit b2d75a0

Browse files
committed
refactor OrgProfile to utilize entity details from global state and streamline data handling
1 parent 31d3fc7 commit b2d75a0

File tree

1 file changed

+31
-43
lines changed
  • app/[locale]/dashboard/[entityType]/[entitySlug]/profile

1 file changed

+31
-43
lines changed

app/[locale]/dashboard/[entityType]/[entitySlug]/profile/orgProfile.tsx

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
1-
import React, { useEffect } from 'react';
2-
import { useParams } from 'next/navigation';
31
import { graphql } from '@/gql';
42
import {
53
ApiOrganizationOrganizationTypesEnum,
64
OrganizationInputPartial,
75
} from '@/gql/generated/graphql';
86
import { useOrganizationTypes } from '@/hooks/useOrganizationTypes';
9-
import { useMutation, useQuery } from '@tanstack/react-query';
10-
import { Button, DropZone, Select, Text, TextField, toast } from 'opub-ui';
11-
127
import { GraphQL } from '@/lib/api';
13-
14-
const OrgDetails: any = graphql(`
15-
query orgDetails($slug: String) {
16-
organizations(slug: $slug) {
17-
id
18-
name
19-
logo {
20-
name
21-
path
22-
}
23-
homepage
24-
organizationTypes
25-
contactEmail
26-
description
27-
slug
28-
}
29-
}
30-
`);
8+
import { useMutation } from '@tanstack/react-query';
9+
import { useParams, useRouter } from 'next/navigation';
10+
import { Button, DropZone, Select, Text, TextField, toast } from 'opub-ui';
11+
import React, { useEffect } from 'react';
12+
import { useDashboardStore } from '../layout';
3113

3214
const organizationUpdateMutation: any = graphql(`
3315
mutation updateOrganization($input: OrganizationInputPartial!) {
@@ -52,31 +34,25 @@ const organizationUpdateMutation: any = graphql(`
5234
`);
5335

5436
const OrgProfile = () => {
55-
const params = useParams<{ entitySlug: string }>();
37+
const params = useParams<{ entitySlug: string; entityType: string }>();
38+
const router = useRouter();
39+
const { setEntityDetails, entityDetails } = useDashboardStore();
5640

57-
const orgDetails: any = useQuery([`org_details_${params.entitySlug}`], () =>
58-
GraphQL(
59-
OrgDetails,
60-
{},
61-
{
62-
slug: params.entitySlug,
63-
}
64-
)
65-
);
6641
const { organizationTypes } = useOrganizationTypes();
42+
6743
useEffect(() => {
68-
if (orgDetails.data) {
44+
if (entityDetails && entityDetails.organizations) {
6945
setFormData({
70-
name: orgDetails.data?.organizations[0].name,
71-
contactEmail: orgDetails.data?.organizations[0].contactEmail,
72-
organizationTypes: orgDetails.data?.organizations[0].organizationTypes,
73-
homepage: orgDetails.data?.organizations[0].homepage,
74-
description: orgDetails.data?.organizations[0].description,
75-
logo: orgDetails.data?.organizations[0].logo,
76-
id: orgDetails.data?.organizations[0].id,
46+
name: entityDetails?.organizations[0].name,
47+
contactEmail: entityDetails?.organizations[0].contactEmail,
48+
organizationTypes: entityDetails?.organizations[0].organizationTypes,
49+
homepage: entityDetails?.organizations[0].homepage,
50+
description: entityDetails?.organizations[0].description,
51+
logo: entityDetails?.organizations[0].logo,
52+
id: entityDetails?.organizations[0].id,
7753
});
7854
}
79-
}, [orgDetails.data]);
55+
}, [entityDetails?.organizations]);
8056

8157
const initialFormData = {
8258
name: '',
@@ -105,6 +81,14 @@ const OrgProfile = () => {
10581
logo: res?.updateOrganization?.logo,
10682
id: res?.updateOrganization?.id,
10783
});
84+
setEntityDetails({
85+
...entityDetails,
86+
organizations: [res?.updateOrganization],
87+
});
88+
if (res?.updateOrganization?.slug && res.updateOrganization.slug !== params.entitySlug) {
89+
const newPath = `/dashboard/${params.entityType}/${res.updateOrganization.slug}/profile`;
90+
router.replace(newPath);
91+
}
10892
},
10993
onError: (error: any) => {
11094
toast(`Error: ${error.message}`);
@@ -128,7 +112,11 @@ const OrgProfile = () => {
128112
}
129113

130114
mutate({ input: inputData });
115+
116+
131117
};
118+
119+
132120
return (
133121
<div>
134122
<div>
@@ -214,4 +202,4 @@ const OrgProfile = () => {
214202
);
215203
};
216204

217-
export default OrgProfile;
205+
export default OrgProfile;

0 commit comments

Comments
 (0)