11"use client"
22
33import 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" ;
55import 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
710export 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,6 +75,7 @@ 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" } >
@@ -36,7 +84,7 @@ export const RoleCreatePage: React.FC = () => {
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 >
0 commit comments