Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions app/routes/contacts.$contactId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,29 @@ import type { FunctionComponent } from "react"
import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/node"
import { json } from "@remix-run/node"
import type { ContactRecord } from "../data"
import { getContact, updateContact } from "../data"
import { getContact as dbGetContact, updateContact } from "../data"
import invariant from "tiny-invariant"
import { fromSuccess, makeDomainFunction } from "domain-functions"
import z from "zod"

export const loader = async ({ params }: LoaderFunctionArgs) => {
invariant(params.contactId, "Missing contactId param")
const contact = await getContact(params.contactId)
const getContactSchema = z.object({
contactId: z.string(),
})

const getContactDF = makeDomainFunction(getContactSchema)(async ({
contactId,
}) => {
const contact = await dbGetContact(contactId)
if (!contact) {
throw new Response("Not Found", { status: 404 })
}
return contact
})

export const loader = async ({ params }: LoaderFunctionArgs) => {
const contact = await fromSuccess(getContactDF)({
contactId: params.contactId,
})
return json({ contact })
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
"@remix-run/node": "^2",
"@remix-run/react": "^2",
"@remix-run/serve": "^2",
"domain-functions": "^2.4.0",
"isbot": "^3.6.13",
"match-sorter": "^6.3.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sort-by": "^1.2.0",
"tiny-invariant": "^1.3.1"
"tiny-invariant": "^1.3.1",
"zod": "^3.22.4"
},
"devDependencies": {
"@remix-run/dev": "^2",
Expand Down
Loading