Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d70c67

Browse files
committed
Show create template conditionally
1 parent eb6644f commit 3d70c67

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

site/src/pages/TemplatesPage/TemplatesPage.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ import { TemplatesPageView } from "./TemplatesPageView"
66
const TemplatesPage: React.FC = () => {
77
const [templatesState] = useMachine(templatesMachine)
88

9-
return <TemplatesPageView templates={templatesState.context.templates} loading={templatesState.hasTag("loading")} />
9+
return (
10+
<TemplatesPageView
11+
templates={templatesState.context.templates}
12+
canCreateTemplate={templatesState.context.canCreateTemplate}
13+
loading={templatesState.hasTag("loading")}
14+
/>
15+
)
1016
}
1117

1218
export default TemplatesPage

site/src/pages/TemplatesPage/TemplatesPageView.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Link as RouterLink } from "react-router-dom"
1616
import * as TypesGen from "../../api/typesGenerated"
1717
import { Margins } from "../../components/Margins/Margins"
1818
import { Stack } from "../../components/Stack/Stack"
19+
import { combineClasses } from "../../util/combineClasses"
1920
import { firstLetter } from "../../util/firstLetter"
2021

2122
dayjs.extend(relativeTime)
@@ -27,6 +28,7 @@ export const Language = {
2728

2829
export interface TemplatesPageViewProps {
2930
loading?: boolean
31+
canCreateTemplate?: boolean
3032
templates?: TypesGen.Template[]
3133
error?: unknown
3234
}
@@ -38,16 +40,16 @@ export const TemplatesPageView: React.FC<TemplatesPageViewProps> = (props) => {
3840
<Stack spacing={4}>
3941
<Margins>
4042
<div className={styles.actions}>
43+
{props.canCreateTemplate && (
4144
<Button startIcon={<AddCircleOutline />}>{Language.createButton}</Button>
45+
)}
4246
</div>
4347
<Table>
4448
<TableHead>
4549
<TableRow>
4650
<TableCell>Name</TableCell>
47-
<TableCell>Resources</TableCell>
48-
<TableCell>Last Updated</TableCell>
49-
<TableCell>Provisioner</TableCell>
5051
<TableCell>Used By</TableCell>
52+
<TableCell>Last Updated</TableCell>
5153
</TableRow>
5254
</TableHead>
5355
<TableBody>
@@ -76,13 +78,10 @@ export const TemplatesPageView: React.FC<TemplatesPageViewProps> = (props) => {
7678
<Link component={RouterLink} to={`/templates/${template.id}`} className={styles.templateLink}>
7779
<b>{template.name}</b>
7880
</Link>
81+
{template.description}
7982
</div>
8083
</TableCell>
81-
<TableCell>{template.description}</TableCell>
8284
<TableCell>{dayjs().to(dayjs(template.updated_at))}</TableCell>
83-
<TableCell>
84-
<img alt="Terraform" src="/terraform-logo.svg" />
85-
</TableCell>
8685
<TableCell>{template.workspace_owner_count} developer{template.workspace_owner_count !== 1 && "s"}</TableCell>
8786
</TableRow>
8887
)

site/src/xServices/templates/templatesXService.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as TypesGen from "../../api/typesGenerated"
55
interface TemplatesContext {
66
organizations?: TypesGen.Organization[]
77
templates?: TypesGen.Template[]
8+
canCreateTemplate?: boolean
9+
permissionsError?: Error | unknown
810
organizationsError?: Error | unknown
911
templatesError?: Error | unknown
1012
}
@@ -18,6 +20,9 @@ export const templatesMachine = createMachine(
1820
getOrganizations: {
1921
data: TypesGen.Organization[]
2022
}
23+
getPermissions: {
24+
data: boolean
25+
}
2126
getTemplates: {
2227
data: TypesGen.Template[]
2328
}
@@ -34,7 +39,7 @@ export const templatesMachine = createMachine(
3439
onDone: [
3540
{
3641
actions: ["assignOrganizations", "clearOrganizationsError"],
37-
target: "gettingTemplates",
42+
target: "gettingPermissions",
3843
},
3944
],
4045
onError: [
@@ -46,6 +51,26 @@ export const templatesMachine = createMachine(
4651
},
4752
tags: "loading",
4853
},
54+
gettingPermissions: {
55+
entry: "clearPermissionsError",
56+
invoke: {
57+
src: "getPermissions",
58+
id: "getPermissions",
59+
onDone: [
60+
{
61+
target: "gettingTemplates",
62+
actions: ["assignPermissions", "clearPermissionsError"],
63+
},
64+
],
65+
onError: [
66+
{
67+
actions: "assignPermissionsError",
68+
target: "error",
69+
},
70+
],
71+
},
72+
tags: "loading",
73+
},
4974
gettingTemplates: {
5075
entry: "clearTemplatesError",
5176
invoke: {
@@ -78,6 +103,16 @@ export const templatesMachine = createMachine(
78103
...context,
79104
organizationsError: undefined,
80105
})),
106+
assignPermissions: assign({
107+
canCreateTemplate: (_, event) => event.data,
108+
}),
109+
assignPermissionsError: assign({
110+
permissionsError: (_, event) => event.data,
111+
}),
112+
clearPermissionsError: assign((context) => ({
113+
...context,
114+
permissionsError: undefined,
115+
})),
81116
assignTemplates: assign({
82117
templates: (_, event) => event.data,
83118
}),
@@ -88,6 +123,20 @@ export const templatesMachine = createMachine(
88123
},
89124
services: {
90125
getOrganizations: API.getOrganizations,
126+
getPermissions: async () => {
127+
const permName = "createTemplates"
128+
const resp = await API.checkUserPermissions("me", {
129+
checks: {
130+
[permName]: {
131+
action: "write",
132+
object: {
133+
resource_type: "template",
134+
},
135+
},
136+
},
137+
})
138+
return resp[permName]
139+
},
91140
getTemplates: async (context) => {
92141
if (!context.organizations || context.organizations.length === 0) {
93142
throw new Error("no organizations")

0 commit comments

Comments
 (0)