Created
November 20, 2018 12:45
-
-
Save pettaysergey/b9dddffe4d3e63a5bffd5fd25f9432d0 to your computer and use it in GitHub Desktop.
Foto-Print
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { all, call, put, select, takeEvery } from 'redux-saga/effects'; | |
| import { showError, showSuccess, hideSuccess } from 'src/app/containers/App/duck'; | |
| import { callAPI } from 'src/app/containers/App/saga'; | |
| import { getCompanyId } from 'src/app/containers/App/selectors'; | |
| import { API } from 'src/utils'; | |
| import * as duck from './duck'; | |
| import { getUserRole } from 'src/app/containers/Page/selectors'; | |
| import { getFotoServices } from './selectors'; | |
| const get = require('lodash/get'); | |
| const isEmpty = require('lodash/isEmpty'); | |
| export function* getPhotoPoints(action) { | |
| const { payload } = action; | |
| try { | |
| const { data, status } = yield call( | |
| callAPI, | |
| `/crm/units/pagination?length=${payload.limit}&offset=${payload.offset}` | |
| ); | |
| if (status === 200) { | |
| const { companyUnitsCount } = data; | |
| const companyUnits = get(data, 'companyUnits', []); | |
| const ids = companyUnits.map(i => i.id); | |
| const byIds = companyUnits.reduce((sum, unit) => { | |
| sum[unit.id] = unit; | |
| return sum; | |
| }, {}); | |
| yield put( | |
| duck.receivePhotoPointsSuccess({ | |
| ids, | |
| byIds, | |
| companyUnitsCount | |
| }) | |
| ); | |
| } else { | |
| yield put(showError('Ошибка при получении данных о фототочках')); | |
| } | |
| } catch { | |
| yield put(showError('Ошибка при получении данных о фототочках')); | |
| } | |
| } | |
| export function* postFotopoint(action) { | |
| const { payload } = action; | |
| try { | |
| const companyId = yield select(getCompanyId); | |
| const res = yield call(callAPI, `${API.addFotopoit}${companyId}`, { | |
| method: 'POST', | |
| data: payload | |
| }); | |
| if (res.status === 200) { | |
| const companyUnits = get(res, 'data.companyUnitModels', []); | |
| const ids = companyUnits.map(i => i.id); | |
| const byIds = companyUnits.reduce((sum, unit) => { | |
| sum[unit.id] = unit; | |
| return sum; | |
| }, {}); | |
| yield put( | |
| duck.addFotopointsSuccess({ | |
| ids, | |
| byIds | |
| }) | |
| ); | |
| } else { | |
| yield put(duck.addFotopointsFailure()); | |
| } | |
| } catch (e) { | |
| yield all([ | |
| put(showError('Ошибка при добавлении фотосалона')), | |
| put(duck.addFotopointsFailure()) | |
| ]); | |
| } | |
| } | |
| export function* updateFotopoint(action) { | |
| const { | |
| payload: { name, value, id } | |
| } = action; | |
| let data; | |
| if (name === 'salonName') { | |
| data = { name: value }; | |
| } else if (name === 'location') { | |
| data = { addressDescription: value }; | |
| } else if (name === 'address') { | |
| data = { ...value }; | |
| } else { | |
| data = { [name]: value }; | |
| } | |
| const role = yield select(getUserRole); | |
| if (!['admin', 'superAdmin'].includes(role)) { | |
| yield put(showError('У вас нет прав для редактирования')); | |
| return; | |
| } | |
| try { | |
| const res = yield call(callAPI, `${API.editFotopoint}${id}`, { | |
| method: 'PUT', | |
| data | |
| }); | |
| if (res.status === 200) { | |
| yield put(hideSuccess()); | |
| yield all([ | |
| put(showSuccess('Данные фотосалона обновлены')), | |
| put(duck.editFotopointSuccess(res.data)) | |
| ]); | |
| } | |
| } catch (e) { | |
| yield put(showError('Ошибка при редактировании фотосалона')); | |
| } | |
| } | |
| // get services types for creating prices | |
| export function* fetchFotoServices() { | |
| const url = API.getFotoServices; | |
| const fotoServices = yield select(getFotoServices); | |
| if (isEmpty(fotoServices)) { | |
| try { | |
| const res = yield call(callAPI, url); | |
| if (res.status === 200) { | |
| const resToObject = res.data.serviceTypes.reduce( | |
| (obj, item) => ({ | |
| ...obj, | |
| [item.serviceName]: item | |
| }), | |
| {} | |
| ); | |
| yield put(duck.getFotoServicesSuccess(resToObject)); | |
| } else { | |
| yield put(showError('Ошибка при получении услуг')); | |
| } | |
| } catch (e) { | |
| yield put(showError('Ошибка при получении услуг')); | |
| } | |
| } | |
| } | |
| // add price service to unit | |
| function* postPriceServices(action) { | |
| const { | |
| payload: { unitId, data } | |
| } = action; | |
| const url = `${API.postNewPriceService}${unitId}`; | |
| try { | |
| const res = yield call(callAPI, url, { | |
| method: 'POST', | |
| data | |
| }); | |
| if (res.status === 200) { | |
| yield put(duck.postPriceServicesSuccess({ data: res.data, unitId })); | |
| } else { | |
| yield put(showError('Ошибка при добавлении услуги')); | |
| } | |
| } catch (e) { | |
| yield put(showError('Ошибка при добавлении услуги')); | |
| } | |
| } | |
| // update price service | |
| function* putPriceServices(action) { | |
| const { | |
| payload: { id, data, unitId } | |
| } = action; | |
| const url = `${API.putPriceService}${id}`; | |
| try { | |
| const res = yield call(callAPI, url, { | |
| method: 'PUT', | |
| data | |
| }); | |
| if (res.status === 200) { | |
| yield all([ | |
| put(hideSuccess()), | |
| put(duck.postPriceServicesSuccess({ data: res.data, id, unitId })), | |
| put(showSuccess('Услуга изменена')) | |
| ]); | |
| } else { | |
| yield put(showError('Ошибка при редактировании услуги')); | |
| } | |
| } catch (e) { | |
| yield put(showError('Ошибка при редактировании услуги')); | |
| } | |
| } | |
| export function* getFilterPhotoPointsWorker(action) { | |
| try { | |
| const { | |
| payload: { params } | |
| } = action; | |
| const response = yield call(callAPI, '/crm/units/pagination/filters', { | |
| method: 'GET', | |
| params | |
| }); | |
| if (response.status === 200) { | |
| const value = response.data.companyUnitsCount; | |
| const data = response.data.companyUnits; | |
| const ids = data.map(item => item.id); | |
| const normalizedOrders = data.reduce( | |
| (acc, item) => Object.assign(acc, { [item.id]: item }), | |
| {} | |
| ); | |
| yield put(duck.getFilteredPhotoValue(value)); | |
| yield put( | |
| duck.loadFilteredPPSuccess({ | |
| ids, | |
| orders: normalizedOrders | |
| }) | |
| ); | |
| } else { | |
| put(showError('Ошибка получения данных о фотосалонах')); | |
| } | |
| } catch (error) { | |
| put(showError('Ошибка получения данных о фотосалонах')); | |
| } | |
| } | |
| export function* getCityWorker(action) { | |
| try { | |
| const data = action.payload; | |
| const response = yield call(callAPI, '/cities/geo', { | |
| method: 'GET', | |
| params: data | |
| }); | |
| yield put(duck.setInitialCityValue(data)); | |
| const responseData = response.data; | |
| if (response.status === 200) { | |
| yield put(duck.getCitySuccess(responseData.cityName)); | |
| } else if (response.status === 400) { | |
| yield put(duck.getCitySuccess(data.name)); | |
| yield put(duck.openPopupCity()); | |
| } | |
| } catch (error) { | |
| yield put(duck.getCityFailure()); | |
| } | |
| } | |
| export function* addNewCityWorker(action) { | |
| try { | |
| const { payload } = action; | |
| const res = yield call(callAPI, '/cities/add', { | |
| method: 'POST', | |
| data: payload | |
| }); | |
| if (res.status === 200) { | |
| yield put(duck.addNewCitySuccess(res.data.cityName)); | |
| } | |
| } catch (error) { | |
| put(showError('Ошибка добавления города')); | |
| } | |
| } | |
| export default function* rootSaga() { | |
| yield takeEvery(duck.REQUEST_PHOTO_POINTS, getPhotoPoints); | |
| yield takeEvery(duck.SEND_FOTOPOINT, postFotopoint); | |
| yield takeEvery(duck.EDIT_FOTOPOINT_REQUEST, updateFotopoint); | |
| yield takeEvery(duck.REQUEST_FOTO_SERVICES, fetchFotoServices); | |
| yield takeEvery(duck.POST_PRICE_SERVICES, postPriceServices); | |
| yield takeEvery(duck.PUT_PRICE_SERVICES, putPriceServices); | |
| yield takeEvery(duck.FILTERED_PP_LIST_REQEUST, getFilterPhotoPointsWorker); | |
| yield takeEvery(duck.GET_CITY_REQUEST, getCityWorker); | |
| yield takeEvery(duck.ADD_NEW_CITY, addNewCityWorker); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment