Skip to content

Commit

Permalink
use query inside navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Jan 2, 2024
1 parent c21ced7 commit b11c5f1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export class NotFoundError extends Error {}
27 changes: 20 additions & 7 deletions src/pages/index/index.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { Route } from "@tanstack/react-router"
import rootRoute from "../root"
import { useQuery } from "@tanstack/react-query";
import { queryOptions } from "@tanstack/react-query";
import { MapsList } from "./MapsList";
import mapApi from "../../api";

export function IndexPage() {
const fetchPortal = async () => {
const map = await mapApi
.get(`portals/${window.PORTAL_KEY}/`)

if (!map) {
throw new NotFoundError(`Portal not found!`)
}

return map
}

const { isPending, error, data } = useQuery({
queryKey: ['portal'],
queryFn: () =>
mapApi.get(`portals/${window.PORTAL_KEY}/`),
})
const portalOptions = () => queryOptions({
queryKey: ['portals'],
queryFn: fetchPortal,
})

export function IndexPage() {
// const query = useSuspenseQuery(portalOptions())

return (
<div className="wrapper">
Expand All @@ -25,4 +36,6 @@ export const indexRoute = new Route({
getParentRoute: () => rootRoute,
component: IndexPage,
path: '/',
loader: ({ context: { queryClient }}) =>
queryClient.ensureQueryData(portalOptions())
})
6 changes: 2 additions & 4 deletions src/pages/viewer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import Map from "./components/Map";
import MapContextProvider from "./components/MapContextProvider";
import Metadata from "./components/Metadata";
import { queryOptions, useSuspenseQuery } from "@tanstack/react-query";
import axios from "axios";
import mapApi from "../../api";

class NotFoundError extends Error {}
import { NotFoundError } from "../../lib/utils";

const fetchMap = async (mapSlug) => {
const map = await mapApi
Expand Down Expand Up @@ -58,7 +56,7 @@ export function Viewer() {
<div id="app-wrap" style={{ display: 'flex' }}>
<div id="sidebar">
<Metadata {...map.data} />
<Layers data={map.data.layers} />
<Layers layers={map.data.layers} />
</div>
<Map {...map.data} />
</div>
Expand Down

0 comments on commit b11c5f1

Please sign in to comment.