-
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.
- Loading branch information
1 parent
8bc42fd
commit f9dbd72
Showing
11 changed files
with
186 additions
and
26 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
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,9 @@ | ||
'use client'; | ||
import { store } from '@/redux/store'; | ||
import { Provider } from 'react-redux'; | ||
|
||
const Providers = ({ children }: { children: React.ReactNode }) => { | ||
return <Provider store={store}>{children}</Provider>; | ||
}; | ||
|
||
export default Providers; |
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,18 @@ | ||
import { tagTypes } from '../tag-types'; | ||
import { baseApi } from './baseApi'; | ||
const AUTH_URL = '/auth'; | ||
|
||
export const authApi = baseApi.injectEndpoints({ | ||
endpoints: build => ({ | ||
userLogin: build.mutation({ | ||
query: loginData => ({ | ||
url: `${AUTH_URL}/login`, | ||
method: 'POST', | ||
data: loginData, | ||
}), | ||
invalidatesTags: [tagTypes.user], | ||
}), | ||
}), | ||
}); | ||
|
||
export const { useUserLoginMutation } = authApi; |
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,12 @@ | ||
import { axiosBaseQuery } from '@/helpers/axios/axiosBaseQuery'; | ||
import { getBaseUrl } from '@/helpers/config/envConfig'; | ||
import { createApi } from '@reduxjs/toolkit/query/react'; | ||
import { tagTypesList } from '../tag-types'; | ||
|
||
// Define a service using a base URL and expected endpoints | ||
export const baseApi = createApi({ | ||
reducerPath: 'api', | ||
baseQuery: axiosBaseQuery({ baseUrl: getBaseUrl() }), | ||
endpoints: () => ({}), | ||
tagTypes: tagTypesList, | ||
}); |
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,69 @@ | ||
import { ICourse, IMeta } from '@/types'; | ||
import { baseApi } from './baseApi'; | ||
import { tagTypes } from '../tag-types'; | ||
|
||
const COURSE_URL = '/courses'; | ||
|
||
export const courseApi = baseApi.injectEndpoints({ | ||
endpoints: build => ({ | ||
// get all | ||
courses: build.query({ | ||
query: (arg: Record<string, any>) => { | ||
return { | ||
url: COURSE_URL, | ||
method: 'GET', | ||
params: arg, | ||
}; | ||
}, | ||
transformResponse: (response: ICourse[], meta: IMeta) => { | ||
return { | ||
courses: response, | ||
meta, | ||
}; | ||
}, | ||
providesTags: [tagTypes.course], | ||
}), | ||
// get single | ||
course: build.query({ | ||
query: (id: string) => ({ | ||
url: `${COURSE_URL}/${id}`, | ||
method: 'GET', | ||
}), | ||
providesTags: [tagTypes.course], | ||
}), | ||
// create | ||
addCourse: build.mutation({ | ||
query: data => ({ | ||
url: COURSE_URL, | ||
method: 'POST', | ||
data, | ||
}), | ||
invalidatesTags: [tagTypes.course], | ||
}), | ||
// update | ||
updateCourse: build.mutation({ | ||
query: data => ({ | ||
url: `${COURSE_URL}/${data.id}`, | ||
method: 'PATCH', | ||
data: data.body, | ||
}), | ||
invalidatesTags: [tagTypes.course], | ||
}), | ||
// delete | ||
deleteCourse: build.mutation({ | ||
query: id => ({ | ||
url: `${COURSE_URL}/${id}`, | ||
method: 'DELETE', | ||
}), | ||
invalidatesTags: [tagTypes.course], | ||
}), | ||
}), | ||
}); | ||
|
||
export const { | ||
useCoursesQuery, | ||
useCourseQuery, | ||
useAddCourseMutation, | ||
useDeleteCourseMutation, | ||
useUpdateCourseMutation, | ||
} = courseApi; |
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,28 @@ | ||
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux"; | ||
import type { RootState, AppDispatch } from "./store"; | ||
import { useEffect, useState } from "react"; | ||
|
||
// Use throughout your app instead of plain `useDispatch` and `useSelector` | ||
export const useAppDispatch: () => AppDispatch = useDispatch; | ||
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector; | ||
|
||
interface IDebounced { | ||
searchQuery: string; | ||
delay: number; | ||
} | ||
|
||
export const useDebounced = ({ searchQuery, delay }: IDebounced) => { | ||
const [debouncedValue, setDebouncedValue] = useState<string>(searchQuery); | ||
|
||
useEffect(() => { | ||
const handler = setTimeout(() => { | ||
setDebouncedValue(searchQuery); | ||
}, delay); | ||
|
||
return () => { | ||
clearTimeout(handler); | ||
}; | ||
}, [searchQuery, delay]); | ||
|
||
return debouncedValue; | ||
}; |
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,6 @@ | ||
import { baseApi } from "./api/baseApi"; | ||
|
||
export const reducer = { | ||
[baseApi.reducerPath]: baseApi.reducer, | ||
} | ||
|
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,16 @@ | ||
import { baseApi } from './api/baseApi'; | ||
import { reducer } from './rootReducer'; | ||
import { configureStore } from '@reduxjs/toolkit' | ||
|
||
export const store = configureStore({ | ||
reducer, | ||
middleware: (getDefaultMiddleware) => | ||
getDefaultMiddleware().concat(baseApi.middleware), | ||
}) | ||
|
||
|
||
|
||
// Infer the `RootState` and `AppDispatch` types from the store itself | ||
export type RootState = ReturnType<typeof store.getState> | ||
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState} | ||
export type AppDispatch = typeof store.dispatch |
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,6 @@ | ||
export enum tagTypes { | ||
course = 'course', | ||
user = 'user', | ||
} | ||
|
||
export const tagTypesList = [tagTypes.course, tagTypes.user]; |
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,6 @@ | ||
import * as yup from "yup"; | ||
|
||
export const loginSchema = yup.object().shape({ | ||
id: yup.string().required("UserId is required"), | ||
password: yup.string().min(6).max(32).required(), | ||
}); |