2023-12-12 18:21:09 +08:00
|
|
|
import React from 'react'
|
2023-12-13 15:17:14 +08:00
|
|
|
import {Validation} from "./validate.js";
|
2023-12-12 18:21:09 +08:00
|
|
|
import {useShopStore} from "./shop_store";
|
2023-12-13 12:39:15 +08:00
|
|
|
import {ShowJSON} from "./ShowJSON";
|
2023-12-14 16:29:32 +08:00
|
|
|
|
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
import {useRenderCount} from "@uidotdev/usehooks";
|
|
|
|
|
2023-11-30 17:26:17 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Components that renders the form to request quote.
|
|
|
|
*/
|
2023-12-12 18:21:09 +08:00
|
|
|
export function OrderForm() {
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
const renderCount = useRenderCount();
|
|
|
|
|
|
|
|
const email = useShopStore((state) => state.email);
|
|
|
|
const note = useShopStore((state) => state.note);
|
|
|
|
const isProcessing = useShopStore((state) => state.isProcessing);
|
|
|
|
const updateEmail = useShopStore((state) => state.updateEmail);
|
|
|
|
const updateNote = useShopStore((state) => state.updateNote);
|
|
|
|
const submitForm = useShopStore((state) => state.submitForm);
|
|
|
|
const submitDisabled = useShopStore((state) => state.submitDisabled);
|
|
|
|
const resetEmailValidation = useShopStore((state) => state.resetEmailValidation);
|
2023-12-12 18:21:09 +08:00
|
|
|
|
2023-12-14 16:29:32 +08:00
|
|
|
// #!render_count
|
2023-12-14 16:09:33 +08:00
|
|
|
console.log("OrderForm renders: ", renderCount)
|
2023-12-12 18:21:09 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="summary-form">
|
|
|
|
|
|
|
|
<form onSubmit={submitForm} noValidate>
|
|
|
|
|
|
|
|
<input
|
|
|
|
className={`${email.error > 0 ? 'errorField' : ''}`}
|
|
|
|
type="email"
|
|
|
|
placeholder="Email"
|
2023-12-13 15:17:14 +08:00
|
|
|
onFocus={resetEmailValidation}
|
2023-12-12 18:21:09 +08:00
|
|
|
onChange={(event) => updateEmail(event.target.value)}
|
|
|
|
onBlur={(event) => updateEmail(event.target.value)}
|
|
|
|
value={email.value}/>
|
|
|
|
|
|
|
|
{email.error === Validation.Empty ? (
|
|
|
|
<div className="error">
|
|
|
|
<small>Required</small>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-12 18:21:09 +08:00
|
|
|
{email.error === Validation.Invalid ? (
|
|
|
|
<div className="error">
|
|
|
|
<small>Your email is incomplete</small>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-12 18:21:09 +08:00
|
|
|
<textarea
|
|
|
|
onChange={(event) => updateNote(event.target.value)}
|
|
|
|
defaultValue={note.value}
|
|
|
|
rows="5"
|
|
|
|
placeholder="Additional notes"/>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-12 18:21:09 +08:00
|
|
|
<div className="d-flex flex-column flex-sm-row justify-content-between">
|
2023-12-13 12:39:15 +08:00
|
|
|
<ShowJSON/>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2024-01-08 17:42:02 +08:00
|
|
|
<input className="btn btn-primary w-100 m-0 ms-sm-2 order-form-submit" type="button"
|
2023-12-13 12:39:15 +08:00
|
|
|
disabled={submitDisabled()}
|
2024-01-08 17:42:02 +08:00
|
|
|
onClick={submitForm}
|
2023-12-12 18:21:09 +08:00
|
|
|
value={`${isProcessing ? 'Processing ...' : 'Request quote'}`}/>
|
|
|
|
</div>
|
|
|
|
{/*This will open an email window. Send the email to make your request.*/}
|
|
|
|
</form>
|
2023-11-30 17:26:17 +08:00
|
|
|
|
2023-12-12 18:21:09 +08:00
|
|
|
</div>
|
|
|
|
);
|
2023-11-30 17:26:17 +08:00
|
|
|
|
|
|
|
}
|