Skip to content

Commit

Permalink
update category page
Browse files Browse the repository at this point in the history
  • Loading branch information
QuocAnh189 committed Oct 15, 2024
1 parent e698c89 commit df9a440
Show file tree
Hide file tree
Showing 29 changed files with 569 additions and 194 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
API_URL = 'https://localhost:8000/api'
API_URL =
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const nextConfig = {
images: {
domains: ['res.cloudinary.com'],
formats: ['image/webp', 'image/avif'],
remotePatterns: [
{
protocol: 'https',
Expand Down
102 changes: 97 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"lint": "next lint"
},
"dependencies": {
"@hookform/resolvers": "^3.9.0",
"@reduxjs/toolkit": "^2.2.7",
"daisyui": "^4.12.13",
"flatpickr": "^4.6.13",
"jsvectormap": "^1.6.0",
"next": "14.2.11",
Expand All @@ -20,9 +22,12 @@
"react": "^18",
"react-apexcharts": "^1.4.1",
"react-dom": "^18",
"react-hook-form": "^7.53.0",
"react-redux": "^9.1.2",
"react-toastify": "^10.0.6",
"sass": "^1.79.1",
"sharp": "^0.33.5"
"sharp": "^0.33.5",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
8 changes: 8 additions & 0 deletions src/app/constant/regex.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const PASSWORD_REGEX = {
upperCase: new RegExp('.*[A-Z].*'),
lowerCase: new RegExp('.*[a-z].*'),
number: new RegExp('.*\\d.*'),
specialCharacter: new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*')
}

export const PHONE_REGEX = new RegExp('^[+]?[(]?[0-9]{3}[)]?[-s.]?[0-9]{3}[-s.]?[0-9]{4,6}$')
33 changes: 20 additions & 13 deletions src/app/redux/apis/category.api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// 'use client'
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'

//type
import { CreateCategoryPayload } from '@shared/interfaces/category/payload'
// //type
// import { CreateCategoryPayload } from '@shared/interfaces/category/payload'

//interface
import { ICategory } from '@shared/interfaces/category/model'
import { IListData } from '@shared/interfaces/common'
import { IGetParam } from '@shared/interfaces/common/index'

export const apiCategory = createApi({
reducerPath: 'apiCategory',
Expand All @@ -24,7 +27,7 @@ export const apiCategory = createApi({
keepUnusedDataFor: 20,
tagTypes: ['Category'],
endpoints: (builder) => ({
createCategory: builder.mutation<ICategory, CreateCategoryPayload>({
createCategory: builder.mutation<ICategory, FormData>({
query: (data) => ({
url: '/categories',
method: 'POST',
Expand All @@ -33,38 +36,42 @@ export const apiCategory = createApi({
invalidatesTags: ['Category']
}),

getCategories: builder.query<ICategory[], void>({
query: () => ({
getCategories: builder.query<IListData<ICategory[]>, IGetParam>({
query: (params) => ({
url: '/categories',
method: 'GET'
method: 'GET',
params
}),
providesTags: ['Category'],
transformResponse: (response: any) => response.data.items
transformResponse: (response: any) => response.data
}),

getCategoryById: builder.query<ICategory, string>({
query: (categoryId) => ({
url: `/categories/${categoryId}`,
method: 'GET'
}),
providesTags: ['Category']
providesTags: ['Category'],
transformResponse: (response: any) => response.data
}),

updateCategory: builder.mutation<ICategory, Partial<ICategory>>({
query: (data) => ({
url: `/categories/${data.id}`,
updateCategory: builder.mutation<ICategory, { id: string; data: FormData }>({
query: ({ id, data }) => ({
url: `/categories/${id}`,
method: 'PUT',
body: data
}),
invalidatesTags: ['Category']
invalidatesTags: ['Category'],
transformResponse: (response: any) => response.data
}),

deleteCategory: builder.mutation<any, string>({
query: (categoryId) => ({
url: `/categories/${categoryId}`,
method: 'DELETE'
}),
invalidatesTags: ['Category']
invalidatesTags: ['Category'],
transformResponse: (response: any) => response.data
})
})
})
Expand Down
43 changes: 43 additions & 0 deletions src/app/redux/slices/category.slice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit'

//interface
import { ICategory } from '@shared/interfaces/category/model'

export const CategorySliceKey = 'category'

type InitialType = {
categories: ICategory[]
}

const initialState = {
categories: []
} as InitialType

const categorySlice = createSlice({
name: CategorySliceKey,
initialState,
reducers: {
setCategories: (state, action: PayloadAction<ICategory[]>) => {
state.categories = action.payload
},

addCategories: (state, action: PayloadAction<ICategory>) => {
state.categories.push(action.payload)
},

updateCategories: (state, action: PayloadAction<ICategory>) => {
state.categories = state.categories.filter((category) =>
category.id === action.payload.id ? action.payload : category
)
},

removeCategories: (state, action: PayloadAction<ICategory>) => {
state.categories = state.categories.filter((category) => category.id !== action.payload.id)
}
}
})

export const { setCategories } = categorySlice.actions

const categoryReducer = categorySlice.reducer
export default categoryReducer
Loading

0 comments on commit df9a440

Please sign in to comment.