Skip to content

Instantly share code, notes, and snippets.

@janpauldahlke
Created June 6, 2018 18:18
Show Gist options
  • Save janpauldahlke/d4968a715f5ff7e244da7a0f92d681c1 to your computer and use it in GitHub Desktop.
Save janpauldahlke/d4968a715f5ff7e244da7a0f92d681c1 to your computer and use it in GitHub Desktop.
import * as Actions from '../constants/actions';
import {createAction} from 'redux-actions';
import axios from 'axios';
import {isNullOrUndefined} from "util";
import * as generals from './general';
function createAxiosInstance() {
const jwt_token = localStorage.getItem('token');
return axios.create({
baseURL: process.env.PPP_API,
timeout: 5000, // due error on updateDealStatus when server response takes longer 1000ms
headers: {
'X-Auth-Token': jwt_token,
}
})
}
// gets us list of dealsList
export function fetchDeals() {
return function (dispatch) {
dispatch(fetchDealsList());
return createAxiosInstance().get('/api/v1/admin/deal')
.then((response) => {
dispatch(fetchDealsListSuccess(response.data.data));
}).catch((error) => {
dispatch(generals.dispatcherrorMessage(generals.parseError(error, "Deals")));
dispatch(fetchDealsListFailure(error.toString()));
});
};
}
// get a single deal
export function fetchDeal(paymentToken: string) {
return function (dispatch) {
dispatch(fetchSingleDeal());
return createAxiosInstance().get('/api/v1/admin/deal/' + paymentToken)
.then((response) => {
dispatch(fetchSingleDealSuccess(response.data.data))
}).catch((error) => {
console.log('error', error)
dispatch(generals.dispatcherrorMessage(generals.parseError(error, "Deals")));
dispatch(fetchSingleDealFailure(error.toString()));
});
};
}
export function createDeal(post: Deal, showSwal: boolean = false) {
return function (dispatch) {
dispatch(createSingleDeal());
let newDeal: Deal = JSON.parse(JSON.stringify(post));
if (!isNullOrUndefined(newDeal.recipient.taxId) && isNullOrUndefined(newDeal.recipient.taxId.id)) {
delete newDeal.recipient.taxId
}
return createAxiosInstance().post('/api/v1/admin/deal', newDeal)
.then((response) => {
// dispatch openswall({hat gelappt})
dispatch(createSingleDealSuccess(response.data.data));
if (showSwal) {
dispatch(generals.openSwal({title: "Create Deal", message: "The Deal has been created", swalType: "success"}))
setTimeout(() => {
dispatch(generals.closeSwal())
}, 1000)
}
}).catch((error) => {
dispatch(createSingleDealFailure(error));
dispatch(generals.dispatcherrorMessage(generals.parseError(error, "Deals")));
dispatch(generals.openSwal({title: "Create Deal", message: generals.parseError(error), swalType: "error"}));
})
}
}
export function sendDealToCustomer(token: string, editor: Editor) {
return function (dispatch) {
dispatch(sendDealAndMailToCustomer());
return createAxiosInstance().post('/api/v1/admin/deal/' + token + '/send', editor)
.then((response) => {
dispatch(sendDealToCustomerSuccess(response.data.data))
dispatch(generals.openSwal({title: "Send Mail", message: "The Mail has been send", swalType: "success"}))
setTimeout(() => {
dispatch(generals.closeSwal())
}, 1000)
}).catch((error) => {
dispatch(generals.dispatcherrorMessage(generals.parseError(error, "Deals")));
dispatch(generals.openSwal({title: "Send Mail", message: generals.parseError(error), swalType: "error"}))
dispatch(sendDealToCustomerFailure(error))
})
}
}
//--------------------------------------------------
//update Deal // http PUT
export function updateDeal(paymentToken: string, deal: Deal, showSwal: boolean = false) {
delete deal.createdAt;
delete deal.id;
return function (dispatch) {
dispatch(updateDealStart());
return createAxiosInstance().put('/api/v1/admin/deal/' + paymentToken, deal)
.then((response) => {
dispatch(updateDealSuccess(response.data.data));
if (showSwal) {
dispatch(generals.openSwal({title: 'Update', message: 'Deal updated', swalType: 'success'}))
setTimeout(() => {
dispatch(generals.closeSwal())
}, 1000)
}
}).catch((error) => {
dispatch(generals.dispatcherrorMessage(generals.parseError(error, "Deals_update")));
dispatch(generals.openSwal({title: 'Update', message: generals.parseError(error), swalType: 'error'}));
dispatch(updateDealFailure(error));
});
}
}
export function removeItemFromCartAndUpdateDeal(token: string, deal: Deal, row: number) {
delete deal.createdAt;
delete deal.id;
let newDeal: Deal = JSON.parse(JSON.stringify(deal));
newDeal.cart.items = newDeal.cart.items.filter(item => item !== newDeal.cart.items[row]);
return function (dispatch) {
dispatch(removeItemFromCart());
return createAxiosInstance().put('/api/v1/admin/deal/' + token, newDeal)
.then((response) => {
dispatch(removeItemFromCartSuccess(response.data.data))
}).catch((error) => {
dispatch(generals.dispatcherrorMessage(generals.parseError(error, "Deals")));
dispatch(removeItemFormCartFailure(error))
})
}
}
export function updateCartStatus(token: string, deal: Deal, status: string) {
delete deal.createdAt;
delete deal.id;
deal.cart.cartType = status;
return function (dispatch) {
//solve timeout problem here
dispatch(updateCartStat());
return createAxiosInstance().put('/api/v1/admin/deal/' + token, deal)
.then((response) => {
dispatch(updateDealStatusSuccess(response.data.data))
}).catch((error) => {
dispatch(generals.dispatcherrorMessage(generals.parseError(error, "Deals")));
dispatch(updateDealStatusFailure(error))
});
}
}
export function updateDealStatus(token: string, deal: Deal, status: string) {
deal.status = status;
return function (dispatch) {
//solve timeout problem here
dispatch(updateDealStat());
return createAxiosInstance().put('/api/v1/admin/deal/' + token, deal)
.then((response) => {
dispatch(updateDealStatusSuccess(response.data.data));
dispatch(generals.openSwal({
title: "Update Status",
message: `DealStatus updated to ${status}`,
swalType: "success"
}))
setTimeout(() => {
dispatch(generals.closeSwal())
}, 1000)
}).catch((error) => {
dispatch(generals.dispatcherrorMessage(generals.parseError(error, "Deals")));
dispatch(generals.openSwal({title: "Update Status", message: generals.parseError(error), swalType: "error"}));
dispatch(updateDealStatusFailure(error))
});
}
}
export function deleteDeal(paymentToken: string) {
return function (dispatch) {
dispatch(deleteSingleDeal());
return createAxiosInstance().delete('/api/v1/admin/deal/' + paymentToken)
.then((response) => {
dispatch(deleteSingleDealSuccess(paymentToken));
dispatch(generals.openSwal({title: "Delete Deal", message: "The Deal has been deleted", swalType: "success"}))
setTimeout(() => {
dispatch(generals.closeSwal())
}, 1000)
}).catch((error) => {
dispatch(generals.dispatcherrorMessage(generals.parseError(error, "Deals")));
dispatch(generals.openSwal({title: "Delete Deal", message: generals.parseError(error), swalType: "error"}))
dispatch(deleteSingleDealFailure(error))
})
}
}
///////////////////////////////////////////////////////
//nulls a single deal
export function flushDeal() {
return function (dispatch) {
dispatch(flushSingleDeal());
}
}
//nulls dealsList
export function flushDeals() {
return function (dispatch) {
dispatch(flushDealsList());
}
}
//////////////////////////////////////////////////////
export function addRecipientToDeal(recipient: Recipient) {
return function (dispatch) {
dispatch(addRecipient(recipient));
}
}
export function addOwnerToDeal(owner: Owner) {
return function (dispatch) {
dispatch(addOwner(owner));
}
}
export function addEditorToDeal(editor: Editor) {
return function (dispatch) {
dispatch(addEditor(editor));
}
}
export function addItemToDeal(item: Item) {
return function (dispatch) {
dispatch(addItem(item));
}
}
export function editItemInDeal(item: Item) {
return function (dispatch) {
dispatch(editItem(item));
}
}
export function addTotalDiscountToDeal(discount: Discounts) {
return function (dispatch) {
dispatch(addTotalDiscount(discount));
}
}
export function removeItemFromDeal(key: number): (dispatch) => any {
return function (dispatch) {
dispatch(removeItem(key));
}
}
//-------------------actions for websockets
export function patchDealDiff(data: DealJsonDiff) {
return function (dispatch) {
dispatch(patchDealDiffAction(data));
}
}
export function addDealToDealsList(deal: Deal) {
return function (dispatch) {
dispatch(createSingleDealWebsocket(deal));
}
}
export function deleteDealsFromList(token: string) {
return function (dispatch) {
dispatch(deleteSingleDealSuccess(token))
}
}
export function getHubSpotDeals(email: string) {
return function (dispatch) {
dispatch(fetchDealsList());
return createAxiosInstance().get('/api/v1/admin/deal/'+email)
.then((response) => {
dispatch(fetchDealsListSuccess(response.data.data));
}).catch((error) => {
dispatch(getHubSpotDealsListFailure(error.toString()));
});
};
}
//---------------------------------------------------------
export const patchDealDiffAction = createAction<DealJsonDiff>(Actions.PATCH_DEAL_DIFF_ACTION);
export const createSingleDealWebsocket = createAction<Deal>(Actions.WEBSOCKET_CREATE_DEAL_SUCCESS)
export const updateCartStat = createAction(Actions.UPDATE_CART_STATUS);
export const removeItemFromCart = createAction(Actions.REMOVE_ITEM_FROM_CART);
export const removeItemFromCartSuccess = createAction(Actions.REMOVE_ITEM_FROM_CART_SUCCESS);
export const removeItemFormCartFailure = createAction(Actions.REMOVE_ITEM_FROM_CART_FAILURE);
export const fetchDealsList = createAction(Actions.FETCH_DEALS_LIST);
export const fetchDealsListSuccess = createAction<Deal>(Actions.FETCH_DEALS_LIST_SUCCESS);
export const fetchDealsListFailure = createAction<any>(Actions.FETCH_DEALS_LIST_FAILURE);
export const fetchSingleDeal = createAction(Actions.FETCH_SINGLE_DEAL);
export const fetchSingleDealSuccess = createAction<Deal>(Actions.FETCH_SINGLE_DEAL_SUCCESS);
export const fetchSingleDealFailure = createAction<any>(Actions.FETCH_SINGLE_DEAL_FAILURE);
export const createSingleDeal = createAction(Actions.CREATE_DEAL_ITEM);
export const createSingleDealSuccess = createAction<Deal>(Actions.CREATE_DEAL_ITEM_SUCCESS);
export const createSingleDealFailure = createAction<any>(Actions.CREATE_DEAL_ITEM_FAILURE);
export const deleteSingleDeal = createAction(Actions.DELETE_DEAL_ITEM);
export const deleteSingleDealSuccess = createAction<String>(Actions.DELETE_DEAL_ITEM_SUCCESS);
export const deleteSingleDealFailure = createAction<any>(Actions.DELETE_DEAL_ITEM_FAILURE);
export const addRecipient = createAction<Recipient>(Actions.ADD_RECIPIENT_TO_DEAL);
export const addItem = createAction<Item>(Actions.ADD_ITEM_TO_DEAL);
export const editItem = createAction<Item>(Actions.EDIT_ITEM_IN_DEAL);
export const removeItem = createAction<number>(Actions.REMOVE_ITEM_FROM_DEAL);
export const addTotalDiscount = createAction<Discounts>(Actions.ADD_TOTAL_DISCOUNT_TO_DEAL);
export const updateDealStart = createAction(Actions.UPDATE_DEAL_START);
export const updateDealSuccess = createAction<Deal>(Actions.UPDATE_DEAL_SUCCESS);
export const updateDealFailure = createAction<any>(Actions.UPDATE_DEAL_FAILURE);
export const updateDealStat = createAction(Actions.UPDATE_DEAL_STATUS);
export const updateDealStatusSuccess = createAction<Deal>(Actions.UPDATE_DEAL_STATUS_SUCCESS);
export const updateDealStatusFailure = createAction<any>(Actions.UPDATE_DEAL_STATUS_FAILURE);
export const flushSingleDeal = createAction(Actions.FLUSH_DEAL_ITEM);
export const flushDealsList = createAction(Actions.FLUSH_DEALS_LIST);
export const addOwner = createAction<Owner>(Actions.ADD_OWNER_TO_DEAL);
export const addEditor = createAction<Editor>(Actions.ADD_EDITOR_TO_DEAL);
export const sendDealAndMailToCustomer = createAction(Actions.SEND_DEAL_OFFER_TO_CUSTOMER);
export const sendDealToCustomerSuccess = createAction<Deal>(Actions.SEND_DEAL_OFFER_TO_CUSTOMER_SUCCESS);
export const sendDealToCustomerFailure = createAction<any>(Actions.SEND_DEAL_OFFER_TO_CUSTOMER_FAILURE);
export const getHubSpotDealsListFailure = createAction<String>(Actions.GET_HUBSPOT_DEALS_LIST_FAILURE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment