Untitled
raw download clone
JSX
views 10
,
size 14206 b
import axios from "axios"
import showerror from "../components/ErrorToast"
import showsuccess from "../components/SuccessToast"
import { PASSWORD_UPDATE_FAIL, PASSWORD_UPDATE_REQUEST, PASSWORD_UPDATE_SUCCESS, USER_DETAILS_FAIL, USER_DETAILS_REQUEST, USER_DETAILS_SUCCESS, USER_REGISTER_FAIL, USER_REGISTER_REQUEST, USER_SIGNIN_FAIL, USER_SIGNIN_REQUEST, USER_SIGNIN_SUCCESS, USER_SIGNOUT, USER_UPDATE_FAIL, USER_UPDATE_IMAGE_FAIL, USER_UPDATE_IMAGE_REQUEST, USER_UPDATE_IMAGE_SUCCESS, USER_UPDATE_REQUEST, USER_UPDATE_SUCCESS } from "../constants/userConstant"

export const signin = (authdata) => async (dispatch) => {
    dispatch({
        type: USER_SIGNIN_REQUEST,

    })

    try {

        const { data } = await axios.post("/api/v1/login/", {
            "username": authdata.username, "password": authdata.password
        })
        dispatch({
            type: USER_SIGNIN_SUCCESS, payload: data
        })

        showsuccess(`You have successfully logged in 🤗! \nWelcome back  🤝  `)

        if (authdata.rememberme) {
            localStorage.setItem("mspay-userInfo", JSON.stringify(data))
        }


    }

    catch (error) {

        const errorMsg = error.response && error.response.data.message ? error.response.data.message : error.message
        dispatch({ type: USER_SIGNIN_FAIL, error: errorMsg })
        showerror(`Oops 😠:  ${errorMsg}`)



    }
}

export const signout = () => async (dispatch) => {
    dispatch({
        type: USER_SIGNOUT,

    })
    localStorage.removeItem("mspay-userInfo")
    showsuccess(`You have successfully log out 👋 `)

}





export const userregister = (registrationdata) => async (dispatch) => {

    dispatch({
        type: USER_REGISTER_REQUEST,

    })

    try {

        const { data } = await axios.post("/api/v1/registration/", {
            "email": registrationdata.email, "password": registrationdata.password, "firstname": registrationdata.firstname,
            "lastname": registrationdata.lastname, "phone": registrationdata.phone, "username": registrationdata.username,
        })


        dispatch({
            type: USER_SIGNIN_SUCCESS, payload: data
        })
        showsuccess(`Registration successful,welcome chief  🤝  `)




    }

    catch (error) {
        console.log(error.response)
        let errorMsg = ""


        if (error.response && error.response.data.phone) {
            errorMsg = `phone:  ${error.response.data.phone[0]}`
        }
        else if (error.response && error.response.data.address) {
            errorMsg = `address:  ${error.response.data.address[0]}`
        }
        else if (error.response && error.response.data.username) {
            errorMsg = `username: ${error.response.data.username[0]}`
        }
        else if (error.response && error.response.data.lastname) {
            errorMsg = `last_name: ${error.response.data.lastname[0]}`
        }
        else if (error.response && error.response.data.firstname) {
            errorMsg = `first_name: ${error.response.data.firstname[0]}`
        }
        else if (error.response && error.response.data.password) {
            errorMsg = `password: ${error.response.data.password[0]}`
        }

        else if (error.response && error.response.data.email) {
            errorMsg = `email: ${error.response.data.email[0]}`
        }
        else if (error.response && error.response.data.non_field_errors) {
            errorMsg = `non_field_errors: ${error.response.data.non_field_errors[0]}`
        }
        else if (error.response && error.response.data.error) {
            errorMsg = error.response.data.error[0]
        }
        else {
            errorMsg = error.message
        }



        dispatch({ type: USER_REGISTER_FAIL, error: errorMsg })
        showerror(`Oops 😠,  ${errorMsg}`)



    }
}




export const userDetails = () => async (dispatch, getState) => {

    dispatch({
        type: USER_DETAILS_REQUEST,

    })
    try {
        const { userSignin: { userInfo } } = getState();

        const { data } = await axios.get(`/api/v1/webconfig/`, {
            headers: {
                'Authorization': `Bearer ${userInfo.token}`,
                'Accept': 'application/json',


            }
        })



        dispatch({
            type: USER_DETAILS_SUCCESS,
            payload: data
        })



        // dispatch({
        //     type: USER_SIGNIN_SUCCESS, payload: data
        // })
        // localStorage.setItem("mspay-userInfo", JSON.stringify(data))

    }
    catch (error) {

        console.log(error)
        let errorMsg = ""

        if (error.response && error.response.data.error) {
            errorMsg = error.response.data.error[0]
        }
        else if (error.response && error.response.data.message)

            errorMsg = error.response.data.message
        else {
            errorMsg = error.message
        }

        dispatch({ type: USER_DETAILS_FAIL, error: errorMsg })
        showerror(`Oops 😠,  ${errorMsg}`)

    }

}






export const userUpdate = (userdata) => async (dispatch, getState) => {

    dispatch({
        type: USER_UPDATE_REQUEST,

    })
    try {
        const { userSignin: { userInfo } } = getState();

        const { data } = await axios.put(`/api/v1/customer/`, {
            "email": userdata.email, "first_name": userdata.firstname,
            "last_name": userdata.lastname, "phone": userdata.phone, "username": userdata.username,
        }, {
            headers: {
                'Authorization': `Bearer ${userInfo.token}`,
                'Accept': 'application/json',
                'Content-Type': 'application/json'

            }
        })

        dispatch({
            type: USER_UPDATE_SUCCESS,
            payload: data
        })

        dispatch({
            type: USER_DETAILS_SUCCESS,
            payload: data
        })

        dispatch({
            type: USER_SIGNIN_SUCCESS, payload: data
        })


        localStorage.setItem("mspay-userInfo", JSON.stringify(data))


        showsuccess(`Profile updated successful 🤝  `)
    }
    catch (error) {

        console.log(error)
        let errorMsg = ""

        if (error.response && error.response.data.error) {
            errorMsg = error.response.data.error[0]
        }

        else if (error.response && error.response.data.phone) {
            errorMsg = `phone:  ${error.response.data.phone[0]}`
        }
        else if (error.response && error.response.data.address) {
            errorMsg = `address:  ${error.response.data.address[0]}`
        }
        else if (error.response && error.response.data.username) {
            errorMsg = `username: ${error.response.data.username[0]}`
        }
        else if (error.response && error.response.data.last_name) {
            errorMsg = `last_name: ${error.response.data.last_name[0]}`
        }
        else if (error.response && error.response.data.first_name) {
            errorMsg = `first_name: ${error.response.data.first_name[0]}`
        }
        else if (error.response && error.response.data.password) {
            errorMsg = `password: ${error.response.data.password[0]}`
        }

        else if (error.response && error.response.data.email) {
            errorMsg = `email: ${error.response.data.email[0]}`
        }
        else if (error.response && error.response.data.non_field_errors) {
            errorMsg = `non_field_errors: ${error.response.data.non_field_errors[0]}`
        }
        else if (error.response && error.response.data.error) {
            errorMsg = error.response.data.error[0]
        }

        else {
            errorMsg = error.message
        }

        dispatch({ type: USER_UPDATE_FAIL, error: errorMsg })
        showerror(`Oops 😠,  ${errorMsg}`)

    }

}


export const verifyPhone = () => async (dispatch, getState) => {

    dispatch({
        type: USER_UPDATE_REQUEST,

    })
    try {
        const { userSignin: { userInfo } } = getState();

        const { data } = await axios.post(`/api/v1/pactivate/`, {
            "phone_verify": true
        }, {
            headers: {
                'Authorization': `Bearer ${userInfo.token}`,
                'Accept': 'application/json',
                'Content-Type': 'application/json'

            }
        })

        dispatch({
            type: USER_UPDATE_SUCCESS,
            payload: data
        })

        dispatch({
            type: USER_DETAILS_SUCCESS,
            payload: data
        })

        dispatch({
            type: USER_SIGNIN_SUCCESS, payload: data
        })


        localStorage.setItem("mspay-userInfo", JSON.stringify(data))


        showsuccess(`Profile updated successful 🤝  `)
    }
    catch (error) {

        console.log(error)
        let errorMsg = ""

        if (error.response && error.response.data.error) {
            errorMsg = error.response.data.error[0]
        }


        else if (error.response && error.response.data.message) {
            errorMsg = error.response.data.message
        }

        else {
            errorMsg = error.message
        }

        dispatch({ type: USER_UPDATE_FAIL, error: errorMsg })
        showerror(`Oops 😠,  ${errorMsg}`)

    }

}




export const UpdateNotification = (dataset) => async (dispatch, getState) => {

    dispatch({
        type: USER_UPDATE_REQUEST,

    })
    try {
        const { userSignin: { userInfo } } = getState();

        const { data } = await axios.post(`/api/v1/update_notification_option/`, {
            allow_two_factor_auth: dataset?.allow_two_factor_auth,
            allow_email_notification: dataset?.allow_email_notification,
            allow_sms_notification: dataset?.allow_sms_notification

        }, {
            headers: {
                'Authorization': `Bearer ${userInfo.token}`,
                'Accept': 'application/json',
                'Content-Type': 'application/json'

            }
        })

        dispatch({
            type: USER_UPDATE_SUCCESS,
            payload: data
        })

        dispatch({
            type: USER_DETAILS_SUCCESS,
            payload: data
        })

        dispatch({
            type: USER_SIGNIN_SUCCESS, payload: data
        })


        localStorage.setItem("mspay-userInfo", JSON.stringify(data))


        showsuccess(`Profile updated successful 🤝  `)
    }
    catch (error) {

        console.log(error)
        let errorMsg = ""

        if (error.response && error.response.data.error) {
            errorMsg = error.response.data.error[0]
        }


        else if (error.response && error.response.data.message) {
            errorMsg = error.response.data.message
        }

        else {
            errorMsg = error.message
        }

        dispatch({ type: USER_UPDATE_FAIL, error: errorMsg })
        showerror(`Oops 😠,  ${errorMsg}`)

    }

}



export const userUpdatePassword = (userdata) => async (dispatch, getState) => {

    dispatch({
        type: PASSWORD_UPDATE_REQUEST,

    })
    try {
        const { userSignin: { userInfo } } = getState();

        const { data } = await axios.post(`/api/v1/password_change/`, JSON.stringify({
            oldpassword: userdata.oldpassword, newpassword: userdata.newpassword,
        }), {
            headers: {
                'Authorization': `Bearer ${userInfo.token}`,
                'Accept': 'application/json',
                'Content-Type': 'application/json'

            }
        })

        dispatch({
            type: PASSWORD_UPDATE_SUCCESS,
            payload: data
        })
        dispatch({
            type: USER_SIGNOUT,

        })
        localStorage.removeItem("mspay-userInfo")
        showsuccess(`password updated successful , proceed to login again with your new password  🤗 `)




    }
    catch (error) {

        console.log(error)
        let errorMsg = ""

        if (error.response && error.response.data.error) {
            errorMsg = error.response.data.error[0]
        }


        else if (error.response && error.response.data.message) {
            errorMsg = error.response.data.message
        }

        else {
            errorMsg = error.message
        }

        dispatch({ type: PASSWORD_UPDATE_FAIL, error: errorMsg })
        showerror(`Oops 😠,  ${errorMsg}`)

    }

}








export const userImageUpdate = (image) => async (dispatch, getState) => {

    dispatch({
        type: USER_UPDATE_IMAGE_REQUEST,

    })
    try {
        const { userSignin: { userInfo } } = getState();
        let formData = new FormData();
        formData.append('image', image);
        const { data } = await axios.post(`/api/v1/customer/`, formData, {
            headers: {
                'Authorization': `Bearer ${userInfo.token}`,
                "Content-type": "multipart/form-data",

            }
        })
        console.log(data)

        dispatch({
            type: USER_UPDATE_IMAGE_SUCCESS,

        })


        dispatch({
            type: USER_SIGNIN_SUCCESS, payload: data
        })




        localStorage.setItem("mspay-userInfo", JSON.stringify(data))


        showsuccess(`Profile Image updated successful 🤝  `)
    }
    catch (error) {

        console.log(error)
        let errorMsg = ""

        if (error.response && error.response.data.error) {
            errorMsg = error.response.data.error[0]
        }

        else if (error.response && error.response.data.message) {
            errorMsg = error.response.data.message
        }


        else {
            errorMsg = error.message
        }

        dispatch({ type: USER_UPDATE_IMAGE_FAIL, error: errorMsg })
        showerror(`Oops 😠,  ${errorMsg}`)

    }

}
close fullscreen
Login or Register to edit or fork this paste. It's free.