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

     1  import {
     2    takeLatest,
     3    put,
     4    call
     5  } from 'redux-saga/effects';
     6  import {
     7    API_URL,
     8    API_MOCK
     9  } from '../../../../../environment';
    10  import {
    11    handleApiError
    12  } from '../../../../../lib/apiErrorHandler';
    13  import {
    14    FETCH_PROJECT,
    15    fetchProjectSuccess,
    16    fetchProjectFailure
    17  } from '../actions/project.actions';
    18  import { showLoading, hideLoading } from 'react-redux-loading-bar';
    19  
    20  // TODO: define API endpoint for projects
    21  const url = API_URL + '/projects/{0}';
    22  
    23  function getProjectMock(projectId) {
    24    return new Promise(function(resolve, reject) {
    25      resolve({
    26        data: {
    27          project: {
    28            id: projectId,
    29            created: '2017-05-09T16:47:49.016Z',
    30            buyer: 'buyer1',
    31            name: 'T-Shirts with logo',
    32            description: 'The T-Shirts with our company\'s logo on the chest, Qty: 50',
    33            priceDesired: 800.10,
    34            desiredDeliveryDate: '2017-05-20T16:47:49.016Z',
    35            deliveryAddress: {
    36              street: '109 S 5th street',
    37              city: 'Brooklyn',
    38              state: 'New York',
    39              zip: '11249'
    40            },
    41            spec: 'Ius te dicit probatus intellegebat, no minimum molestiae delicatissimi cum. Omnium officiis instructior ne mel,',
    42            status: 'OPEN',
    43            deliveredDate: '2017-05-20T16:47:49.016Z'
    44          }
    45        }
    46      });
    47    });
    48  }
    49  
    50  function getProject(projectId) {
    51    if (API_MOCK) {
    52      return getProjectMock(projectId);
    53    }
    54    return fetch(url.replace('{0}', projectId), {
    55      method: 'GET',
    56      headers: {
    57        'Accept': 'application/json'
    58      }
    59    })
    60      .then(handleApiError)
    61      .then(function(response) {
    62        return response.json();
    63      })
    64      .catch(function(error) {
    65        throw error;
    66      });
    67  }
    68  
    69  function* fetchProject(action) {
    70    try {
    71      yield put(showLoading());
    72      const response = yield call(getProject,action.projectId);
    73      yield put(hideLoading());
    74      yield put(fetchProjectSuccess(response.data['project']));
    75    }
    76    catch (err) {
    77      yield put(fetchProjectFailure(err));
    78      yield put(hideLoading());
    79    }
    80  }
    81  
    82  export default function* watchFetchProject() {
    83    yield takeLatest(FETCH_PROJECT, fetchProject);
    84  }