Skip to content

Commit

Permalink
refactor: change axios call to fetch api
Browse files Browse the repository at this point in the history
  • Loading branch information
brunosllz committed Nov 14, 2023
1 parent c08fea8 commit a7770ff
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 96 deletions.
9 changes: 7 additions & 2 deletions src/actions/get-general-skills-itens.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { serverExternalApi } from '@/libs/axios'
import { getCurrentServerSession } from './get-current-user'
import { externalApi } from '@/libs/fetch-api'

export type GeneralSkills = {
id: string
Expand All @@ -20,14 +20,19 @@ export async function getGeneralSkillsItens({
}> {
const session = await getCurrentServerSession()

const { data } = await serverExternalApi.get<ResponseItens>(
const response = await externalApi(
`/projects/general-skills?search=${search ?? ''}`,
{
headers: {
Authorization: `Bearer ${session?.user.accessToken}`,
},
next: {
revalidate: 60 * 60 * 6, // 6 hours
},
},
)

const data: ResponseItens = await response.json()

return { generalSkills: data }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client'

import { useState } from 'react'
import { clientExternalApi } from '@/libs/axios'

import { Button } from '@/components/ui/button'
import { toast } from '@/components/ui/use-toast'
import { externalApi } from '@/libs/fetch-api'

type ManifestInterestButtonProps = {
projectId: string
Expand All @@ -19,7 +19,9 @@ export function ManifestInterestButton({

async function handleManifestInterest() {
try {
await clientExternalApi.post(`/projects/interest/in/${projectId}`)
await externalApi(`/projects/interest/in/${projectId}`, {
method: 'POST',
})

setHasInterest(true)
} catch (error) {
Expand All @@ -35,7 +37,6 @@ export function ManifestInterestButton({

return (
<Button
size="lg"
onClick={handleManifestInterest}
disabled={hasInterest || isInterestedInParticipate}
>
Expand Down
22 changes: 13 additions & 9 deletions src/app/(app)/projects/components/project-details.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import dayjs from 'dayjs'
import { getCurrentServerSession } from '@/actions/get-current-user'
import { serverExternalApi } from '@/libs/axios'
import { toast } from '@/components/ui/use-toast'

import Image from 'next/image'
Expand All @@ -13,6 +12,7 @@ import { Badge } from '@/components/ui/badge'
import { ProjectDetailsSkeleton } from './project-details-skeleton'
import { ProjectRoleTabs } from './project-role-tabs'
import { ManifestInterestButton } from './manifest-interest-button'
import { externalApi } from '@/libs/fetch-api'

type ProjectDetailsResponse = {
id: string
Expand Down Expand Up @@ -44,15 +44,19 @@ async function getProjectDetails(currentProjectId: string) {
const session = await getCurrentServerSession()

try {
const { data: projectDetails } =
await serverExternalApi.get<ProjectDetailsResponse>(
`/projects/from/${currentProjectId}/details`,
{
headers: {
Authorization: `Bearer ${session?.user.accessToken}`,
},
const response = await externalApi(
`/projects/from/${currentProjectId}/details`,
{
headers: {
Authorization: `Bearer ${session?.user.accessToken}`,
},
)
next: {
tags: ['projects', `${currentProjectId}`],
},
},
)

const projectDetails: ProjectDetailsResponse = await response.json()

return { projectDetails }
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,77 +1,6 @@
// import { Button } from '@/components/ui/button'
// import { ChevronLeft, ChevronRight } from 'lucide-react'

// type PaginationProps = {
// lastPage: number
// currentPage: number
// selectPage: (selectedPage: number) => void
// fetchNextPage: () => void
// fetchPreviousPage: () => void
// }

// export function Pagination({
// currentPage,
// lastPage,
// selectPage,
// fetchNextPage,
// fetchPreviousPage,
// }: PaginationProps) {
// function handleSelectPage(selectedPage: number) {
// selectPage(selectedPage)
// }

// function nextPage() {
// if (lastPage > currentPage) {
// return fetchNextPage()
// }
// }

// function previousPage() {
// if (currentPage > 1) {
// return fetchPreviousPage()
// }
// }

// return (
// <footer className="flex items-center space-x-[10px] rounded-md border p-5">
// <Button
// variant="ghost"
// size="icon"
// disabled={Number(currentPage) === 1}
// onClick={previousPage}
// >
// <ChevronLeft size={16} />
// </Button>

// {Array.from({ length: lastPage }).map((_, index) => {
// return (
// <Button
// key={index}
// onClick={() => handleSelectPage(index + 1)}
// data-current-index={index + 1 === Number(currentPage)}
// variant="ghost"
// size="icon"
// className="font-normal data-[current-index=true]:bg-accent"
// >
// {index + 1}
// </Button>
// )
// })}

// <Button
// variant="ghost"
// size="icon"
// onClick={nextPage}
// disabled={Number(currentPage) === lastPage}
// >
// <ChevronRight size={16} />
// </Button>
// </footer>
// )
// }

import { Button } from '@/components/ui/button'
import { DOTS, usePagination } from '@/hooks/use-pagination'

import { ChevronLeft, ChevronRight } from 'lucide-react'

type PaginationProps = {
Expand Down Expand Up @@ -117,7 +46,7 @@ export function Pagination({
<footer className="flex items-center space-x-[10px] rounded-md border p-5">
<Button
variant="ghost"
size="icon"
size="icon-sm"
disabled={Number(currentPage) === 1}
onClick={previousPage}
>
Expand All @@ -131,7 +60,7 @@ export function Pagination({
<Button
key={index}
variant="ghost"
size="icon"
size="icon-sm"
className="font-normal data-[current-index=true]:bg-accent"
>
{DOTS}
Expand All @@ -145,7 +74,7 @@ export function Pagination({
onClick={() => handleSelectPage(Number(page))}
data-current-index={page === Number(currentPage)}
variant="ghost"
size="icon"
size="icon-sm"
className="font-normal data-[current-index=true]:bg-accent"
>
{page}
Expand All @@ -155,7 +84,7 @@ export function Pagination({

<Button
variant="ghost"
size="icon"
size="icon-sm"
onClick={nextPage}
disabled={Number(currentPage) === lastPage}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import { useCallback, useEffect, useRef } from 'react'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
import { clientExternalApi } from '@/libs/axios'
import { useQuery } from '@tanstack/react-query'
import { PaginationResponseData } from '@/@types/pagination-response-data'
import { externalApi } from '@/libs/fetch-api'

import { ScrollArea } from '@/components/ui/scroll-area'
import { ProjectCardSkeleton } from './project-card-skeleton'
Expand Down Expand Up @@ -70,12 +70,15 @@ export function ProjectsList({

const mergedParams = mergeSearchParams(selectedProjectsSearchParams)

const { data } = await clientExternalApi.get(
const response = await externalApi(
`/projects/short/details?page=${useBoundStore.getState().currentPage}&${
selectedProjectsSearchParams.length && mergedParams
}`,
{ cache: 'no-store' },
)

const data = await response.json()

return data
},
staleTime: 1000 * 60 * 10, // 10 minutes
Expand Down Expand Up @@ -292,11 +295,11 @@ export function ProjectsList({

if (!projectsResponse) {
return (
<>
<div className="pr-6">
{Array.from({ length: 3 }).map((_, index) => (
<ProjectCardSkeleton key={index} />
))}
</>
</div>
)
}

Expand All @@ -305,11 +308,11 @@ export function ProjectsList({
return (
<>
{isLoading ? (
<>
<div className="pr-6">
{Array.from({ length: 3 }).map((_, index) => (
<ProjectCardSkeleton key={index} />
))}
</>
</div>
) : projects.length ? (
<ScrollArea
viewport={{
Expand Down

0 comments on commit a7770ff

Please sign in to comment.