@twreporter/redux
Version:
redux actions and reducers for twreporter website
203 lines (193 loc) • 7.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getUserData = getUserData;
exports.onboarding = onboarding;
exports.setUserData = setUserData;
var _url = require("../utils/url");
var _apiConfig = _interopRequireDefault(require("../constants/api-config"));
var _apiEndpoints = _interopRequireDefault(require("../constants/api-endpoints"));
var _errorActionCreators = _interopRequireDefault(require("./error-action-creators"));
var _reduxStateFieldNames = _interopRequireDefault(require("../constants/redux-state-field-names"));
var _actionTypes = _interopRequireDefault(require("../constants/action-types"));
var _axios = _interopRequireDefault(require("axios"));
var _get = _interopRequireDefault(require("lodash/get"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
// lodash
var _ = {
get: _get["default"]
};
var apiTimeout = _apiConfig["default"].timeout;
/**
* @typedef {Object} userData
* @property {string} email - email of user
* @property {string} id - id of user
* @property {string} jwt - json web token issued by backend for this certain user
*/
/**
* @typedef {Object} RequestAction
* @property {string} type - The type of action
* @property {string} payload.method - The HTTP request method
* @property {string} payload.url - The target to send request
* @property {Object} payload.body - The request body with POST, PUT, DELETE, or PATCH request
* @property {Object} payload.headers - The request header
*/
/**
* @typedef {Object} SuccessAction
* @property {string} type - The type of action
* @property {string} payload
* @property {Object} payload.data - Response data
* @property {string} payload.statusCode - Response status code
*/
/**
* @param {Object} axiosResponse
* @param {string} actionType
* @returns {SuccessAction}
*/
function buildSuccessActionFromRes(axiosResponse, actionType) {
return {
type: actionType,
payload: {
data: _.get(axiosResponse, 'data'),
statusCode: _.get(axiosResponse, 'status')
}
};
}
/**
* @typedef {Object} FailAction
* @property {string} type - Action error type
* @property {string} payload
* @property {string} payload.statusCode - Response status code
* @property {string} payload.message - Error message
* @property {object} payload.error - Error object
*/
/**
*
*
* @export
* @param {string} jwt - access_token granted for the user
* @param {number} userID - id of user
* @return {Function} - function will be executed in Redux Thunk middleware
*/
function getUserData(jwt, userID) {
/**
* @param {Function} dispatch - Redux store dispatch function
* @param {Function} getState - Redux store getState function
* @return {Promise} resolve with success action or reject with fail action
*/
return function (dispatch, getState) {
var state = getState();
var apiOrigin = _.get(state, [_reduxStateFieldNames["default"].origins, 'api']);
var url = (0, _url.formURL)(apiOrigin, "/v2/".concat(_apiEndpoints["default"].users, "/").concat(userID));
dispatch({
type: _actionTypes["default"].user.read.request,
url: url
});
var axiosConfig = {
timeout: apiTimeout,
headers: {
Authorization: "Bearer ".concat(jwt)
}
};
return _axios["default"].get(url, axiosConfig).then(function (res) {
var successAction = buildSuccessActionFromRes(res, _actionTypes["default"].user.read.success);
dispatch(successAction);
return successAction;
})["catch"](function (error) {
var failAction = _errorActionCreators["default"].axios(error, _actionTypes["default"].user.read.failure);
dispatch(failAction);
return Promise.reject(failAction);
});
};
}
/**
* @export
* @param {string} jwt - access_token granted for the user
* @param {number} userID - id of user
* @param {string[]} readPreference - subscribed topics
* @param {string[]} maillist - subscribed mail list
* @return {Function} - function will be executed in Redux Thunk middleware
*/
function setUserData(jwt, userID, readPreference, maillist) {
/**
* @param {Function} dispatch - Redux store dispatch function
* @param {Function} getState - Redux store getState function
* @return {Promise} resolve with success action or reject with fail action
*/
return function (dispatch, getState) {
var state = getState();
var apiOrigin = _.get(state, [_reduxStateFieldNames["default"].origins, 'api']);
var url = (0, _url.formURL)(apiOrigin, "/v2/".concat(_apiEndpoints["default"].users, "/").concat(userID));
dispatch({
type: _actionTypes["default"].user.update.request,
url: url
});
var axiosConfig = {
timeout: apiTimeout,
headers: {
Authorization: "Bearer ".concat(jwt)
}
};
var body = {
read_preference: readPreference,
maillist: maillist
};
return _axios["default"].post(url, body, axiosConfig).then(function (res) {
var successAction = buildSuccessActionFromRes(res, _actionTypes["default"].user.update.success);
dispatch(successAction);
return successAction;
})["catch"](function (error) {
var failAction = _errorActionCreators["default"].axios(error, _actionTypes["default"].user.update.failure);
dispatch(failAction);
return Promise.reject(failAction);
});
};
}
/**
* @export
* @param {string} jwt - access_token granted for the user
* @param {number} userID - id of user
* @param {string[]} readPreference - subscribed topics
* @param {string[]} maillist - subscribed mail list
* @param {string} destination - redirect destination
* @return {Function} - function will be executed in Redux Thunk middleware
*/
function onboarding(jwt, userID, readPreference, maillist, destination) {
/**
* @param {Function} dispatch - Redux store dispatch function
* @param {Function} getState - Redux store getState function
* @return {Promise} resolve with success action or reject with fail action
*/
return function (dispatch, getState) {
var state = getState();
var apiOrigin = _.get(state, [_reduxStateFieldNames["default"].origins, 'api']);
var url = (0, _url.formURL)(apiOrigin, "/v2/".concat(_apiEndpoints["default"].onboarding, "/").concat(userID), {
destination: destination
});
dispatch({
type: _actionTypes["default"].user.update.request,
url: url
});
var axiosConfig = {
timeout: apiTimeout,
headers: {
Authorization: "Bearer ".concat(jwt)
},
withCredentials: true
};
var body = {
read_preference: readPreference,
maillist: maillist
};
return _axios["default"].post(url, body, axiosConfig).then(function (res) {
var successAction = buildSuccessActionFromRes(res, _actionTypes["default"].user.update.success);
dispatch(successAction);
return successAction;
})["catch"](function (error) {
var failAction = _errorActionCreators["default"].axios(error, _actionTypes["default"].user.update.failure);
dispatch(failAction);
return Promise.reject(failAction);
});
};
}