I have three slices ProductSlice, CategorySlice and BrandSlice. Each slice has createAsyncThunk which makes fetch request for products, categories and brands.
CategorySlice (BrandSlice the same):
export const fetchCategories = createAsyncThunk('category/fetchCategories', async () => {
const response = await fetch(`${BASE_URL}/categories.json`);
const data = await response.json();
const categories: Category[] = handleObj(data);
return categories;
});
builder.addCase(fetchCategories.fulfilled, (state, { payload }) => {
state.categories = payload;
state.isLoading = false;
});
App component dispatches three fetch requests.
useEffect(() => {
dispatch(fetchCategories());
dispatch(fetchProducts());
dispatch(fetchBrands());
}, [dispatch]);
I need to get data from brands and categories. Then get data for products and add some data to each product and only after that set it to ProducSlice state. The data should come from categories and brands. I'm trying to it in ProductSlice createAsyncThunk this way:
export const fetchProducts = createAsyncThunk<Product[], void, { state: RootState }>(
'product/fetchProducts',
async (_, { getState }) => {
const response = await fetch(`${BASE_URL}/products.json`);
const data = await response.json();
const products: Product[] = handleObj(data);
const { brands } = await getState().brand;
const { categories } = await getState().category;
const productsWithUpdatedBrandsAndCategories = products.map((product) => {
const category = categories.find((category) => category.id === product.category.id);
const brand = brands.find((brand) => brand.id === product.brand.id);
if (!category && !brand) return product;
return {
...product,
category: {
...category,
name: category && category.name,
},
brand: {
...brand,
name: brand && brand.name,
},
};
}) as Product[];
return productsWithUpdatedBrandsAndCategories;
}
);
The problem is that from time to time the brands or categories in ProductSlice in thunk are empty arrays (I mean the thunk fires when the data has not came from other slices..so productsWithUpdatedBrandsAndCategories do nothing then). How to deal with that?