3
3
import { platform } from '@/src/lib/trpc' ;
4
4
import { Button } from '@/src/components/shadcn-ui/button' ;
5
5
import { IdentificationCard } from '@phosphor-icons/react' ;
6
- import { useMemo , useState } from 'react' ;
6
+ import { useEffect , useMemo , useState } from 'react' ;
7
7
import { z } from 'zod' ;
8
8
import { useRouter } from 'next/navigation' ;
9
9
import { toast } from 'sonner' ;
@@ -16,19 +16,21 @@ import {
16
16
TooltipContent ,
17
17
TooltipTrigger
18
18
} from '@/src/components/shadcn-ui/tooltip' ;
19
+ import { useDebounce } from '@uidotdev/usehooks' ;
19
20
20
21
export function CreateOrg ( ) {
21
22
const [ orgName , setOrgName ] = useState ( '' ) ;
22
23
const [ orgShortcode , setOrgShortcode ] = useState ( '' ) ;
23
24
const [ customShortcode , setCustomShortcode ] = useState ( false ) ;
24
25
const router = useRouter ( ) ;
26
+ const debouncedOrgName = useDebounce ( orgName , 750 ) ;
25
27
26
28
const [ shortcodeValid , shortcodeError ] = useMemo ( ( ) => {
27
29
const { success, error } = z
28
30
. string ( )
29
31
. min ( 5 )
30
32
. max ( 64 )
31
- . regex ( / ^ [ a - z 0 - 9 \- ] * $ / , {
33
+ . regex ( / ^ [ a - z 0 - 9 ] * $ / , {
32
34
message : 'Only lowercase letters and numbers'
33
35
} )
34
36
. safeParse ( orgShortcode ) ;
@@ -48,16 +50,48 @@ export function CreateOrg() {
48
50
}
49
51
) ;
50
52
51
- const { refetch : generateShortcode } =
53
+ const { data : generateShortcodeData , error : generateShortcodeError } =
52
54
platform . org . crud . generateOrgShortcode . useQuery (
53
55
{
54
- orgName
56
+ orgName : debouncedOrgName
55
57
} ,
56
58
{
57
- enabled : false
59
+ enabled : ! customShortcode && debouncedOrgName . trim ( ) . length >= 5
58
60
}
59
61
) ;
60
62
63
+ const generateOrgShortcodeUtils =
64
+ platform . useUtils ( ) . org . crud . generateOrgShortcode ;
65
+
66
+ // Update the shortcode if the org name changes
67
+ useEffect ( ( ) => {
68
+ if ( generateShortcodeData ) setOrgShortcode ( generateShortcodeData . shortcode ) ;
69
+ } , [ generateShortcodeData ] ) ;
70
+
71
+ // If org name is less than 5 characters, clear the shortcode as min length is 5
72
+ useEffect ( ( ) => {
73
+ if ( debouncedOrgName . trim ( ) . length < 5 && ! customShortcode ) {
74
+ void generateOrgShortcodeUtils . invalidate ( ) ;
75
+ setOrgShortcode ( '' ) ;
76
+ }
77
+ } , [
78
+ debouncedOrgName ,
79
+ customShortcode ,
80
+ orgShortcode ,
81
+ generateOrgShortcodeUtils
82
+ ] ) ;
83
+
84
+ // handle shortcode generation error
85
+ useEffect ( ( ) => {
86
+ if ( generateShortcodeError ) {
87
+ toast . error ( 'An Error Occurred while generating the Org Shortcode' , {
88
+ description : generateShortcodeError . message
89
+ } ) ;
90
+ setOrgShortcode ( debouncedOrgName . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, '' ) ) ;
91
+ setCustomShortcode ( true ) ;
92
+ }
93
+ } , [ debouncedOrgName , generateShortcodeError ] ) ;
94
+
61
95
const { mutateAsync : createOrg , isPending } =
62
96
platform . org . crud . createNewOrg . useMutation ( {
63
97
onError : ( e ) => {
@@ -74,33 +108,6 @@ export function CreateOrg() {
74
108
fullWidth
75
109
inputSize = "lg"
76
110
value = { orgName }
77
- onBlur = { async ( ) => {
78
- // If the org name is empty, clear the shortcode
79
- if ( orgName . trim ( ) . length === 0 && ! customShortcode ) {
80
- if ( orgShortcode ) {
81
- setOrgShortcode ( '' ) ;
82
- }
83
- }
84
- // If the org name is less than 5 characters, do not generate a shortcode
85
- if ( customShortcode || orgName . trim ( ) . length < 5 ) return ;
86
- const { data, error } = await generateShortcode ( ) ;
87
-
88
- if ( error ) {
89
- toast . error (
90
- 'An Error Occurred while generating the Org Shortcode' ,
91
- {
92
- description : error . message
93
- }
94
- ) ;
95
- setOrgShortcode ( orgName . toLowerCase ( ) . replace ( / [ ^ a - z 0 - 9 ] / g, '' ) ) ;
96
- setCustomShortcode ( true ) ;
97
- return ;
98
- }
99
-
100
- if ( data ) {
101
- setOrgShortcode ( data . shortcode ) ;
102
- }
103
- } }
104
111
onChange = { ( e ) => setOrgName ( e . target . value ) }
105
112
leadingSlot = { IdentificationCard }
106
113
/>
0 commit comments