Skip to content

Commit 3ad4a3e

Browse files
author
Nico Sammito
authored
Merge pull request #93 from code0-tech/feat/#75
Create role mutation and screen are missing and not linked
2 parents 9bf4c0b + 9245b68 commit 3ad4a3e

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

src/packages/ce/src/role/pages/RoleCreatePage.tsx

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,60 @@
11
"use client"
22

33
import React from "react";
4-
import {Button, Col, Flex, Spacing, Text, TextInput} from "@code0-tech/pictor";
4+
import {Button, Col, Flex, Spacing, Text, TextInput, toast, useForm, useService} from "@code0-tech/pictor";
55
import Link from "next/link";
6+
import {useParams, useRouter} from "next/navigation";
7+
import {Namespace} from "@code0-tech/sagittarius-graphql-types";
8+
import {RoleService} from "@edition/role/services/Role.service";
69

710
export const RoleCreatePage: React.FC = () => {
811

912
//TODO: user abilities for add role within namespace
1013

14+
const params = useParams()
15+
const roleService = useService(RoleService)
16+
const [, startTransition] = React.useTransition()
17+
const router = useRouter()
18+
19+
const namespaceIndex = params?.namespaceId as string
20+
const namespaceId: Namespace['id'] = `gid://sagittarius/Namespace/${namespaceIndex as unknown as number}`
21+
22+
//TODO: user abilities for project creation within namespace
23+
24+
const [inputs, validate] = useForm({
25+
initialValues: {
26+
name: ""
27+
},
28+
validate: {
29+
name: (value) => {
30+
if (!value) return "Name is required"
31+
if (value.length < 3) return "Name needs to be at least 3 characters"
32+
if (value.length > 50) return "Name needs to be less than 50 characters"
33+
return null
34+
},
35+
},
36+
onSubmit: (values) => {
37+
startTransition(() => {
38+
if (!namespaceId) {
39+
toast({
40+
title: "The current user does not have a personal namespace.",
41+
color: "error",
42+
dismissible: true,
43+
})
44+
return
45+
}
46+
roleService.roleCreate({
47+
name: values.name as unknown as string,
48+
namespaceId: namespaceId
49+
}).then(payload => {
50+
if ((payload?.errors?.length ?? 0) <= 0) {
51+
router.push(`/namespace/${namespaceIndex}/roles`)
52+
}
53+
})
54+
})
55+
}
56+
})
57+
1158
return <div style={{
1259
background: "#070514",
1360
height: "100%",
@@ -28,15 +75,16 @@ export const RoleCreatePage: React.FC = () => {
2875
<TextInput required
2976
title={"Name"}
3077
description={"Provide a simple role name"}
78+
{...inputs.getInputProps("name")}
3179
/>
3280
<Spacing spacing={"xl"}/>
3381
<Flex style={{gap: "0.35rem"}} justify={"space-between"}>
34-
<Link href={"/public"}>
82+
<Link href={`/namespace/${namespaceIndex}/roles`}>
3583
<Button color={"primary"}>
3684
Go back to roles
3785
</Button>
3886
</Link>
39-
<Button color={"success"}>
87+
<Button color={"success"} onClick={validate}>
4088
Create role
4189
</Button>
4290
</Flex>

src/packages/ce/src/role/services/Role.service.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {GraphqlClient} from "@core/util/graphql-client";
1818
import rolesQuery from "@edition/role/services/queries/Roles.query.graphql";
1919
import roleUpdateMutation from "@edition/role/services/mutations/Role.update.mutation.graphql";
2020
import roleDeleteMutation from "@edition/role/services/mutations/Role.delete.mutation.graphql";
21+
import roleCreateMutation from "@edition/role/services/mutations/Role.create.mutation.graphql";
2122
import roleAssignAbilitiesMutation from "@edition/role/services/mutations/Role.assignAbilities.mutation.graphql";
2223
import roleAssignProjectsMutation from "@edition/role/services/mutations/Role.assignProjects.mutation.graphql";
2324
import {View} from "@code0-tech/pictor/dist/utils/view";
@@ -157,8 +158,22 @@ export class RoleService extends ReactiveArrayService<RoleView, RoleDependencies
157158
return result.data?.namespacesRolesUpdate ?? undefined
158159
}
159160

160-
roleCreate(payload: NamespacesRolesCreateInput): Promise<NamespacesRolesCreatePayload | undefined> {
161-
throw new Error("Method not implemented.")
161+
async roleCreate(payload: NamespacesRolesCreateInput): Promise<NamespacesRolesCreatePayload | undefined> {
162+
const result = await this.client.mutate<Mutation, NamespacesRolesCreateInput>({
163+
mutation: roleCreateMutation,
164+
variables: {
165+
...payload
166+
}
167+
})
168+
169+
if (result.data && result.data.namespacesRolesCreate && result.data.namespacesRolesCreate.namespaceRole) {
170+
const role = result.data.namespacesRolesCreate.namespaceRole
171+
if (!this.hasById(role.id)) {
172+
this.add(new View(new RoleView(role)))
173+
}
174+
}
175+
176+
return result.data?.namespacesRolesCreate ?? undefined
162177
}
163178

164179
async roleDelete(payload: NamespacesRolesDeleteInput): Promise<NamespacesRolesDeletePayload | undefined> {

src/packages/ce/src/role/services/mutations/Role.create.mutation.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#import "../fragments/Role.basic.fragment.graphql"
2+
13
mutation createRole($namespaceId: NamespaceID!, $name: String!) {
24
namespacesRolesCreate(input: {
35
namespaceId: $namespaceId,

0 commit comments

Comments
 (0)