File tree 7 files changed +75
-23
lines changed
7 files changed +75
-23
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ const CountryList = ({ data }: { data: Country[] }) => {
31
31
32
32
return (
33
33
< >
34
- < div className = "flex flex-wrap md:flex-nowrap justify-between py -10 gap-5" >
34
+ < div className = "flex flex-wrap md:flex-nowrap justify-between pb -10 gap-5" >
35
35
< SearchCountry search = { search } setSearch = { setSearch } />
36
36
37
37
< RegionSelect
Original file line number Diff line number Diff line change 1
1
import Link from "next/link" ;
2
- import { GlobeAsiaAustraliaIcon } from "@heroicons/react/24/outline" ;
3
2
import ThemeToggle from "./ThemeToggle" ;
3
+ import { GlobeAsiaAustraliaIcon } from "@heroicons/react/24/outline" ;
4
4
import { SignedIn , SignedOut , SignInButton } from "@clerk/nextjs" ;
5
5
import { SignOutButton } from "./SignOutButton" ;
6
6
import { getUser } from "@/lib/user" ;
7
7
import { auth } from "@clerk/nextjs/server" ;
8
8
9
9
const NavBar = async ( ) => {
10
10
const { userId } = auth ( ) ;
11
-
11
+
12
12
if ( userId ) await getUser ( ) ;
13
13
14
14
return (
Original file line number Diff line number Diff line change
1
+ import { getCountry } from "@/lib/countries" ;
2
+ import { getFavouriteCountries } from "@/lib/user" ;
3
+ import { SignedIn , SignedOut } from "@clerk/nextjs" ;
4
+ import { auth } from "@clerk/nextjs/server" ;
5
+ import CountryCard from "../components/CountryCard" ;
6
+
7
+ const FavouriteCountriesPage = async ( ) => {
8
+ const { userId } = auth ( ) ;
9
+
10
+ let favouriteCountries = [ ] ;
11
+
12
+ if ( userId ) {
13
+ const { countries = [ ] , error } = await getFavouriteCountries ( ) ;
14
+
15
+ if ( error ) return < div className = "px-4 md:container mt-10" > { error } </ div > ;
16
+
17
+ if ( countries . length > 0 ) {
18
+ favouriteCountries = await Promise . all (
19
+ countries . map ( async ( country ) => {
20
+ const { data } = await getCountry ( country . country ) ;
21
+ return data ;
22
+ } )
23
+ ) ;
24
+ }
25
+ }
26
+
27
+ return (
28
+ < div className = "px-4 md:container mt-10" >
29
+ < h1 className = "mb-10 text-lg font-semibold" > Favourite Countries</ h1 >
30
+ < SignedOut >
31
+ < p > Please sign in to see your favourite countries</ p >
32
+ </ SignedOut >
33
+ < SignedIn >
34
+ < ul className = "grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-8" >
35
+ { favouriteCountries . map ( ( country ) => (
36
+ < li key = { country . cca3 } >
37
+ < CountryCard country = { country } />
38
+ </ li >
39
+ ) ) }
40
+ </ ul >
41
+ </ SignedIn >
42
+ </ div >
43
+ ) ;
44
+ } ;
45
+
46
+ export default FavouriteCountriesPage ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -11,10 +11,10 @@ export default async function Home() {
11
11
) ;
12
12
13
13
return (
14
- < main className = "px-4 md:container mb -10" >
14
+ < main className = "px-4 md:container my -10" >
15
15
{ error && < p > { error } </ p > }
16
16
17
- < CountryList data = { countriesInAlphabeticalOrder } />
17
+ { ! error && < CountryList data = { countriesInAlphabeticalOrder } /> }
18
18
< ScrollButton />
19
19
</ main >
20
20
) ;
Original file line number Diff line number Diff line change @@ -24,3 +24,18 @@ export async function getUser() {
24
24
console . error ( error ) ;
25
25
}
26
26
}
27
+
28
+ export async function getFavouriteCountries ( ) {
29
+ try {
30
+ const user = await getUser ( ) ;
31
+ if ( ! user ) return { error : "User not found" } ;
32
+
33
+ const countries = await prisma . favouriteCountry . findMany ( {
34
+ where : { userId : user . id } ,
35
+ } ) ;
36
+
37
+ return { countries } ;
38
+ } catch ( error ) {
39
+ return { error : "Error fetching data" } ;
40
+ }
41
+ }
Original file line number Diff line number Diff line change @@ -17,5 +17,13 @@ model User {
17
17
id Int @id @default (autoincrement () )
18
18
name String ?
19
19
email String @unique
20
- favCountries String []
20
+ favoriteCountries FavouriteCountry []
21
21
}
22
+
23
+ model FavouriteCountry {
24
+ id Int @id @default (autoincrement () )
25
+ userId Int
26
+ country String
27
+ user User @relation (fields : [userId ] , references : [id ] )
28
+ }
29
+
You can’t perform that action at this time.
0 commit comments