File tree Expand file tree Collapse file tree 6 files changed +296
-186
lines changed
migrations/20241028235939_user_address
app/(shop)/checkout/address Expand file tree Collapse file tree 6 files changed +296
-186
lines changed Original file line number Diff line number Diff line change 1+ -- CreateTable
2+ CREATE TABLE "Country " (
3+ " id" TEXT NOT NULL ,
4+ " name" TEXT NOT NULL ,
5+
6+ CONSTRAINT " Country_pkey" PRIMARY KEY (" id" )
7+ );
8+
9+ -- CreateTable
10+ CREATE TABLE "UserAddress " (
11+ " id" TEXT NOT NULL ,
12+ " firstName" TEXT NOT NULL ,
13+ " lastName" TEXT NOT NULL ,
14+ " address" TEXT NOT NULL ,
15+ " address2" TEXT NOT NULL ,
16+ " postalCode" TEXT NOT NULL ,
17+ " phone" TEXT NOT NULL ,
18+ " city" TEXT NOT NULL ,
19+ " countryId" TEXT NOT NULL ,
20+ " userId" TEXT NOT NULL ,
21+
22+ CONSTRAINT " UserAddress_pkey" PRIMARY KEY (" id" )
23+ );
24+
25+ -- CreateIndex
26+ CREATE UNIQUE INDEX "UserAddress_userId_key " ON " UserAddress" (" userId" );
27+
28+ -- AddForeignKey
29+ ALTER TABLE " UserAddress" ADD CONSTRAINT " UserAddress_countryId_fkey" FOREIGN KEY (" countryId" ) REFERENCES " Country" (" id" ) ON DELETE RESTRICT ON UPDATE CASCADE;
30+
31+ -- AddForeignKey
32+ ALTER TABLE " UserAddress" ADD CONSTRAINT " UserAddress_userId_fkey" FOREIGN KEY (" userId" ) REFERENCES " User" (" id" ) ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change @@ -6,9 +6,8 @@ generator client {
66}
77
88datasource db {
9- provider = " postgresql "
10- url = env (" DATABASE_URL " )
11- relationMode = " prisma "
9+ provider = " postgresql "
10+ url = env (" DATABASE_URL " )
1211}
1312
1413enum Size {
@@ -64,16 +63,35 @@ model ProductImage {
6463}
6564
6665model User {
67- id String @id @default (uuid () )
66+ id String @id @default (uuid () )
6867 name String
69- email String @unique
68+ email String @unique
7069 emailVerified DateTime ?
7170 password String
72- role Role @default (user )
71+ role Role @default (user )
7372 image String ?
73+ Address UserAddress ?
7474}
7575
7676model Country {
77- id String @id
78- name String
77+ id String @id
78+ name String
79+ Address UserAddress []
80+ }
81+
82+ model UserAddress {
83+ id String @id @default (uuid () )
84+ firstName String
85+ lastName String
86+ address String
87+ address2 String
88+ postalCode String
89+ phone String
90+ city String
91+ // Relaciones
92+ country Country @relation (fields : [countryId ] , references : [id ] )
93+ countryId String
94+
95+ user User @relation (fields : [userId ] , references : [id ] )
96+ userId String @unique
7997}
Original file line number Diff line number Diff line change 11import { getCountries } from '@/actions/country/get-countries' ;
22import { Title } from '@/components' ;
33import { Country } from '@/interfaces/country.interface' ;
4+ import type { Viewport } from 'next' ;
45import AddressForm from './ui/AddressForm' ;
56
7+ export function generateViewport ( { width, height } : Viewport ) {
8+ return {
9+ width,
10+ height,
11+ deviceScaleFactor : 1 ,
12+ isMobile : true ,
13+ hasTouch : false ,
14+ isLandscape : true ,
15+ } ;
16+ }
17+
618export default async function AdressPage ( ) {
719 const countries : Country [ ] = ( await getCountries ( ) ) || [ ] ;
820
Original file line number Diff line number Diff line change 11'use client' ;
22
33import { Country } from '@/interfaces/country.interface' ;
4+ import { useAddressStore } from '@/store/address/address.store' ;
45import clsx from 'clsx' ;
5- import type { FC } from 'react' ;
6+ import { useEffect , type FC } from 'react' ;
67import { useForm } from 'react-hook-form' ;
78
89interface AddressFormProps {
@@ -25,14 +26,27 @@ const AddressForm: FC<AddressFormProps> = ({ countries }) => {
2526 handleSubmit,
2627 register,
2728 formState : { isValid } ,
29+ reset,
2830 } = useForm < FormInput > ( {
2931 defaultValues : {
30- // Todo : Obtener datos de la cuenta
32+ // Obtener datos
33+ ...useAddressStore . getState ( ) . address ,
3134 } ,
3235 } ) ;
3336
37+ const setAddress = useAddressStore ( ( state ) => state . setAddress ) ;
38+ const address = useAddressStore ( ( state ) => state . address ) ;
39+
40+ useEffect ( ( ) => {
41+ if ( address . firstName ) {
42+ reset ( address ) ;
43+ }
44+ // eslint-disable-next-line react-hooks/exhaustive-deps
45+ } , [ ] ) ;
46+
3447 const onSubmit = ( data : FormInput ) => {
35- console . log ( { data } ) ;
48+ console . log ( data ) ;
49+ setAddress ( data ) ;
3650 } ;
3751 return (
3852 < form
@@ -97,7 +111,7 @@ const AddressForm: FC<AddressFormProps> = ({ countries }) => {
97111 < select
98112 className = 'p-2 border rounded-md bg-gray-300/20'
99113 { ...register ( 'country' , { required : true } ) } >
100- < option value = '' > [ Seleccione ] </ option >
114+ < option value = '' > Seleccione</ option >
101115 { countries . map ( ( country ) => (
102116 < option
103117 key = { country . id }
You can’t perform that action at this time.
0 commit comments