-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from brookslybrand/refactor
Refactor code to send less data and revalidate data properly
- Loading branch information
Showing
6 changed files
with
100 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,23 @@ | ||
import { Link, useLocation, useLoaderData } from "@remix-run/react" | ||
import { LoaderFunctionArgs, json } from "@remix-run/node"; | ||
|
||
export const loader = async ({ | ||
params, | ||
}: LoaderFunctionArgs) => { | ||
const lang = params.lang | ||
return json({ lang }); | ||
}; | ||
import { Link, useLocation, useParams } from "@remix-run/react"; | ||
import { getLang } from "~/utils"; | ||
|
||
export default function Header() { | ||
let { lang } = useLoaderData<typeof loader>(); | ||
console.log("LANG: ", lang); | ||
|
||
const location = useLocation(); | ||
const locationEntries = Object.entries(location) | ||
const pathname = locationEntries[0] | ||
const enPathname = pathname[1].includes("/ja") ? pathname[1].replace("/ja", "") : pathname[1] | ||
const { pathname } = useLocation(); | ||
const params = useParams(); | ||
const lang = getLang(params); | ||
|
||
return ( | ||
<div id="header"> | ||
<h1>{lang === "ja" ? `Optional Segments デモ` : `Optional Segments Example`}</h1> | ||
<h1> | ||
{lang === "ja" ? `Optional Segments デモ` : `Optional Segments Example`} | ||
</h1> | ||
<nav> | ||
<Link to={`${enPathname}`}>🇺🇸</Link> | ||
<Link to={`${lang = "ja"}${location.pathname}`}>🇯🇵</Link> | ||
{lang === "ja" ? ( | ||
<Link to={pathname.replace(/^\/ja/, "")}>🇺🇸</Link> | ||
) : ( | ||
<Link to={`/ja${pathname}`}>🇯🇵</Link> | ||
)} | ||
</nav> | ||
</div> | ||
) | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { LoaderFunctionArgs, json } from "@remix-run/node"; | ||
import { Link, Outlet, useLoaderData } from "@remix-run/react"; | ||
import Header from "~/components/Header"; | ||
import { getContacts } from "~/data"; | ||
import { getLang } from "~/utils"; | ||
|
||
export const loader = async ({ params }: LoaderFunctionArgs) => { | ||
const fullContact = await getContacts(); | ||
const lang = getLang(params); | ||
|
||
const contacts = fullContact.map((contact) => ({ | ||
id: contact.id, | ||
name: `${contact.details?.[lang]?.first} ${contact.details?.[lang]?.last}`, | ||
})); | ||
|
||
return json({ contacts, lang }); | ||
}; | ||
|
||
export default function ContactsLayout() { | ||
const { contacts, lang } = useLoaderData<typeof loader>(); | ||
|
||
return ( | ||
<> | ||
<Header /> | ||
<div id="wrapper"> | ||
<div id="sidebar"> | ||
<h1>{lang === "ja" ? `Remix コンタクト` : `Remix Contacts`}</h1> | ||
<nav> | ||
{contacts.length ? ( | ||
<ul> | ||
{contacts.map(({ id, name }) => { | ||
return ( | ||
<li key={id}> | ||
<Link to={`${id}`}>{name}</Link> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
) : ( | ||
<p> | ||
<i>No contacts</i> | ||
</p> | ||
)} | ||
</nav> | ||
</div> | ||
<div id="detail"> | ||
<Outlet /> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { redirect } from "@remix-run/node"; | ||
|
||
export function loader() { | ||
return redirect("/contacts"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Params } from "@remix-run/react"; | ||
|
||
export function getLang(params: Params<string>) { | ||
const lang = params.lang ?? "en"; | ||
if (lang !== "ja" && lang !== "en") { | ||
throw new Error(`Invalid language ${lang}`); | ||
} | ||
return lang; | ||
} |