0

I want to create the package and save it to the database but getting an error that getState is not a function.

The redux-store file:

const reducer = combineReducers({
  adminLogin: adminLoginReducer,
  packageCreate: packageCreateReducer,
});

const adminInfoFromStorage = localStorage.getItem("adminInfo")
  ? JSON.parse(localStorage.getItem("adminInfo"))
  : null;

const initialState = {
  adminLogin: { adminInfo: adminInfoFromStorage },
};

The packageScreen where I want to verify if the user is logged in or not:

const adminLogin = useSelector((state) => state.adminLogin);
  const { adminInfo } = adminLogin;

The createPackage action:

export const createPackage = (packageData, getState) => async (dispatch) => {
  try {
    dispatch({ type: PACKAGE_CREATE_REQUEST });

    const {
      adminLogin: { adminInfo },
    } = getState();

    const config = {
      headers: {
        "Content-Type": "application/json",
        Authorization: `${adminInfo.data.JWToken}`,
      },
    };
    const { data } = await axios.post(
      "http://localhost:8000/v1/package/add_package",
      packageData,
      config
    );

    dispatch({ type: PACKAGE_CREATE_SUCCESS, payload: data });
  } catch (error) {
    dispatch({
      type: PACKAGE_CREATE_FAILURE,
      payload:
        error.response && error.response.data.message
          ? error.response.data.message
          : error.message,
    });
  }
};

It saves the adminInfo in the localstorage but how to dispatch the adminInfo and save the package Details to the database.

const adminLogin = useSelector((state) => state.adminLogin);
  const { adminInfo } = adminLogin;

  const packageCreate = useSelector((state) => state.packageCreate);
  const { loading, success, packageDetails, error } = packageCreate;

  useEffect(() => {
    if (success) {
      console.log("Complete");
    }
  }, [dispatch, adminInfo, success]);

const submitHandler = (e) => {   //SubmitHandler Function.
    e.preventDefault();
    dispatch({
      createPackage(
        packageName,
        packageAmount,
        affiliationPercentage,
        description,
        features}
      })
    );
  };
John Oliver
  • 125
  • 1
  • 11

0 Answers0