File tree Expand file tree Collapse file tree 7 files changed +114
-3
lines changed
Expand file tree Collapse file tree 7 files changed +114
-3
lines changed Original file line number Diff line number Diff line change 1+ import type { PropsWithChildren } from "preact/compat" ;
2+ import { UserProvider } from "./context/user-context" ;
3+ import "./styles/styles.css" ;
4+
5+ export function App ( props : PropsWithChildren ) {
6+ return < UserProvider > { props . children } </ UserProvider > ;
7+ }
Original file line number Diff line number Diff line change @@ -14,6 +14,14 @@ export function Header() {
1414 Home
1515 </ Link >
1616 </ li >
17+ < li >
18+ < Link
19+ to = "/users"
20+ className = "text-white hover:text-indigo-200 transition-colors font-medium"
21+ >
22+ Users
23+ </ Link >
24+ </ li >
1725 < li >
1826 < Link
1927 to = "/about"
Original file line number Diff line number Diff line change 1+ import { createContext } from "preact" ;
2+ import type { PropsWithChildren } from "preact/compat" ;
3+ import { useContext , useState } from "preact/hooks" ;
4+
5+ type User = {
6+ id : string ;
7+ name : string ;
8+ age : number ;
9+ } ;
10+
11+ const userContext = createContext < {
12+ user : User | null ;
13+ setUser : ( u : User | null ) => void ;
14+ } > ( {
15+ user : null ,
16+ setUser ( ) { } ,
17+ } ) ;
18+
19+ export const UserProvider = ( props : PropsWithChildren ) => {
20+ const [ user , setUser ] = useState < User | null > ( {
21+ id : "1" ,
22+ age : 20 ,
23+ name : "Raul" ,
24+ } ) ;
25+
26+ return (
27+ < userContext . Provider
28+ value = { {
29+ setUser : ( u ) => {
30+ setUser ( u ) ;
31+ } ,
32+ user,
33+ } }
34+ >
35+ { props . children }
36+ </ userContext . Provider >
37+ ) ;
38+ } ;
39+
40+ export const useUserContext = ( ) => {
41+ const c = useContext ( userContext ) ;
42+ if ( ! c ) throw new Error ( "useUserContext should be inside of an UserProvider" ) ;
43+ return c ;
44+ } ;
Original file line number Diff line number Diff line change 11import { mount , StartApp } from "pranx/client" ;
2- import "./styles/styles.css " ;
2+ import { App } from "./App " ;
33
4- mount ( < StartApp /> , document . body ) ;
4+ mount (
5+ < App >
6+ < StartApp />
7+ </ App > ,
8+ document . body
9+ ) ;
Original file line number Diff line number Diff line change 11import type { ServerEntryProps } from "pranx" ;
22import { Meta , Scripts } from "pranx/server" ;
3+ import { App } from "./App" ;
34
45export default function ServerEntry ( { children } : ServerEntryProps ) {
56 return (
@@ -35,7 +36,7 @@ export default function ServerEntry({ children }: ServerEntryProps) {
3536 < Meta />
3637 </ head >
3738 < body class = "dark" >
38- { children }
39+ < App > { children } </ App >
3940 < Scripts />
4041 </ body >
4142 </ html >
Original file line number Diff line number Diff line change @@ -2,14 +2,28 @@ import { readFile } from "node:fs/promises";
22import { join } from "node:path" ;
33import type { GetStaticPropsFunction , InferStaticProps } from "pranx" ;
44import { Header } from "src/components/Header" ;
5+ import { useUserContext } from "src/context/user-context" ;
56import aboutStyles from "./about.module.css" ;
67
78export default function AboutPage ( props : InferStaticProps < typeof getStaticProps > ) {
9+ const { setUser } = useUserContext ( ) ;
810 return (
911 < div >
1012 < Header />
1113 < h1 className = { aboutStyles . title } > About Page</ h1 >
1214 < pre > { props . file_content } </ pre >
15+ < button
16+ type = "button"
17+ onClick = { ( ) => {
18+ setUser ( {
19+ id : "29" ,
20+ age : Math . trunc ( Math . random ( ) * 20 ) ,
21+ name : "Jose" ,
22+ } ) ;
23+ } }
24+ >
25+ Update info
26+ </ button >
1327 </ div >
1428 ) ;
1529}
Original file line number Diff line number Diff line change 1+ import { Header } from "src/components/Header" ;
2+ import { useUserContext } from "src/context/user-context" ;
3+
4+ export default function UsersPage ( ) {
5+ const { user, setUser } = useUserContext ( ) ;
6+
7+ return (
8+ < div className = "flex flex-col min-h-screen" >
9+ < Header />
10+
11+ < div class = "flex flex-col gap-4 p-6 max-w-3xl mx-auto" >
12+ < h1 > Users Page</ h1 >
13+ < span > ID: { user ?. id } </ span >
14+ < span > Name: { user ?. name } </ span >
15+ < span > Age: { user ?. age } </ span >
16+
17+ < button
18+ type = "button"
19+ onClick = { ( ) => {
20+ setUser ( {
21+ id : "29" ,
22+ age : Math . trunc ( Math . random ( ) * 20 ) ,
23+ name : "Jose" ,
24+ } ) ;
25+ } }
26+ >
27+ Update info
28+ </ button >
29+ </ div >
30+ </ div >
31+ ) ;
32+ }
You can’t perform that action at this time.
0 commit comments