Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added useTotalPlaces hook #47

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/places/[placeId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetchedMeta from 'fetch-meta-tags'
import type { fetchedMeta as FetchedMetadata } from 'fetch-meta-tags'
import { getPlaceData } from '../../../src/api/place'
import { getPlace } from '../../../src/api/place'
import { SafeImage } from '../../../src/views/safe-image'
import styles from './place.module.css'

Expand All @@ -13,7 +13,7 @@ type PlacePageProps = {
type PlaceMeta = FetchedMetadata | null

export default async function PlacePage({ params }: PlacePageProps) {
const place = await getPlaceData(params.placeId)
const place = await getPlace(params.placeId)

const placeMeta: PlaceMeta = place.site
? await fetchedMeta(place.site || '')
Expand Down
4 changes: 2 additions & 2 deletions app/places/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Link from 'next/link'
import { getCountryCodes } from '../../src/api/country'
import { getPlacesData } from '../../src/api/place'
import { getPlaces } from '../../src/api/place'
import styles from './places.module.css'

export const dynamic = 'force-dynamic'

export default async function PlacesPage() {
const [countryCodes, places] = await Promise.all([
getCountryCodes(),
getPlacesData()
getPlaces()
])

return places.map((place) => (
Expand Down
14 changes: 12 additions & 2 deletions src/api/place/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Place } from '../../../src/types/place'
import clientPromise from '../../config/mongodb'

export const getPlacesData = async (): Promise<Place[]> => {
export const getPlaces = async (): Promise<Place[]> => {
const mongoClient = await clientPromise
const data = await mongoClient
.db('ua-db')
Expand All @@ -14,7 +14,7 @@ export const getPlacesData = async (): Promise<Place[]> => {
return JSON.parse(JSON.stringify(data))
}

export const getPlaceData = async (placeId: string): Promise<Place> => {
export const getPlace = async (placeId: string): Promise<Place> => {
const mongoClient = await clientPromise
const data = await mongoClient
.db('ua-db')
Expand All @@ -23,3 +23,13 @@ export const getPlaceData = async (placeId: string): Promise<Place> => {

return JSON.parse(JSON.stringify(data))
}

export const totalPlaces = async (): Promise<number> => {
const mongoClient = await clientPromise
const data = await mongoClient
.db('ua-db')
.collection('ua-places')
.countDocuments()

return data
}
39 changes: 39 additions & 0 deletions src/hooks/useTotalPlaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use client'

import { useEffect, useState } from 'react'
import { totalPlaces } from '../api/place'

export type UseTotalPlacesHook = () => UseTotalPlacesHookResult

export type UseTotalPlacesHookResult = {
data: number
isLoading: boolean
error: boolean
}

export const useTotalPlaces: UseTotalPlacesHook = () => {
const [data, setData] = useState(0)
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState(false)

useEffect(() => {
const fetchTotalPlaces = async () => {
setIsLoading(true)

try {
// const total = await totalPlaces()
// setData(total)
setData(12)
} catch {
setError(true)
} finally {
setIsLoading(false)
}
}

fetchTotalPlaces()
return () => {}
}, [])

return { data, isLoading, error }
}