Skip to content

Commit

Permalink
Redux Course Slice added (#138)
Browse files Browse the repository at this point in the history
* Redux | Course Slice #16: Update routing constants

* Remove unused imports in dashboard.tsx

* Redux | Course Slice #16: Add COURSE route to ServerPath class

* renamed authActionSlice to authSlice

* Update bootcamp endpoint in store/slice/bootcamp/endpoint.ts

* Redux | Course Slice #16: Add courseSlice endpoint and export it

* Add course endpoint to store slice

* Add coursePersistConfig to root-reducer.ts

* Add invalidatesTags property to EndpointBuilder interface

* set interface to EndpointBuilder

* fixed the url for deleteCourse endpoint

---------

Co-authored-by: triple iii <[email protected]>
  • Loading branch information
ballyalley-o and in-a-iii authored Apr 16, 2024
1 parent 1592634 commit 07c8d4f
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/constant/routing/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ enum ROUTING {
ROOT = '/',
ID = ':id',
DASHBOARD = 'dashboard',
// @bootcamp
// :bootcamp
BOOTCAMP = 'bootcamp',

// @auth
// :course
COURSE = 'course',
// :auth
AUTH = 'auth',
LOG_IN = 'log-in',
LOG_OUT = 'log-out',
Expand Down
4 changes: 1 addition & 3 deletions src/page/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useLocation } from 'react-router-dom'
import { Fragment, useEffect } from 'react'
import { Fragment } from 'react'
import { DashboardHero, BootcampConsult, DashboardBootcampRundown, DashboardFeedbackSection } from 'section/dashboard'
import { snack } from 'hook'

const Dashboard = () => {
return (
Expand Down
2 changes: 2 additions & 0 deletions src/route/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class ServerPath {
static SERVER = GLOBAL.APP_SERVER
static BOOTCAMP = ROUTING.BOOTCAMP
static BOOTCAMP_ID = (bootcampId: string) => conNex(ROUTING.BOOTCAMP, bootcampId)
static COURSE = ROUTING.COURSE
static COURSE_ID = (courseId: string) => conNex(ROUTING.COURSE, courseId)
static AUTH_LOG_IN = conNex(ROUTING.AUTH, ROUTING.LOG_IN)
static AUTH_LOG_OUT = conNex(ROUTING.AUTH, ROUTING.LOG_OUT)
static AUTH_REGISTER = conNex(ROUTING.AUTH, ROUTING.REGISTER)
Expand Down
7 changes: 7 additions & 0 deletions src/store/root-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export const bootcampPersistConfig = {
blacklist: ['error', 'initial', 'responseMessage']
}

export const coursePersistConfig = {
key: 'course',
storage,
keyPrefix: KEY.PERSIST_PREFIX,
blacklist: ['error', 'initial', 'responseMessage']
}

const rootReducer = combineReducers({
[apiSlice.reducerPath]: persistReducer(apiPersistConfig, apiSlice.reducer),
auth: persistReducer(authPersistConfig, authReducer)
Expand Down
4 changes: 2 additions & 2 deletions src/store/slice/auth/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getAuthToken } from 'auth/utility'

const { GET, POST } = METHOD

export const authActionSlice = apiSlice.injectEndpoints({
export const authSlice = apiSlice.injectEndpoints({
endpoints: (builder: EndpointBuilder) => ({
login: builder.mutation({
query: (data: any) => ({
Expand Down Expand Up @@ -40,4 +40,4 @@ export const authActionSlice = apiSlice.injectEndpoints({
})
})

export const { useLoginMutation, useLogoutMutation, useRegisterMutation, useAccountQuery } = authActionSlice
export const { useLoginMutation, useLogoutMutation, useRegisterMutation, useAccountQuery } = authSlice
44 changes: 36 additions & 8 deletions src/store/slice/bootcamp/endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
import { apiSlice } from '../api'
import { apiSlice } from 'store/slice/api'
import { ServerPath } from 'route/path'
import { METHOD } from 'constant'

export const bootcampApiSlice = apiSlice.injectEndpoints({
endpoints: (builder) => ({
getAllBootcamp: builder.query<any, void>({
const { GET, POST, PUT, DELETE } = METHOD

export const bootcampSlice = apiSlice.injectEndpoints({
endpoints: (builder: EndpointBuilder) => ({
getAllBootcamp: builder.query({
query: () => ({
url: ServerPath.BOOTCAMP
url: ServerPath.BOOTCAMP,
method: GET
}),
keepUnusedDataFor: 5
}),
getBootcamp: builder.query<any, string>({
getBootcamp: builder.query({
query: (id) => ({
url: ServerPath.BOOTCAMP_ID(id)
url: ServerPath.BOOTCAMP_ID(id),
method: GET
})
}),
createBootcamp: builder.mutation({
query: (data: any) => ({
url: ServerPath.BOOTCAMP,
method: POST,
body: data
}),
invalidatesTags: ['Bootcamp']
}),
updateBootcamp: builder.mutation({
query: (data: any) => ({
url: ServerPath.BOOTCAMP_ID(data.id),
method: PUT,
body: data
}),
invalidatesTags: ['Bootcamp']
}),
deleteBootcamp: builder.mutation({
query: (id: string) => ({
url: ServerPath.BOOTCAMP_ID(id),
method: DELETE
})
})
})
})

export const { useGetAllBootcampQuery, useGetBootcampQuery } = bootcampApiSlice
export const { useGetAllBootcampQuery, useGetBootcampQuery, useCreateBootcampMutation, useUpdateBootcampMutation, useDeleteBootcampMutation } =
bootcampSlice
45 changes: 45 additions & 0 deletions src/store/slice/course/endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { apiSlice } from 'store/slice/api'
import { ServerPath } from 'route/path'
import { METHOD } from 'constant'

const { GET, POST, PUT, DELETE } = METHOD

export const courseSlice = apiSlice.injectEndpoints({
endpoints: (builder: EndpointBuilder) => ({
getAllCourse: builder.query({
query: () => ({
url: ServerPath.COURSE,
method: GET
}),
keepUnusedDataFor: 5
}),
getCourse: builder.query({
query: (id) => ({
url: ServerPath.COURSE_ID(id),
method: GET
})
}),
createCourse: builder.mutation({
query: (data: any) => ({
url: ServerPath.COURSE,
method: POST,
body: data
})
}),
updateCourse: builder.mutation({
query: (data: any) => ({
url: ServerPath.COURSE_ID(data.id),
method: PUT,
body: data
})
}),
deleteCourse: builder.mutation({
query: (id: string) => ({
url: ServerPath.COURSE_ID(id),
method: DELETE
})
})
})
})

export const { useGetAllCourseMutation, useGetCourseQuery, useCreateCourseMutation, useUpdateCourseMutation, useDeleteCourseMutation } = courseSlice
1 change: 1 addition & 0 deletions src/store/slice/course/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './endpoint'
1 change: 1 addition & 0 deletions src/store/slice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as authReducer } from './auth/auth'
export * from './bootcamp/endpoint'
export * from './auth/endpoint'
export * from './user/endpoint'
export * from './course/endpoint'
2 changes: 1 addition & 1 deletion src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ declare global {
BadgeProps?: object
sx?: object
}


interface EndpointBuilder {
/**
Expand All @@ -117,6 +116,7 @@ declare global {
query: ((data: any) => { url: string; method?: METHOD; body: any }) | ((ids: any) => { url: string; method: METHOD })
providesTags?: any
keepUnusedDataFor?: number
invalidatesTags?: any[]
}) => any
}

Expand Down

0 comments on commit 07c8d4f

Please sign in to comment.