Skip to content

Instantly share code, notes, and snippets.

View asherccohen's full-sized avatar

Asher Cohen asherccohen

View GitHub Profile
@asherccohen
asherccohen / LoadingReducer.js
Created July 26, 2019 11:13
LoadingReducer
export default (state = {}, action) => {
const { type } = action;
const matches = /(.*)_(REQUEST|SUCCESS|FAILURE)/.exec(type);
// not a *_REQUEST / *_SUCCESS / *_FAILURE actions, so we ignore them
if (!matches) return state;
const [, requestName, requestState] = matches;
return {
…state,
// Store whether a request is happening at the moment or not
// e.g. will be true when receiving FETCH_TODOS_REQUEST
import uuid from 'uuid';
export default (state = {}, action) => {
const { type, payload } = action;
const matches = /(.*)_(REQUEST|FAILURE)/.exec(type);
// not a *_REQUEST / *_FAILURE actions, so we ignore them
if (!matches) return state;
const [, requestName, requestState] = matches;
return {
…state,
// Store errorMessage
@asherccohen
asherccohen / LoadingSelector.js
Created July 26, 2019 11:16
LoadingSelector
export const loadingSelector = (state, actions) => {
const types = actions.map(type => {
const matches = /(.*)_(REQUEST|SUCCESS|FAILURE)/.exec(type);
if (!matches) return type;
const [, requestName] = matches;
return requestName;
});
return types.some(action => state.fetching[action]);
};
@asherccohen
asherccohen / ErrorsSelector.js
Created July 26, 2019 11:19
ErrorsSelector
export const errorByActionSelector = (state = {}, action = '') => {
if (state.errors[action] && state.errors[action].message) {
return { [action]: state.errors[action] };
}
return undefined;
};
export const errorsSelector = (state = {}, actions = []) => {
const errors = actions.reduce((accumulator, action) => {
if (errorByActionSelector(state, action)) {
function makeActionCreator(type, ...argNames) {
return function(...args) {
const action = { type }
argNames.forEach((arg, index) => {
action[argNames[index]] = args[index]
})
return action
}
}
@asherccohen
asherccohen / reducerCreator.js
Created July 26, 2019 11:21
reducerCreator
function createReducer(initialState, handlers) {
return function reducer(state = initialState, action) {
if (handlers.hasOwnProperty(action.type)) {
return handlers[action.type](state, action)
} else {
return state
}
}
}
@asherccohen
asherccohen / secureRedirectMiddleware.js
Created December 17, 2019 08:44
secure-middleware
const secureRedirectMiddleware = ({ port, dev }) => (req, res, next) => {
if (!dev && !req.secure) {
const protocol = dev ? 'http' : 'https';
const portNumber = dev ? `:${port}` : '';
const url =`${protocol}://${req.hostname}${portNumber}${req.originalUrl}`;
res.redirect(301, url);
}
else {
next();
@asherccohen
asherccohen / secureRedirectMiddlewareHeroku.js
Created December 17, 2019 08:49
secure-redirect-middleware-heroku
const secureRedirectMiddleware = ({ dev }) => (req, res, next) => {
if(dev || req.headers['x-forwarded-proto'] === 'https'){
next();
} else {
res.redirect(301, `https://${req.hostname}${req.originalUrl}`);
}
};
@asherccohen
asherccohen / changeObjInArray.js
Created December 17, 2019 12:45
Use spread operator to change an element in an array of objects
const array = [
{ id: 1,
name: 'Bob' },
{ id: 2,
name: 'Lucy' },
{ id: 3,
name: 'John' }
];
const [first, ...rest] = array;
{
"root": true,
"plugins": [
"@nx",
"prettier",
"prefer-arrow",
"simple-import-sort",
"fp-ts-strict",
"fp"
],