1- import {
2- type ReactElement ,
3- type ReactNode ,
4- Suspense ,
5- use ,
6- useCallback ,
7- useEffect ,
8- useMemo ,
9- useRef ,
10- useState ,
11- } from 'react' ;
1+ import { type ReactElement , useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
122import { groupSub } from 'group-sub' ;
13- import { ErrorBoundary } from 'react-error-boundary' ;
143
154import { ShortInput } from '@/components/ui/ShortInput' ;
165import { LongInput } from '@/components/ui/LongInput' ;
176import { ContentCard } from '@/components/atoms/ContentCard' ;
187import { Button } from '@/components/ui/button' ;
198import { StaticEditor } from '@/components/molecules/StaticEditor' ;
209import { useFetch } from '@/hooks/useFetch' ;
10+ import { getName , NameInput } from '@/components/molecules/NameInput' ;
11+ import { ClimbingBoxLoader , PacmanLoader } from 'react-spinners' ;
2112
22- function BaseTemplateRenderer ( {
23- templateCode,
24- onSubmit,
25- } : {
26- templateCode : string ;
27- onSubmit : ( code : string ) => void ;
28- } ) : ReactElement {
13+ function BaseTemplateRenderer ( { templateCode, template } : { templateCode : string ; template : string } ) : ReactElement {
2914 const [ currentCode , setCurrentCode ] = useState ( ( ) => templateCode ) ;
3015
3116 const onChange = useRef < ( ) => void | null > ( null ) ;
@@ -66,14 +51,63 @@ function BaseTemplateRenderer({
6651 } ;
6752 } , [ getCurrentCode ] ) ;
6853
54+ const [ submitting , setSubmitting ] = useState ( false ) ;
55+ const [ status , setStatus ] = useState < boolean | null > ( null ) ;
56+
57+ const handleSubmit = useCallback < ( ) => void > ( ( ) => {
58+ const code = getCurrentCode ( ) ;
59+ const name = getName ( ) ;
60+
61+ if ( ! code || ! name ) return ;
62+
63+ setSubmitting ( true ) ;
64+ fetch ( `/api/submit/${ template } ` , {
65+ method : 'POST' ,
66+ headers : { 'Content-Type' : 'application/json' } ,
67+ body : JSON . stringify ( { name, code } ) ,
68+ } ) . then ( res => {
69+ if ( ! res . ok ) {
70+ setStatus ( false ) ;
71+ setSubmitting ( false ) ;
72+ } else {
73+ setStatus ( true ) ;
74+ setSubmitting ( false ) ;
75+ // TODO: Store in localStorage
76+ }
77+ } ) ;
78+ } , [ getCurrentCode , template ] ) ;
79+
6980 return (
7081 < div className = "flex gap-24 w-full justify-around m-12 max-w-full" >
7182 < ContentCard className = "grow shrink basis-0 min-w-0" >
7283 < pre className = "text-start" >
7384 { parts . map ( part => ( part === '\n' ? < br /> : typeof part === 'string' ? part : part . component ) ) }
7485 </ pre >
7586 < hr className = "m-6 border-zinc-300 dark:border-zinc-600" />
76- < Button onClick = { ( ) => console . log ( getCurrentCode ( ) ) } > Submit!</ Button >
87+ < form className = "flex gap-4 justify-center" action = { handleSubmit } >
88+ < NameInput />
89+ < Button disabled = { submitting } className = "w-32" >
90+ { submitting ? 'Submitting...' : 'Submit!' }
91+ </ Button >
92+ { /*TODO*/ }
93+ < div className = "w-3 h-3" >
94+ { submitting ? (
95+ < PacmanLoader
96+ size = { 12 }
97+ color = "var(--foreground)"
98+ className = "mt-1"
99+ speedMultiplier = { 1.5 }
100+ cssOverride = { { width : 0 } }
101+ />
102+ ) : typeof status === 'boolean' ? (
103+ status ? (
104+ ':D'
105+ ) : (
106+ ':<'
107+ )
108+ ) : null }
109+ </ div >
110+ </ form >
77111 </ ContentCard >
78112 < ContentCard className = "grow shrink basis-0 min-w-0" >
79113 < StaticEditor code = { currentCode } />
@@ -82,19 +116,13 @@ function BaseTemplateRenderer({
82116 ) ;
83117}
84118
85- export function TemplateRenderer ( {
86- template,
87- onSubmit,
88- } : {
89- template : string ;
90- onSubmit : ( code : string ) => void ;
91- } ) : ReactElement {
119+ export function TemplateRenderer ( { template } : { template : string ; onSubmit : ( code : string ) => void } ) : ReactElement {
92120 const { data, loading, error } = useFetch < { template : string } > ( `/api/templates/${ template } ` ) ;
93121
94122 return (
95123 < >
96124 { loading ? < ContentCard > Loading...</ ContentCard > : null }
97- { data ? < BaseTemplateRenderer onSubmit = { onSubmit } templateCode = { data . template } /> : null }
125+ { data ? < BaseTemplateRenderer templateCode = { data . template } template = { template } /> : null }
98126 { error ? < ContentCard > Template not found.</ ContentCard > : null }
99127 </ >
100128 ) ;
0 commit comments