-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/staging' into #386-Add-tags-comp…
…onent
- Loading branch information
Showing
34 changed files
with
991 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use client'; | ||
|
||
import React, { createContext, useContext } from 'react'; | ||
import { AxiosInstance } from 'axios'; | ||
import axiosInstanceDefault from './axiosInstance'; | ||
|
||
const AxiosContext = createContext<AxiosInstance | null>(null); | ||
|
||
export const useAxios = () => { | ||
const axiosInstance = useContext(AxiosContext); | ||
if (!axiosInstance) { | ||
throw new Error('useAxios must be used within an AxiosProvider'); | ||
} | ||
return axiosInstance; | ||
}; | ||
|
||
interface AxiosProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export const AxiosProvider = ({ children }: AxiosProviderProps) => { | ||
return <AxiosContext.Provider value={axiosInstanceDefault}>{children}</AxiosContext.Provider>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import axios from 'axios'; | ||
|
||
const axiosInstance = axios.create({ | ||
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
export default axiosInstance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL; | ||
|
||
if (!API_BASE_URL) { | ||
throw new Error('NEXT_PUBLIC_API_BASE_URL is not defined in the environment variables'); | ||
} | ||
|
||
const ENDPOINTS = { | ||
BLOGS: { | ||
GET_ALL: (limit: number, skip: number) => `${API_BASE_URL}/posts?limit=${limit}&skip=${skip}`, | ||
CREATE: `${API_BASE_URL}/posts`, | ||
}, | ||
USERS: { | ||
GET_ALL: `${API_BASE_URL}/users`, | ||
}, | ||
CONTACT: { | ||
SUBMIT: `${API_BASE_URL}/contact`, | ||
}, | ||
NEWSLETTER: { | ||
SUBSCRIBE: `${API_BASE_URL}/newsletter-subscribers`, | ||
}, | ||
}; | ||
|
||
export default ENDPOINTS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use client'; | ||
|
||
import { useMutation } from '@tanstack/react-query'; | ||
import { useAxios } from '../../AxiosProvider'; | ||
import ENDPOINTS from '../../endpoints'; | ||
|
||
type NewPost = { | ||
title: string; | ||
body: string; | ||
userId: number; | ||
}; | ||
|
||
const useAddNewPost = () => { | ||
const axios = useAxios(); | ||
|
||
return useMutation({ | ||
mutationFn: async (newPost: NewPost) => { | ||
const response = await axios.post(ENDPOINTS.BLOGS.CREATE, newPost); | ||
return response.data; | ||
}, | ||
}); | ||
}; | ||
|
||
export default useAddNewPost; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import ENDPOINTS from '../../endpoints'; | ||
import { useAxios } from '../../AxiosProvider'; | ||
|
||
export interface ContactFormData { | ||
name: string; | ||
email: string; | ||
message: string; | ||
cfTurnstileResponse: string; | ||
} | ||
|
||
export const useSubmitContactForm = () => { | ||
const axios = useAxios(); | ||
|
||
return useMutation({ | ||
mutationFn: async (formData: ContactFormData) => { | ||
const response = await axios.post(ENDPOINTS.CONTACT.SUBMIT, formData); | ||
return response.data.message; | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import ENDPOINTS from '../../endpoints'; | ||
import { useAxios } from '../../AxiosProvider'; | ||
|
||
export interface NewsletterFormData { | ||
first_name: string; | ||
email: string; | ||
'cf-turnstile-response': string; | ||
} | ||
|
||
export const useSubmitNewsletterForm = () => { | ||
const axios = useAxios(); | ||
|
||
return useMutation({ | ||
mutationFn: async (formData: NewsletterFormData) => { | ||
const response = await axios.post(ENDPOINTS.NEWSLETTER.SUBSCRIBE, formData); | ||
return response.data.message; | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import fetchBlogPosts from '../../../app/action'; | ||
import QUERY_KEYS from '../../queryKeys'; | ||
|
||
interface GetBlogsParams { | ||
pageTitle: string; | ||
blogCardsNumber: number; | ||
} | ||
|
||
const useGetBlogs = ({ pageTitle, blogCardsNumber }: GetBlogsParams) => { | ||
return useQuery({ | ||
queryKey: [...QUERY_KEYS.BLOGS.ALL, pageTitle, blogCardsNumber], | ||
queryFn: () => fetchBlogPosts(0, blogCardsNumber), | ||
}); | ||
}; | ||
|
||
export default useGetBlogs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useInfiniteQuery } from '@tanstack/react-query'; | ||
import QUERY_KEYS from '../../queryKeys'; | ||
import fetchBlogPosts from '../../../app/action'; | ||
import { BlogCardProps } from '../../../components/reusable-components/blog-card/BlogCard'; | ||
|
||
interface GetInfiniteBlogsParams { | ||
pageTitle: string; | ||
blogCardsNumber: number; | ||
} | ||
const useGetInfiniteBlogs = ({ pageTitle, blogCardsNumber }: GetInfiniteBlogsParams) => { | ||
return useInfiniteQuery< | ||
BlogCardProps[], | ||
Error, | ||
BlogCardProps[], | ||
[string, string, string, string], | ||
number | ||
>({ | ||
queryKey: [ | ||
QUERY_KEYS.BLOGS.INFINITE[0], | ||
QUERY_KEYS.BLOGS.INFINITE[1], | ||
pageTitle, | ||
blogCardsNumber.toString(), | ||
], | ||
queryFn: ({ pageParam }) => fetchBlogPosts(pageParam, blogCardsNumber), | ||
initialPageParam: 0, | ||
getNextPageParam: (lastPage, allPages) => { | ||
return lastPage.length === blogCardsNumber ? allPages.length * blogCardsNumber : undefined; | ||
}, | ||
}); | ||
}; | ||
|
||
export default useGetInfiniteBlogs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useAxios } from '../../AxiosProvider'; | ||
import ENDPOINTS from '../../endpoints'; | ||
import QUERY_KEYS from '../../queryKeys'; | ||
|
||
const useGetUsers = () => { | ||
const axios = useAxios(); | ||
|
||
return { | ||
queryKey: QUERY_KEYS.USERS.ALL, | ||
queryFn: async () => { | ||
const { data } = await axios.get(ENDPOINTS.USERS.GET_ALL); | ||
return data; | ||
}, | ||
}; | ||
}; | ||
|
||
export default useGetUsers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const QUERY_KEYS = { | ||
BLOGS: { | ||
ALL: ['blogPosts'], | ||
INFINITE: ['infiniteBlogPosts', 'infinite'] as const, | ||
}, | ||
USERS: { | ||
ALL: ['users'], | ||
}, | ||
} as const; | ||
|
||
export default QUERY_KEYS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.