Skip to content

Commit e26c57f

Browse files
committed
refactor template fetching
1 parent cb846ba commit e26c57f

File tree

3 files changed

+69
-60
lines changed

3 files changed

+69
-60
lines changed

site/src/components/TemplateLayout/TemplateLayout.tsx

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,65 @@
11
import { makeStyles } from "@material-ui/core/styles"
2-
import { useMachine } from "@xstate/react"
32
import { useOrganizationId } from "hooks/useOrganizationId"
43
import { createContext, FC, Suspense, useContext } from "react"
54
import { NavLink, Outlet, useNavigate, useParams } from "react-router-dom"
65
import { combineClasses } from "util/combineClasses"
7-
import {
8-
TemplateContext,
9-
templateMachine,
10-
} from "xServices/template/templateXService"
116
import { Margins } from "components/Margins/Margins"
127
import { Stack } from "components/Stack/Stack"
13-
import { Permissions } from "xServices/auth/authXService"
148
import { Loader } from "components/Loader/Loader"
15-
import { usePermissions } from "hooks/usePermissions"
169
import { TemplatePageHeader } from "./TemplatePageHeader"
1710
import { AlertBanner } from "components/AlertBanner/AlertBanner"
11+
import {
12+
checkAuthorization,
13+
getTemplateByName,
14+
getTemplateDAUs,
15+
getTemplateVersion,
16+
getTemplateVersionResources,
17+
getTemplateVersions,
18+
} from "api/api"
19+
import { useQuery } from "@tanstack/react-query"
20+
21+
const templatePermissions = (templateId: string) => ({
22+
canUpdateTemplate: {
23+
object: {
24+
resource_type: "template",
25+
resource_id: templateId,
26+
},
27+
action: "update",
28+
},
29+
})
1830

19-
const useTemplateName = () => {
20-
const { template } = useParams()
31+
const fetchTemplate = async (orgId: string, templateName: string) => {
32+
const template = await getTemplateByName(orgId, templateName)
33+
const [activeVersion, resources, versions, daus, permissions] =
34+
await Promise.all([
35+
getTemplateVersion(template.active_version_id),
36+
getTemplateVersionResources(template.active_version_id),
37+
getTemplateVersions(template.id),
38+
getTemplateDAUs(template.id),
39+
checkAuthorization({
40+
checks: templatePermissions(template.id),
41+
}),
42+
])
2143

22-
if (!template) {
23-
throw new Error("No template found in the URL")
44+
return {
45+
template,
46+
activeVersion,
47+
resources,
48+
versions,
49+
daus,
50+
permissions,
2451
}
25-
26-
return template
2752
}
2853

29-
type TemplateLayoutContextValue = {
30-
context: TemplateContext
31-
permissions?: Permissions
54+
const useTemplateData = (orgId: string, templateName: string) => {
55+
return useQuery({
56+
queryKey: ["template", templateName],
57+
queryFn: () => fetchTemplate(orgId, templateName),
58+
})
3259
}
3360

61+
type TemplateLayoutContextValue = Awaited<ReturnType<typeof fetchTemplate>>
62+
3463
const TemplateLayoutContext = createContext<
3564
TemplateLayoutContextValue | undefined
3665
>(undefined)
@@ -50,38 +79,32 @@ export const TemplateLayout: FC<{ children?: JSX.Element }> = ({
5079
}) => {
5180
const navigate = useNavigate()
5281
const styles = useStyles()
53-
const organizationId = useOrganizationId()
54-
const templateName = useTemplateName()
55-
const [templateState, _] = useMachine(templateMachine, {
56-
context: {
57-
templateName,
58-
organizationId,
59-
},
60-
})
61-
const {
62-
template,
63-
permissions: templatePermissions,
64-
getTemplateError,
65-
} = templateState.context
66-
const permissions = usePermissions()
82+
const orgId = useOrganizationId()
83+
const { template } = useParams() as { template: string }
84+
const templateData = useTemplateData(orgId, template)
6785

68-
if (getTemplateError) {
86+
if (templateData.error) {
6987
return (
7088
<div className={styles.error}>
71-
<AlertBanner severity="error" error={getTemplateError} />
89+
<AlertBanner severity="error" error={templateData.error} />
7290
</div>
7391
)
7492
}
7593

76-
if (!template || !templatePermissions) {
94+
if (templateData.isLoading) {
7795
return <Loader />
7896
}
7997

98+
// Make typescript happy
99+
if (!templateData.data) {
100+
return <></>
101+
}
102+
80103
return (
81104
<>
82105
<TemplatePageHeader
83-
template={template}
84-
permissions={templatePermissions}
106+
template={templateData.data.template}
107+
permissions={templateData.data.permissions}
85108
onDeleteTemplate={() => {
86109
navigate("/templates")
87110
}}
@@ -92,7 +115,7 @@ export const TemplateLayout: FC<{ children?: JSX.Element }> = ({
92115
<Stack direction="row" spacing={0.25}>
93116
<NavLink
94117
end
95-
to={`/templates/${template.name}`}
118+
to={`/templates/${template}`}
96119
className={({ isActive }) =>
97120
combineClasses([
98121
styles.tabItem,
@@ -103,7 +126,7 @@ export const TemplateLayout: FC<{ children?: JSX.Element }> = ({
103126
Summary
104127
</NavLink>
105128
<NavLink
106-
to={`/templates/${template.name}/permissions`}
129+
to={`/templates/${template}/permissions`}
107130
className={({ isActive }) =>
108131
combineClasses([
109132
styles.tabItem,
@@ -118,9 +141,7 @@ export const TemplateLayout: FC<{ children?: JSX.Element }> = ({
118141
</div>
119142

120143
<Margins>
121-
<TemplateLayoutContext.Provider
122-
value={{ permissions, context: templateState.context }}
123-
>
144+
<TemplateLayoutContext.Provider value={templateData.data}>
124145
<Suspense fallback={<Loader />}>{children}</Suspense>
125146
</TemplateLayoutContext.Provider>
126147
</Margins>

site/src/pages/TemplatePage/TemplatePermissionsPage/TemplatePermissionsPage.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ export const TemplatePermissionsPage: FC<
1818
React.PropsWithChildren<unknown>
1919
> = () => {
2020
const organizationId = useOrganizationId()
21-
const { context } = useTemplateLayoutContext()
22-
const { template, permissions } = context
21+
const { template, permissions } = useTemplateLayoutContext()
2322
const { template_rbac: isTemplateRBACEnabled } = useFeatureVisibility()
2423
const [state, send] = useMachine(templateACLMachine, {
25-
context: { templateId: template?.id },
24+
context: { templateId: template.id },
2625
})
2726
const { templateACL, userToBeUpdated, groupToBeUpdated } = state.context
2827

site/src/pages/TemplatePage/TemplateSummaryPage/TemplateSummaryPage.tsx

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,10 @@ import { FC } from "react"
33
import { Helmet } from "react-helmet-async"
44
import { pageTitle } from "util/page"
55
import { TemplateSummaryPageView } from "./TemplateSummaryPageView"
6-
import { Loader } from "components/Loader/Loader"
76

87
export const TemplateSummaryPage: FC = () => {
9-
const { context } = useTemplateLayoutContext()
10-
const {
11-
template,
12-
activeTemplateVersion,
13-
templateResources,
14-
templateVersions,
15-
templateDAUs,
16-
} = context
17-
18-
if (!template || !activeTemplateVersion || !templateResources) {
19-
return <Loader />
20-
}
8+
const { template, activeVersion, resources, versions, daus } =
9+
useTemplateLayoutContext()
2110

2211
return (
2312
<>
@@ -34,10 +23,10 @@ export const TemplateSummaryPage: FC = () => {
3423
</Helmet>
3524
<TemplateSummaryPageView
3625
template={template}
37-
activeTemplateVersion={activeTemplateVersion}
38-
templateResources={templateResources}
39-
templateVersions={templateVersions}
40-
templateDAUs={templateDAUs}
26+
activeTemplateVersion={activeVersion}
27+
templateResources={resources}
28+
templateVersions={versions}
29+
templateDAUs={daus}
4130
/>
4231
</>
4332
)

0 commit comments

Comments
 (0)