Skip to content

Commit

Permalink
Refactor for server action
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Nov 18, 2024
1 parent 46e7b42 commit b0c3b8c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
31 changes: 8 additions & 23 deletions src/components/nodes/pages/sup-book/precart/precart.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import useIsInternational from "@lib/hooks/useIsInternational"
import Button from "@components/elements/button"
import {FormEvent, useEffect, useState} from "react"
import {useEffect, useState} from "react"
import {ArrowRightIcon} from "@heroicons/react/16/solid"
import {Maybe, PressPrice} from "@lib/gql/__generated__/drupal.d"
import {BookOpenIcon as BookOpenIconOutline} from "@heroicons/react/24/outline"
import {BookOpenIcon} from "@heroicons/react/24/solid"
import {useRouter} from "next/navigation"
import {formatCurrency} from "@lib/utils/format-currency"
import {twMerge} from "tailwind-merge"
import {clsx} from "clsx"
import {submitForm} from "@components/nodes/pages/sup-book/precart/precart.server"

type PriceProps = PressPrice & {}

Expand All @@ -32,31 +32,16 @@ const PrecartClient = ({priceId, clothIsbn, paperIsbn, hasIntlCart, bookTitle}:
.catch(_e => console.warn(`Something went wrong fetching ${priceId}`))
}, [priceId])

const router = useRouter()

const [isIntl, setIntl] = useIsInternational()

const onFormSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
// @ts-expect-error Format exists in the form data.
const isbn = e.target.format.value

if (isIntl) {
const title = bookTitle.replace(/[^a-zA-Z\d\s:]/, "").replace(/ /, "-")
router.push(`https://www.combinedacademic.co.uk/${isbn}/${title}`)
return
}
router.push(`https://add-to-cart-2.supadu.com/add-to-cart?isbn=${isbn}&client=indiepubs-stanford-university-press`)
}

return (
<form
className="rs-mb-1 rs-pb-1 border-b-2 border-fog @container"
onSubmit={onFormSubmit}
action="/add-to-cart"
method="post"
>
/* eslint-disable-next-line @typescript-eslint/no-misused-promises */
<form className="rs-mb-1 rs-pb-1 border-b-2 border-fog @container" action={submitForm}>
<input type="hidden" name="title" value={bookTitle} />
<label className="sr-only">
Do not fill with any information
<input name="email" type="email" />
</label>

<fieldset className="rs-mb-1">
<legend className="sr-only">Format</legend>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {NextRequest} from "next/server"
"use server"

import {redirect} from "next/navigation"

export const revalidate = false
export const dynamic = "force-static"
export const submitForm = async (formData: FormData) => {
"use server"

// Honeypot field.
if (formData.get("email")) return

export const POST = async (request: NextRequest) => {
const formData = await request.formData()
const bookTitle = formData.get("title") as string
const formatIsbn = formData.get("format") as string
const isIntl = formData.get("intl") !== "us"
Expand Down

0 comments on commit b0c3b8c

Please sign in to comment.