import { Link, Route, Switch, useAtomValue } from"jarl-react";
import { asyncArticleRoute, asyncLookupDemoRoute, asyncLookupSlugRoute } from"../router/routes";
import { articleSlugs } from"./asyncArticles";
constMISSING_SLUG = "no-such-article";
constArticleIndex = () => (
<div><h3>Articles</h3><ul>
{articleSlugs().map((slug) => (
<likey={slug}><Linkroute={asyncLookupSlugRoute}to={{slug }}>
{slug}
</Link></li>
))}
<li><Linkroute={asyncLookupSlugRoute}to={{slug:MISSING_SLUG }}>
{MISSING_SLUG}
</Link>{" "}
<span>— not in the database, so this address 404s</span></li></ul></div>
);
constArticleNotFound = () => (
<div><h3>Not found</h3><p>The database has no article at this address, so the route never matched.</p><p><Linkroute={asyncLookupDemoRoute}to={{}}>
Back to all articles
</Link></p></div>
);
/**
* Demo of a route whose existence only a database can answer: `asyncArticleRoute` matches only
* when the lookup finds something, and binds what it found to the route's own `values`, so the
* page below renders `article` without fetching anything itself. The same settled lookup decides
* the server's 404 status - see `notFoundAtom` in router/routes.ts.
*/exportconstAsyncLookupApp = () => {
const pending = useAtomValue(asyncArticleRoute.pending);
return (
<><nav><Linkroute={asyncLookupDemoRoute}to={{}}exact>
All articles
</Link></nav>
{pending ? (
<p>Looking the slug up…</p>
) : (
<Switchfallback={<ArticleNotFound />}>
<Routeon={asyncLookupDemoRoute}exact><ArticleIndex /></Route><Routeon={asyncArticleRoute}exact>
{({ article }) => (
<article><h3>{article.title}</h3><p><em>by {article.author}</em></p><p>{article.body}</p></article>
)}
</Route></Switch>
)}
</>
);
};
exportdefaultAsyncLookupApp;