After clicked delete button, automatically route to signin page
<
exports.deleteProductById = (req, res) => {
console.log({ body: req.body });
const { productId } = req.body.payload;
if (productId) {
Product.deleteOne({ _id: productId }).exec((error, result) => {
if (error) return res.status(400).json({ error });
if (result) {
res.status(202).json({ result });
}
});
} else {
res.status(400).json({ error: "Params required" });
}
};
exports.getProducts = async (req, res) => {
const products = await Product.find({})
.select("_id name price quantity slug description productPictures category")
.populate({ path: "category", select: "_id name" })
.exec();
res.status(200).json({ products });
};/>
<import { ModalBody } from "react-bootstrap";
import axios from "../helpers/axios";
import { productConstants } from "./constants";
const getProducts = () => {
return async (dispatch) => {
try {
dispatch({ type: productConstants.GET_ALL_PRODUCTS_REQUEST });
const res = await axios.post(`product/getProducts`);
if (res.status === 200) {
const { products } = res.data;
dispatch({
type: productConstants.GET_ALL_PRODUCTS_SUCCESS,
payload: { products },
});
} else {
dispatch({ type: productConstants.GET_ALL_PRODUCTS_FAILURE });
}
} catch (error) {
console.log(error);
}
};
};
export const addProduct = (form) => {
return async (dispatch) => {
try {
dispatch({ type: productConstants.ADD_PRODUCT_REQUEST });
const res = await axios.post(`product/create`, form);
if (res.status === 201) {
dispatch({ type: productConstants.ADD_PRODUCT_SUCCESS });
dispatch(getProducts());
} else {
dispatch({ type: productConstants.ADD_PRODUCT_FAILURE });
}
} catch (error) {
console.log(error);
}
};
};
export const deleteProductById = (payload) => {
return async (dispatch) => {
try {
const res = await axios.delete(`product/deleteProductById`, {
data: { payload },
});
dispatch({ type: productConstants.DELETE_PRODUCT_BY_ID_REQUEST });
if (res.status === 202) {
dispatch({ type: productConstants.DELETE_PRODUCT_BY_ID_SUCCESS });
dispatch(getProducts());
} else {
const { error } = res.data;
dispatch({
type: productConstants.DELETE_PRODUCT_BY_ID_FAILURE,
payload: {
error,
},
});
}
} catch (error) {
console.log(error);
}
};
};/>