Skip to content

Commit

Permalink
Call EnsureRegion then EnsureSite
Browse files Browse the repository at this point in the history
  • Loading branch information
jadehopepunk committed Jan 12, 2025
1 parent a9e3787 commit 0b61fd3
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 136 deletions.
6 changes: 3 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { ChakraProvider } from "@chakra-ui/react"
import { ColorModeProvider } from "./components/ui/color-mode"
import { InstalledApps } from "./contexts/apps"
import { themeSystem } from "./theme"
import { ThisSite, ThisNode } from "./contexts/this_site"
import { ThisRegion } from "./contexts/this_region"
import { EnsureSite, ThisNode } from "./contexts/this_site"
import { EnsureRegion } from "./contexts/this_region"

const router = createBrowserRouter(
[
{
path: "/",
element: <Layout />,
children: [
{ path: "", element: <ThisRegion /> },
{ path: "", element: <EnsureRegion><EnsureSite /></EnsureRegion> },
{ path: "node", element: <ThisNode /> },
{ path: "apps", element: <InstalledApps /> },
],
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/contexts/this_region/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default as ThisRegion } from "./pages/ThisRegion"
export { default as EnsureRegion } from "./pages/EnsureRegion"
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getRegion = async (): Promise<RegionDetails | null> => {
return null
}

export default function ThisRegion() {
export default function EnsureRegion({ children }: { children: React.ReactNode }) {
const [region, setRegion] = useState<RegionDetails | null>(null)
const [loading, setLoading] = useState(true)

Expand Down Expand Up @@ -64,6 +64,7 @@ export default function ThisRegion() {
{region == null && (
<FindRegion onSubmitNewRegion={onSubmitNewRegion} />
)}
{region != null && children}
</Container>
)
}
2 changes: 1 addition & 1 deletion frontend/src/contexts/this_site/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as ThisSite } from "./pages/ThisSite"
export { default as EnsureSite } from "./pages/EnsureSite"
export { default as ThisNode } from "./pages/ThisNode"
69 changes: 69 additions & 0 deletions frontend/src/contexts/this_site/pages/EnsureSite.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Center, Container, Spinner } from "@chakra-ui/react"
import { useEffect, useState } from "react"
import NewSite, { NewSiteData } from "../components/NewSite"
import { SiteDetails } from "../types"
import ThisSiteApi from "../api"
import { ApiResult } from "../../shared/types"

const api = new ThisSiteApi()

const getSite = async (): Promise<SiteDetails | null> => {
const result = await api.show()
if ("Ok" in result) return result.Ok
return null
}

export default function EnsureSite() {
const [site, setSite] = useState<SiteDetails | null>(null)
const [loading, setLoading] = useState(true)

const updateSite = (newSite: SiteDetails | null) => {
console.log("Updating site", newSite)
setSite(newSite)
}

const withLoading = async (fn: () => Promise<void>) => {
setLoading(true)
await fn()
setLoading(false)
}

const fetchSite = async () => {
withLoading(async () => {
console.log("EFFECT: fetchSite")
const newSite = await getSite()
updateSite(newSite) })
}

useEffect(() => {
if (site == null) fetchSite()
}, [])

const onSubmitNewSite = (data: NewSiteData) => {
api.create(data.name).then((result: ApiResult<SiteDetails, any>) => {
if ("Ok" in result) updateSite(result.Ok)
})
}

if (loading) {
return (
<Container maxWidth={"2xl"}>
<Center>
<Spinner size="lg" opacity={0.5} />
</Center>
</Container>
)
}

return (
<Container maxWidth={"2xl"}>
{site == null && <NewSite onSubmitNewSite={onSubmitNewSite} />}
{site != null && (
<div>
<h1>Site created!</h1>
<p>Site: {site?.name}</p>
</div>
)}
</Container>
)
}
130 changes: 0 additions & 130 deletions frontend/src/contexts/this_site/pages/ThisSite.tsx

This file was deleted.

0 comments on commit 0b61fd3

Please sign in to comment.