github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/blockapps-ba-master/ui/src/scenes/Projects/components/Project/sagas/project-event.saga.js (about)

     1  import {
     2    takeLatest, put, call
     3  } from 'redux-saga/effects';
     4  import {
     5    PROJECT_EVENT,
     6    projectEventSuccess,
     7    projectEventFailure
     8  } from '../actions/project-event.actions';
     9  import { browserHistory } from 'react-router';
    10  import { API_URL, API_MOCK } from '../../../../../environment';
    11  import { handleApiError } from '../../../../../lib/apiErrorHandler';
    12  import { PROJECT_EVENTS } from '../../../../../constants';
    13  import { showLoading, hideLoading } from 'react-redux-loading-bar';
    14  import { setUserMessage } from '../../../../../components/UserMessage/user-message.action'
    15  import { userBalanceSubmit } from '../../../../../components/App/components/UserBadge/user-badge.actions';
    16  
    17  const url = API_URL + '/projects/:projectName/events';
    18  
    19  function projectEventCall(projectName, projectEvent, username) {
    20  
    21    if(API_MOCK) {
    22      return new Promise(function(resolve,reject){
    23        resolve({});
    24      });
    25    }
    26    else {
    27      const apiUrl = url.replace(':projectName', projectName);
    28  
    29      return fetch(apiUrl, {
    30        method: 'POST',
    31        headers: {
    32          'Content-Type': 'application/json;charset=utf-8',
    33          'Accept': 'application/json'
    34        },
    35        body: JSON.stringify({projectEvent: projectEvent, username: username})
    36      })
    37        .then(handleApiError)
    38        .then(function(response) {
    39          return response.json();
    40        })
    41        .catch(function(error){
    42          throw error;
    43        });
    44    }
    45  }
    46  
    47  function* projectEvent(action){
    48    try {
    49      yield put(showLoading());
    50      yield call(projectEventCall, action.projectName, action.projectEvent, action.username);
    51      yield put(projectEventSuccess());
    52      yield put(userBalanceSubmit(action.username));
    53      yield put(hideLoading());
    54      yield put(setUserMessage('Item ' + PROJECT_EVENTS[action.projectEvent]));
    55      browserHistory.goBack(); // todo: update current project data on the page instead?
    56    }
    57    catch(err) {
    58      yield put(projectEventFailure(err));
    59    }
    60  }
    61  
    62  export default function* watchProjectEvent() {
    63    yield takeLatest(PROJECT_EVENT, projectEvent);
    64  }