Advertisement
rizkyalviandra

axios Transaction

Sep 18th, 2019
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import axios from 'axios';
  2. import API_URL from '../url';
  3.  
  4. export const TRANSACTIONS_URL = `${API_URL}/sales`;
  5.  
  6. // Transaction Types
  7. export const TRANSACTION_FETCH_REQUEST = 'TRANSACTION_FETCH_REQUEST';
  8. export const TRANSACTION_FETCH_SUCCESS = 'TRANSACTION_FETCH_SUCCESS';
  9. export const TRANSACTION_FETCH_FAILED = 'TRANSACTION_FETCH_FAILED';
  10. export const TRANSACTION_FETCH_RESET = 'TRANSACTION_FETCH_RESET';
  11.  
  12. // Transaction Action & Functions
  13. export const transactionFetchRequest = () => ({
  14.   type: TRANSACTION_FETCH_REQUEST,
  15. });
  16.  
  17.  
  18. export const transactionFetchSuccess = response => ({
  19.   type: TRANSACTION_FETCH_SUCCESS,
  20.   payload: {
  21.     data: response.body,
  22.   },
  23. });
  24.  
  25. export const transactionFetchFailed = error => ({
  26.   type: TRANSACTION_FETCH_FAILED,
  27.   payload: {
  28.     error: error.header.response_code,
  29.     errorMessage: error.header.response_description,
  30.   },
  31. });
  32.  
  33. export const transactionFetchReset = () => ({
  34.   type: TRANSACTION_FETCH_RESET,
  35. });
  36.  
  37. export const transactionFetch = () => (dispatch, getState) => {
  38.   const token = getState().userStore.userToken;
  39.   const userId = getState().userStore.user.id;
  40.  
  41.   dispatch(transactionFetchRequest());
  42.  
  43.   axios({
  44.     method: 'post',
  45.     url: TRANSACTIONS_URL,
  46.     headers: {
  47.       Authorization: `Bearer ${token}`,
  48.     },
  49.     data: {
  50.       header: {
  51.         page: 1,
  52.         sort: 'DESC',
  53.       },
  54.       body: {
  55.         // created_by : `${userId}`,
  56.       },
  57.     },
  58.   }).then((response) => {
  59.     if (response.data.header.response_code === '99') {
  60.       dispatch(transactionFetchSuccess(response.data));
  61.     } else {
  62.       dispatch(transactionFetchFailed(response.data));
  63.     }
  64.   });
  65. };
  66.  
  67. // Transaction Reducers
  68. export const TRANSACTION_INIT_STATE = {
  69.   transactions: [],
  70.   transationsStatus: '',
  71.   transactionsIsFetching: false,
  72. };
  73.  
  74. export const transactionStore = (state = TRANSACTION_INIT_STATE, action) => {
  75.   switch (action.type) {
  76.     case TRANSACTION_FETCH_REQUEST:
  77.       return {
  78.         ...state,
  79.         transactionsIsFetching: true,
  80.       };
  81.  
  82.     case TRANSACTION_FETCH_SUCCESS:
  83.       return {
  84.         ...state,
  85.         transactions: action.payload.data,
  86.         transactionsStatus: 'Transactions Fetched!',
  87.         transactionsIsFetching: false,
  88.       };
  89.  
  90.     case TRANSACTION_FETCH_FAILED:
  91.       return {
  92.         ...state,
  93.         transactionsStatus: `${action.payload.error} ${action.payload.errorMessage}`,
  94.         transactionsIsFetching: false,
  95.       };
  96.  
  97.     case TRANSACTION_FETCH_RESET:
  98.       return {
  99.         ...state,
  100.         transactionsIsFetching: false,
  101.       };
  102.  
  103.     default:
  104.       return state;
  105.   }
  106. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement