github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/blockapps-ba-master/ui/src/scenes/Projects/components/ProjectList/project-list.saga.js (about) 1 import { 2 takeEvery, 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_LIST, 15 fetchProjectListSuccess, 16 fetchProjectListFailure 17 } from './project-list.actions'; 18 import { showLoading, hideLoading } from 'react-redux-loading-bar'; 19 20 const url = API_URL + '/projects?{0}'; 21 22 23 // TODO: move to utils and use it everywhere 24 function getProjectsMock() { 25 return new Promise(function(resolve, reject) { 26 resolve({ 27 data: { 28 projects: [ 29 { 30 id: 132, 31 created: '2017-05-09T16:47:49.016Z', 32 buyer: 'buyer1', 33 name: 'T-Shirts with logo', 34 description: 'The T-Shirts with our company\'s logo on the chest, Qty: 50', 35 priceDesired: 800.10, 36 desiredDeliveryDate: '2017-05-20T16:47:49.016Z', 37 deliveryAddress: { 38 street: '109 S 5th street', 39 city: 'Brooklyn', 40 state: 'New York', 41 zip: '11249' 42 }, 43 spec: 'Lorem ipsum dolor sit amet, eam molestie singulis referrentur', 44 state: 'OPEN', 45 deliveredDate: null 46 }, 47 { 48 id: 1431, 49 created: '2017-05-09T16:47:49.016Z', 50 buyer: 'buyer2', 51 name: 'NY Yankees sleeve', 52 description: 'Sleeve with New York Yankees logos all over it', 53 priceDesired: 10.2, 54 desiredDeliveryDate: '2017-05-17T10:32:01.016Z', 55 deliveryAddress: { 56 street: '109 South 5th st.', 57 city: 'Brooklyn', 58 state: 'NY', 59 zip: '11249' 60 }, 61 spec: 'Et qui altera assentior reformidans, cum case augue te. Ius te dicit probatus intellegebat, no minimum', 62 state: 'RECEIVED', 63 deliveredDate: '2017-05-18T12:35:00.000Z' 64 } 65 ] 66 } 67 }); 68 }); 69 } 70 71 function getProjectList(listType, username) { 72 if (API_MOCK) { 73 return getProjectsMock(); 74 } 75 let query; 76 switch (listType) { 77 case 'buyer': 78 query = 'filter=buyer&buyer=' + username; 79 break; 80 case 'open': 81 query = 'filter=state&state=1'; // state 1 is 'OPEN' 82 break; 83 case 'supplier': 84 query = 'filter=supplier&supplier=' + username; 85 break; 86 default: 87 query = ''; 88 } 89 90 return fetch( 91 url.replace('{0}', query), 92 { 93 method: 'GET', 94 headers: { 95 'Accept': 'application/json' 96 }, 97 }) 98 .then(handleApiError) 99 .then(function(response) { 100 return response.json(); 101 }) 102 .catch(function(error) { 103 throw error; 104 }); 105 } 106 107 function* fetchProjectList(action) { 108 try { 109 yield put(showLoading()); 110 let response = yield call(getProjectList, action.listType, action.username); 111 yield put(hideLoading()); 112 yield put(fetchProjectListSuccess(action.listType, response.data['projects'])); 113 } 114 catch (err) { 115 yield put(fetchProjectListFailure(err)); 116 yield put(hideLoading()); 117 } 118 } 119 120 export default function* watchFetchProjectList() { 121 yield takeEvery(FETCH_PROJECT_LIST, fetchProjectList); 122 }