Last active
September 26, 2017 18:51
-
-
Save kwekuboateng/ac6dc81adaae1d848e3f70094ca5448b to your computer and use it in GitHub Desktop.
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
import axios from 'axios'; | |
import * as actionType from './actionTypes/productActionTypes'; | |
import setAuthorizationToken from '../Utils/setAuthorizationToken' | |
import localforage from 'localforage'; | |
axios.defaults.baseURL = `https://api.aidahbot.com`; | |
export function productAddNew(product) { | |
return { | |
type: actionType.ADD_NEW_PRODUCT, | |
payload: product | |
} | |
} | |
export function fetchProductId(product) { | |
return{ | |
type: actionType.FETCH_SINGLE_PRODUCT, | |
payload: product | |
} | |
} | |
export function productSaved(bool) { | |
return { | |
type: actionType.PRODUCT_IS_SAVED, | |
payload: bool | |
} | |
} | |
export function productIsSaving(bool) { | |
return { | |
type: actionType.PRODUCT_IS_SAVING, | |
payload: bool | |
} | |
} | |
export function productDelete(id) { | |
return { | |
type: actionType.DELETE_PRODUCT, | |
payload: id | |
} | |
} | |
export function editProduct(product, show) { | |
return { | |
type: actionType.EDIT_PRODUCT, | |
payload: { | |
...product, | |
show: show | |
} | |
} | |
} | |
export function productFetch(product) { | |
return { | |
type: actionType.FETCH_PRODUCT, | |
payload: product | |
} | |
} | |
export function productSaveErrors(errors) { | |
return { | |
type: actionType.PRODUCT_SAVE_ERROR, | |
payload: errors | |
} | |
} | |
export function toggleModalClose(bool){ | |
return { | |
type: actionType.TOGGLE_CLOSE_MODAL, | |
payload: bool | |
} | |
} | |
export function addNewProduct(store_name, product) { | |
return (dispatch) => { | |
const token = localforage.getItem('jwtToken'). | |
then(token => { | |
setAuthorizationToken(token); | |
}); | |
axios.post(`/merchant/${store_name}/product`, product). | |
then(response => { | |
dispatch(productIsSaving(false)); | |
dispatch(productAddNew(response.data.product)); | |
console.log(response.data, 'this is response for product'); | |
dispatch(productSaved(true)); | |
dispatch(toggleModalClose(false)); | |
alert('new product added successfully'); | |
}). | |
catch(function (err) { | |
console.log('Err: ', err); | |
}); | |
} | |
} | |
export function updateProduct(store_name, id, product) { | |
return (dispatch) => { | |
localforage.getItem('jwtToken'). | |
then(token => { | |
setAuthorizationToken(token); | |
}); | |
axios.put(`/merchant/${store_name}/product/${id}`, product). | |
then(response => { | |
console.log(response.data, 'new data'); | |
dispatch(productAddNew(response.data.product)); | |
// dispatch(toggleEditProductModal(false)) | |
}). | |
catch(function (err) { | |
console.log('Err: ', err); | |
}); | |
} | |
} | |
export function fetchMerchantProduct() { | |
return (dispatch) => { | |
localforage.getItem('jwtToken').then(token => { | |
setAuthorizationToken(token); | |
}) | |
localforage.getItem('reduxPersist:Account').then(function (value) { | |
let user = JSON.parse(value); | |
return axios.get(`/merchant/${user.user.store_name}/product`); | |
}).then(response => { | |
dispatch(productFetch(response.data.products)) | |
}). | |
catch(function (err) { | |
console.log('Err: ', err); | |
}); | |
} | |
} | |
export function fetchSingleProduct(id) { | |
return (dispatch) => { | |
localforage.getItem('jwtToken').then(token => { | |
setAuthorizationToken(token); | |
}) | |
localforage.getItem('reduxPersist:Account').then(function (value) { | |
let user = JSON.parse(value); | |
return axios.get(`/merchant/${user.user.store_name}/product/${id}`); | |
}).then(response => { | |
dispatch(fetchProductId(response.data)); | |
}). | |
catch(function (err) { | |
console.log('Err: ', err); | |
}); | |
} | |
} | |
export function deleteProduct(store_name, id) { | |
return (dispatch) => { | |
localforage.getItem('jwtToken').then(token => { | |
setAuthorizationToken(token); | |
}) | |
const serverResponse = axios.delete(`/merchant/${store_name}/product/${id}`); | |
serverResponse.then((response) => { | |
dispatch(productDelete(id)); | |
alert('Product: deleted successfully'); | |
console.log(response); | |
}).catch(() => { | |
alert('Failed to delete product: '); | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment