11import { useNavigate , useParams } from 'solid-app-router' ;
22import { Icon } from 'solid-heroicons' ;
33import { eye , eyeOff , plus , x } from 'solid-heroicons/outline' ;
4- import { createResource , For , Suspense } from 'solid-js' ;
4+ import { createResource , For , Show , Suspense } from 'solid-js' ;
55import { createStore , produce } from 'solid-js/store' ;
66import { defaultTabs } from '../../src' ;
77import { API , useAppContext } from '../context' ;
@@ -55,6 +55,21 @@ export const Home = () => {
5555 < button
5656 class = "bg-solid-lightgray shadow-md dark:bg-solid-darkLighterBg rounded-xl p-4 text-3xl flex mx-auto"
5757 onClick = { async ( ) => {
58+ if ( ! params . user && ! context . user ( ) ?. display ) {
59+ let id = Math . floor ( Date . now ( ) / 1000 ) . toString ( ) ;
60+ localStorage . setItem (
61+ id ,
62+ JSON . stringify ( {
63+ title : 'Counter Example' ,
64+ public : true ,
65+ labels : [ ] ,
66+ version : '1.0' ,
67+ files : defaultTabs . map ( ( x ) => ( { name : x . name , content : x . source . split ( '\n' ) } ) ) ,
68+ } ) ,
69+ ) ;
70+ navigate ( `/local/${ id } ` ) ;
71+ return ;
72+ }
5873 const result = await fetch ( `${ API } /repl` , {
5974 method : 'POST' ,
6075 headers : {
@@ -73,7 +88,8 @@ export const Home = () => {
7388 navigate ( `/${ context . user ( ) ?. display } /${ id } ` ) ;
7489 } }
7590 >
76- < Icon path = { plus } class = "w-8" /> Create new REPL
91+ < Icon path = { plus } class = "w-8" /> { ' ' }
92+ { params . user || context . user ( ) ?. display ? 'Create new REPL' : 'Create Anonymous REPL' }
7793 </ button >
7894
7995 < table class = "w-128 mx-auto my-8" >
@@ -106,56 +122,67 @@ export const Home = () => {
106122 </ tr >
107123 }
108124 >
109- < For each = { get ( repls . list ) } >
110- { ( repl , i ) => (
125+ < Show
126+ when = { params . user || context . user ( ) ?. display }
127+ fallback = {
111128 < tr >
112- < td >
113- < a href = { `${ params . user || context . user ( ) ?. display } /${ repl . id } ` } > { repl . title } </ a >
114- </ td >
115- < td > { new Date ( repl . created_at ) . toLocaleString ( ) } </ td >
116- < td >
117- < Icon
118- path = { repl . public ? eye : eyeOff }
119- class = "w-6 inline m-2 ml-0 cursor-pointer"
120- onClick = { async ( ) => {
121- fetch ( `${ API } /repl/${ repl . id } ` , {
122- method : 'PUT' ,
123- headers : {
124- 'authorization' : `Bearer ${ context . token } ` ,
125- 'Content-Type' : 'application/json' ,
126- } ,
127- body : JSON . stringify ( {
128- ...repl ,
129- public : ! repl . public ,
130- } ) ,
131- } ) ;
132- setRepls (
133- produce ( ( x ) => {
134- x ! . list [ i ( ) ] . public = ! repl . public ;
135- } ) ,
136- ) ;
137- } }
138- />
139- < Icon
140- path = { x }
141- class = "w-6 inline m-2 mr-0 text-red-700 cursor-pointer"
142- onClick = { async ( ) => {
143- fetch ( `${ API } /repl/${ repl . id } ` , {
144- method : 'DELETE' ,
145- headers : {
146- authorization : `Bearer ${ context . token } ` ,
147- } ,
148- } ) ;
149- setRepls ( {
150- total : repls . total - 1 ,
151- list : repls . list . filter ( ( x ) => x . id !== repl . id ) ,
152- } ) ;
153- } }
154- />
129+ < td colspan = "3" class = "text-center" >
130+ Not logged in. Please login to see your repls.
155131 </ td >
156132 </ tr >
157- ) }
158- </ For >
133+ }
134+ >
135+ < For each = { get ( repls . list ) } >
136+ { ( repl , i ) => (
137+ < tr >
138+ < td >
139+ < a href = { `${ params . user || context . user ( ) ?. display } /${ repl . id } ` } > { repl . title } </ a >
140+ </ td >
141+ < td > { new Date ( repl . created_at ) . toLocaleString ( ) } </ td >
142+ < td >
143+ < Icon
144+ path = { repl . public ? eye : eyeOff }
145+ class = "w-6 inline m-2 ml-0 cursor-pointer"
146+ onClick = { async ( ) => {
147+ fetch ( `${ API } /repl/${ repl . id } ` , {
148+ method : 'PUT' ,
149+ headers : {
150+ 'authorization' : `Bearer ${ context . token } ` ,
151+ 'Content-Type' : 'application/json' ,
152+ } ,
153+ body : JSON . stringify ( {
154+ ...repl ,
155+ public : ! repl . public ,
156+ } ) ,
157+ } ) ;
158+ setRepls (
159+ produce ( ( x ) => {
160+ x ! . list [ i ( ) ] . public = ! repl . public ;
161+ } ) ,
162+ ) ;
163+ } }
164+ />
165+ < Icon
166+ path = { x }
167+ class = "w-6 inline m-2 mr-0 text-red-700 cursor-pointer"
168+ onClick = { async ( ) => {
169+ fetch ( `${ API } /repl/${ repl . id } ` , {
170+ method : 'DELETE' ,
171+ headers : {
172+ authorization : `Bearer ${ context . token } ` ,
173+ } ,
174+ } ) ;
175+ setRepls ( {
176+ total : repls . total - 1 ,
177+ list : repls . list . filter ( ( x ) => x . id !== repl . id ) ,
178+ } ) ;
179+ } }
180+ />
181+ </ td >
182+ </ tr >
183+ ) }
184+ </ For >
185+ </ Show >
159186 </ Suspense >
160187 </ tbody >
161188 </ table >
0 commit comments