Skip to content

Commit

Permalink
refactor(form): ♻️ enhances form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyyxxyy committed Dec 12, 2023
1 parent d4d8f3f commit 1a8160a
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/pages/Checkout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export function Checkout() {
<Input
placeholder={t("zipCode")}
type="number"
disabled={!products.length}
minLength={i18n.language === "en" ? 5 : 8}
maxLength={i18n.language === "en" ? 9 : 8}
required
Expand All @@ -114,6 +115,7 @@ export function Checkout() {
type="text"
minLength={5}
required
disabled={!products.length}
onChange={(e) => setStreet(e.target.value)}
/>
<div className="flex flex-col items-center gap-3 md:flex-row">
Expand All @@ -123,13 +125,15 @@ export function Checkout() {
type="number"
min={0}
required
disabled={!products.length}
onChange={(e) => setNumber(e.target.value)}
/>
</div>
<div className="w-full">
<Input
placeholder={t("addressComplement")}
type="text"
disabled={!products.length}
optionalText={t("optionalFieldText")}
/>
</div>
Expand All @@ -140,6 +144,7 @@ export function Checkout() {
placeholder={t("neighbourhood")}
type="text"
required
disabled={!products.length}
onChange={(e) => setNeighborhood(e.target.value)}
/>
</div>
Expand All @@ -149,6 +154,7 @@ export function Checkout() {
placeholder={t("city")}
type="text"
required
disabled={!products.length}
onChange={(e) => setCity(e.target.value)}
/>
</div>
Expand All @@ -158,6 +164,7 @@ export function Checkout() {
type="text"
maxLength={2}
required
disabled={!products.length}
onChange={(e) => setState(e.target.value)}
/>
</div>
Expand Down Expand Up @@ -185,6 +192,7 @@ export function Checkout() {
type="button"
className="group max-w-[14rem] flex-grow outline-none"
onClick={() => setSelectedPaymentType("credit")}
disabled={!products.length}
>
<IconButton.Root
color="button"
Expand All @@ -205,6 +213,7 @@ export function Checkout() {
type="button"
className="group max-w-[14rem] flex-grow outline-none"
onClick={() => setSelectedPaymentType("debit")}
disabled={!products.length}
>
<IconButton.Root
color="button"
Expand All @@ -225,6 +234,7 @@ export function Checkout() {
type="button"
className="group max-w-[14rem] flex-grow outline-none"
onClick={() => setSelectedPaymentType("money")}
disabled={!products.length}
>
<IconButton.Root
color="button"
Expand Down Expand Up @@ -302,6 +312,11 @@ export function Checkout() {
if (selectedPaymentType === null) {
alert(t("selectPaymentType"));
event.preventDefault();
} else if (
!(city && neighborhood && number && state && street)
) {
alert(t("fillAllFieldsToConfirm"));
event.preventDefault();
} else {
handleConfirmOrder({
address: {
Expand All @@ -320,7 +335,7 @@ export function Checkout() {
</Link>
</>
) : (
<h3>Adicione produtos</h3>
<h3>{t("noProductsInCheckout")}</h3>
)}
</div>
</div>
Expand Down

0 comments on commit 1a8160a

Please sign in to comment.