-
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.
- Combine Redux Reducers in Every Reducers File to Make The File Cont…
…ains One Reducer(Like allCategories and category Reducers Becomes One Reducer Which is category) 1) Refactor The Main itemReducer Shared File 2) Refactor and Reduce Redux Types in Redux Types Folder 3) Refactor and Reduce Redux Actions in Redux Actions Folder 4) Refactor rootReducer.js File 5) Edit The Reflected Changes Because The Previous Refactoring in The Whole Project - Enable Loading on Mounting The Pages That Uses useGetAllItems Hook
- Loading branch information
1 parent
0f0d7c4
commit 2659c13
Showing
32 changed files
with
214 additions
and
235 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
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
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
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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import { ALL_BRANDS_ENABLE_LOADING, BRAND_ENABLE_LOADING, CREATE_BRAND, CREATE_BRAND_ERROR, GET_ALL_BRANDS, GET_ALL_BRANDS_ERROR } from "../types/brandTypes"; | ||
import { BRAND_ENABLE_LOADING, BRAND_ERROR, CREATE_BRAND, GET_ALL_BRANDS } from "../types/brandTypes"; | ||
import createFormDataItem from "./utils/createFormDataItem"; | ||
import getAllItems from "./utils/getAllItems"; | ||
|
||
|
||
export const allBrandsEnableLoading = () => ({ type: ALL_BRANDS_ENABLE_LOADING }); | ||
|
||
export const getAllBrands = getAllItems({ url: "/api/v1/brands", GET_ALL_ITEMS: GET_ALL_BRANDS, GET_ALL_ITEMS_ERROR: GET_ALL_BRANDS_ERROR,allItemsEnableLoadingAction:allBrandsEnableLoading }); | ||
|
||
|
||
export const brandEnableLoading = () => ({ type: BRAND_ENABLE_LOADING }); | ||
|
||
export const createBrand = createFormDataItem({ url: "/api/v1/brands", CREATE_ITEM: CREATE_BRAND, CREATE_ITEM_ERROR: CREATE_BRAND_ERROR,itemEnableLoadingAction:brandEnableLoading }); | ||
export const getAllBrands = getAllItems({ url: "/api/v1/brands", GET_ALL_ITEMS: GET_ALL_BRANDS, ITEM_ERROR: BRAND_ERROR, itemEnableLoading: brandEnableLoading }); | ||
|
||
export const createBrand = createFormDataItem({ url: "/api/v1/brands", CREATE_ITEM: CREATE_BRAND, ITEM_ERROR: BRAND_ERROR, itemEnableLoading: brandEnableLoading }); | ||
|
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 |
---|---|---|
@@ -1,18 +1,12 @@ | ||
import { CREATE_CATEGORY, CREATE_CATEGORY_ERROR, ALL_CATEGORIES_ENABLE_LOADING, CATEGORY_ENABLE_LOADING, GET_ALL_CATEGORIES, GET_ALL_CATEGORIES_ERROR } from "../types/categoryTypes"; | ||
import { CATEGORY_ENABLE_LOADING, CATEGORY_ERROR, CREATE_CATEGORY, GET_ALL_CATEGORIES } from "../types/categoryTypes"; | ||
import createFormDataItem from "./utils/createFormDataItem"; | ||
import getAllItems from "./utils/getAllItems"; | ||
|
||
|
||
|
||
|
||
export const allCategoriesEnableLoading = () => ({ type: ALL_CATEGORIES_ENABLE_LOADING }); | ||
|
||
|
||
export const getAllCategories = getAllItems({ url: "/api/v1/categories", GET_ALL_ITEMS: GET_ALL_CATEGORIES, GET_ALL_ITEMS_ERROR: GET_ALL_CATEGORIES_ERROR, allItemsEnableLoadingAction: allCategoriesEnableLoading }); | ||
|
||
|
||
|
||
export const categoryEnableLoading = () => ({ type: CATEGORY_ENABLE_LOADING }); | ||
|
||
export const createCategory = createFormDataItem({ url: "/api/v1/categories", CREATE_ITEM: CREATE_CATEGORY, CREATE_ITEM_ERROR: CREATE_CATEGORY_ERROR ,itemEnableLoadingAction:categoryEnableLoading}); | ||
export const getAllCategories = getAllItems({ url: "/api/v1/categories", GET_ALL_ITEMS: GET_ALL_CATEGORIES, ITEM_ERROR: CATEGORY_ERROR, itemEnableLoading: categoryEnableLoading }); | ||
|
||
export const createCategory = createFormDataItem({ url: "/api/v1/categories", CREATE_ITEM: CREATE_CATEGORY, ITEM_ERROR: CATEGORY_ERROR, itemEnableLoading: categoryEnableLoading }); | ||
|
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 |
---|---|---|
@@ -1,18 +1,11 @@ | ||
import { ALL_PRODUCTS_ENABLE_LOADING, CREATE_PRODUCT, CREATE_PRODUCT_ERROR, GET_ALL_PRODUCTS, GET_ALL_PRODUCTS_ERROR, PRODUCT_ENABLE_LOADING } from "../types/productTypes"; | ||
import { CREATE_PRODUCT, GET_ALL_PRODUCTS, PRODUCT_ENABLE_LOADING, PRODUCT_ERROR } from "../types/productTypes"; | ||
import createFormDataItem from "./utils/createFormDataItem"; | ||
import getAllItems from "./utils/getAllItems"; | ||
|
||
|
||
|
||
|
||
export const allProductsEnableLoading = () => ({ type: ALL_PRODUCTS_ENABLE_LOADING }); | ||
|
||
|
||
export const getAllProducts = getAllItems({ url: "/api/v1/products", GET_ALL_ITEMS: GET_ALL_PRODUCTS, GET_ALL_ITEMS_ERROR: GET_ALL_PRODUCTS_ERROR, allItemsEnableLoadingAction: allProductsEnableLoading }); | ||
|
||
|
||
|
||
export const productEnableLoading = () => ({ type: PRODUCT_ENABLE_LOADING }); | ||
|
||
export const createProduct = createFormDataItem({ url: "/api/v1/products", CREATE_ITEM: CREATE_PRODUCT, CREATE_ITEM_ERROR: CREATE_PRODUCT_ERROR ,itemEnableLoadingAction:productEnableLoading}); | ||
export const getAllProducts = getAllItems({ url: "/api/v1/products", GET_ALL_ITEMS: GET_ALL_PRODUCTS, ITEM_ERROR: PRODUCT_ERROR, itemEnableLoading: productEnableLoading }); | ||
|
||
export const createProduct = createFormDataItem({ url: "/api/v1/products", CREATE_ITEM: CREATE_PRODUCT, ITEM_ERROR: PRODUCT_ERROR, itemEnableLoading: productEnableLoading }); | ||
|
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 |
---|---|---|
@@ -1,19 +1,13 @@ | ||
import { CREATE_SUB_CATEGORY, CREATE_SUB_CATEGORY_ERROR, ALL_SUB_CATEGORIES_ENABLE_LOADING, SUB_CATEGORY_ENABLE_LOADING, GET_ALL_SUB_CATEGORIES, GET_ALL_SUB_CATEGORIES_ERROR } from "../types/subCategoryTypes"; | ||
import { CREATE_SUB_CATEGORY, GET_ALL_SUB_CATEGORIES, SUB_CATEGORY_ENABLE_LOADING, SUB_CATEGORY_ERROR } from "../types/subCategoryTypes"; | ||
import createRawDataItem from "./utils/createRawDataItem"; | ||
import getAllItems from "./utils/getAllItems"; | ||
|
||
|
||
|
||
export const subCategoryEnableLoading = () => ({ type: SUB_CATEGORY_ENABLE_LOADING }); | ||
|
||
export const allSubCategoriesEnableLoading = () => ({ type: ALL_SUB_CATEGORIES_ENABLE_LOADING }); | ||
export const getAllSubCategories = getAllItems({ url: "/api/v1/subcategories", GET_ALL_ITEMS: GET_ALL_SUB_CATEGORIES, ITEM_ERROR: SUB_CATEGORY_ERROR, itemEnableLoading: subCategoryEnableLoading }); | ||
|
||
export const createSubCategory = createRawDataItem({ url: "/api/v1/subcategories", CREATE_ITEM: CREATE_SUB_CATEGORY, ITEM_ERROR: SUB_CATEGORY_ERROR, itemEnableLoading: subCategoryEnableLoading }); | ||
|
||
export const getAllSubCategories = getAllItems({ url: "/api/v1/subcategories", GET_ALL_ITEMS: GET_ALL_SUB_CATEGORIES, GET_ALL_ITEMS_ERROR: GET_ALL_SUB_CATEGORIES_ERROR, allItemsEnableLoadingAction: allSubCategoriesEnableLoading }); | ||
|
||
|
||
|
||
export const createSubCategory = createRawDataItem({ url: "/api/v1/subcategories", CREATE_ITEM: CREATE_SUB_CATEGORY, CREATE_ITEM_ERROR: CREATE_SUB_CATEGORY_ERROR }); | ||
|
||
|
||
|
||
export const subCategoryEnableLoading = () => ({ type: SUB_CATEGORY_ENABLE_LOADING }); |
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 |
---|---|---|
@@ -1,44 +1,44 @@ | ||
import baseURL from "../../../api/baseURL"; | ||
import { notify } from "../../../components/utils/ActionMessageContainer"; | ||
|
||
const createFormDataItem = ({url,CREATE_ITEM,CREATE_ITEM_ERROR,itemEnableLoadingAction})=>(formData)=>async(dispatch)=>{ | ||
const controller = new AbortController() | ||
const id = notify({message:"جاري الحفظ 0%",type:"loading",data:{action:()=>controller.abort()}}) | ||
|
||
controller.signal.onabort = ()=>{ | ||
setTimeout(()=>{ | ||
notify({dismissId:id}) | ||
notify({message:"تم الإلغاء",type:"warning"}) | ||
},1000) | ||
} | ||
const createFormDataItem = ({ url, CREATE_ITEM, ITEM_ERROR, itemEnableLoading }) => (formData) => async (dispatch) => { | ||
const controller = new AbortController(); | ||
const id = notify({ message: "جاري الحفظ 0%", type: "loading", data: { action: () => controller.abort() } }); | ||
|
||
controller.signal.onabort = () => { | ||
setTimeout(() => { | ||
notify({ dismissId: id }); | ||
notify({ message: "تم الإلغاء", type: "warning" }); | ||
}, 1000); | ||
}; | ||
|
||
try { | ||
const config = { | ||
signal:controller.signal, | ||
onUploadProgress: function(progressEvent) { | ||
const percentCompleted = Math.round((progressEvent.progress*100)) | ||
notify({message:`جاري الحفظ ${percentCompleted}%`,type:"loading",id,progress:progressEvent.progress}) | ||
signal: controller.signal, | ||
onUploadProgress: function (progressEvent) { | ||
const percentCompleted = Math.round((progressEvent.progress * 100)); | ||
notify({ message: `جاري الحفظ ${percentCompleted}%`, type: "loading", id, progress: progressEvent.progress }); | ||
} | ||
} | ||
}; | ||
|
||
dispatch(itemEnableLoadingAction()) | ||
const {data} = await baseURL.postForm(url,formData,config) | ||
dispatch(itemEnableLoading()); | ||
const { data } = await baseURL.postForm(url, formData, config); | ||
|
||
notify({message:"تم الحفظ بنجاح",type:"success",id}) | ||
notify({ message: "تم الحفظ بنجاح", type: "success", id }); | ||
|
||
return dispatch({ | ||
type:CREATE_ITEM, | ||
payload:data | ||
}) | ||
type: CREATE_ITEM, | ||
payload: data | ||
}); | ||
|
||
} catch ({ response, message }) { | ||
|
||
notify({message:"حدث خطأ أثناء الحفظ",type:"error",id}) | ||
notify({ message: "حدث خطأ أثناء الحفظ", type: "error", id }); | ||
return dispatch({ | ||
type: CREATE_ITEM_ERROR, | ||
type: ITEM_ERROR, | ||
error: `Error: ${response ? response.data?.message : message}` | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
export default createFormDataItem; |
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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
import baseURL from "../../../api/baseURL"; | ||
import { notify } from "../../../components/utils/ActionMessageContainer"; | ||
|
||
const createRawDataItem = ({url,CREATE_ITEM,CREATE_ITEM_ERROR})=>(rawData)=>async(dispatch)=>{ | ||
const id = notify({message:"جاري الحفظ",type:"loading"}) | ||
const createRawDataItem = ({ url, CREATE_ITEM, ITEM_ERROR }) => (rawData) => async (dispatch) => { | ||
const id = notify({ message: "جاري الحفظ", type: "loading" }); | ||
|
||
try { | ||
const {data} = await baseURL.post(url,rawData) | ||
notify({message:"تم الحفظ بنجاح",type:"success",id}) | ||
const { data } = await baseURL.post(url, rawData); | ||
notify({ message: "تم الحفظ بنجاح", type: "success", id }); | ||
return dispatch({ | ||
type:CREATE_ITEM, | ||
payload:data | ||
}) | ||
type: CREATE_ITEM, | ||
payload: data | ||
}); | ||
|
||
} catch ({ response, message }) { | ||
notify({message:"حدث خطأ أثناء الحفظ",type:"error",id}) | ||
notify({ message: "حدث خطأ أثناء الحفظ", type: "error", id }); | ||
return dispatch({ | ||
type: CREATE_ITEM_ERROR, | ||
type: ITEM_ERROR, | ||
error: `Error: ${response ? response.data?.message : message}` | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
export default createRawDataItem; |
Oops, something went wrong.