Skip to content

Commit

Permalink
agrega data persistente para las direcciones
Browse files Browse the repository at this point in the history
  • Loading branch information
Lostovayne committed Oct 29, 2024
1 parent 685f88f commit 01743a6
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 186 deletions.
32 changes: 32 additions & 0 deletions prisma/migrations/20241028235939_user_address/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- CreateTable
CREATE TABLE "Country" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,

CONSTRAINT "Country_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "UserAddress" (
"id" TEXT NOT NULL,
"firstName" TEXT NOT NULL,
"lastName" TEXT NOT NULL,
"address" TEXT NOT NULL,
"address2" TEXT NOT NULL,
"postalCode" TEXT NOT NULL,
"phone" TEXT NOT NULL,
"city" TEXT NOT NULL,
"countryId" TEXT NOT NULL,
"userId" TEXT NOT NULL,

CONSTRAINT "UserAddress_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "UserAddress_userId_key" ON "UserAddress"("userId");

-- AddForeignKey
ALTER TABLE "UserAddress" ADD CONSTRAINT "UserAddress_countryId_fkey" FOREIGN KEY ("countryId") REFERENCES "Country"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UserAddress" ADD CONSTRAINT "UserAddress_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
34 changes: 26 additions & 8 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ generator client {
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
relationMode = "prisma"
provider = "postgresql"
url = env("DATABASE_URL")
}

enum Size {
Expand Down Expand Up @@ -64,16 +63,35 @@ model ProductImage {
}

model User {
id String @id @default(uuid())
id String @id @default(uuid())
name String
email String @unique
email String @unique
emailVerified DateTime?
password String
role Role @default(user)
role Role @default(user)
image String?
Address UserAddress?
}

model Country {
id String @id
name String
id String @id
name String
Address UserAddress[]
}

model UserAddress {
id String @id @default(uuid())
firstName String
lastName String
address String
address2 String
postalCode String
phone String
city String
// Relaciones
country Country @relation(fields: [countryId], references: [id])
countryId String
user User @relation(fields: [userId], references: [id])
userId String @unique
}
12 changes: 12 additions & 0 deletions src/app/(shop)/checkout/address/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { getCountries } from '@/actions/country/get-countries';
import { Title } from '@/components';
import { Country } from '@/interfaces/country.interface';
import type { Viewport } from 'next';
import AddressForm from './ui/AddressForm';

export function generateViewport({ width, height }: Viewport) {
return {
width,
height,
deviceScaleFactor: 1,
isMobile: true,
hasTouch: false,
isLandscape: true,
};
}

export default async function AdressPage() {
const countries: Country[] = (await getCountries()) || [];

Expand Down
22 changes: 18 additions & 4 deletions src/app/(shop)/checkout/address/ui/AddressForm.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use client';

import { Country } from '@/interfaces/country.interface';
import { useAddressStore } from '@/store/address/address.store';
import clsx from 'clsx';
import type { FC } from 'react';
import { useEffect, type FC } from 'react';
import { useForm } from 'react-hook-form';

interface AddressFormProps {
Expand All @@ -25,14 +26,27 @@ const AddressForm: FC<AddressFormProps> = ({ countries }) => {
handleSubmit,
register,
formState: { isValid },
reset,
} = useForm<FormInput>({
defaultValues: {
// Todo : Obtener datos de la cuenta
// Obtener datos
...useAddressStore.getState().address,
},
});

const setAddress = useAddressStore((state) => state.setAddress);
const address = useAddressStore((state) => state.address);

useEffect(() => {
if (address.firstName) {
reset(address);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const onSubmit = (data: FormInput) => {
console.log({ data });
console.log(data);
setAddress(data);
};
return (
<form
Expand Down Expand Up @@ -97,7 +111,7 @@ const AddressForm: FC<AddressFormProps> = ({ countries }) => {
<select
className='p-2 border rounded-md bg-gray-300/20'
{...register('country', { required: true })}>
<option value=''>[ Seleccione ]</option>
<option value=''>Seleccione</option>
{countries.map((country) => (
<option
key={country.id}
Expand Down
Loading

0 comments on commit 01743a6

Please sign in to comment.