Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import axios from 'axios';
- import API_URL from '../url';
- export const TRANSACTIONS_URL = `${API_URL}/sales`;
- // Transaction Types
- export const TRANSACTION_FETCH_REQUEST = 'TRANSACTION_FETCH_REQUEST';
- export const TRANSACTION_FETCH_SUCCESS = 'TRANSACTION_FETCH_SUCCESS';
- export const TRANSACTION_FETCH_FAILED = 'TRANSACTION_FETCH_FAILED';
- export const TRANSACTION_FETCH_RESET = 'TRANSACTION_FETCH_RESET';
- // Transaction Action & Functions
- export const transactionFetchRequest = () => ({
- type: TRANSACTION_FETCH_REQUEST,
- });
- export const transactionFetchSuccess = response => ({
- type: TRANSACTION_FETCH_SUCCESS,
- payload: {
- data: response.body,
- },
- });
- export const transactionFetchFailed = error => ({
- type: TRANSACTION_FETCH_FAILED,
- payload: {
- error: error.header.response_code,
- errorMessage: error.header.response_description,
- },
- });
- export const transactionFetchReset = () => ({
- type: TRANSACTION_FETCH_RESET,
- });
- export const transactionFetch = () => (dispatch, getState) => {
- const token = getState().userStore.userToken;
- const userId = getState().userStore.user.id;
- dispatch(transactionFetchRequest());
- axios({
- method: 'post',
- url: TRANSACTIONS_URL,
- headers: {
- Authorization: `Bearer ${token}`,
- },
- data: {
- header: {
- page: 1,
- sort: 'DESC',
- },
- body: {
- // created_by : `${userId}`,
- },
- },
- }).then((response) => {
- if (response.data.header.response_code === '99') {
- dispatch(transactionFetchSuccess(response.data));
- } else {
- dispatch(transactionFetchFailed(response.data));
- }
- });
- };
- // Transaction Reducers
- export const TRANSACTION_INIT_STATE = {
- transactions: [],
- transationsStatus: '',
- transactionsIsFetching: false,
- };
- export const transactionStore = (state = TRANSACTION_INIT_STATE, action) => {
- switch (action.type) {
- case TRANSACTION_FETCH_REQUEST:
- return {
- ...state,
- transactionsIsFetching: true,
- };
- case TRANSACTION_FETCH_SUCCESS:
- return {
- ...state,
- transactions: action.payload.data,
- transactionsStatus: 'Transactions Fetched!',
- transactionsIsFetching: false,
- };
- case TRANSACTION_FETCH_FAILED:
- return {
- ...state,
- transactionsStatus: `${action.payload.error} ${action.payload.errorMessage}`,
- transactionsIsFetching: false,
- };
- case TRANSACTION_FETCH_RESET:
- return {
- ...state,
- transactionsIsFetching: false,
- };
- default:
- return state;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement