Skip to content

Instantly share code, notes, and snippets.

View asherccohen's full-sized avatar

Asher Cohen asherccohen

View GitHub Profile
@asherccohen
asherccohen / gist:c9eda21ea68f270c96968b6c34823ce4
Created April 18, 2024 04:51
Zustand context not finished
import { createContext, PropsWithChildren, useContext, useRef } from "react";
import { createStore, Mutate, useStore } from "zustand";
type TState = Record<string, unknown>;
type TActions<State extends TState, P = unknown> = Record<
string,
(state: State, payload?: P) => Mutate<State, any>
>;
import {
createContext,
PropsWithChildren,
ReducerAction,
useContext,
useMemo,
useRef,
} from "react";
import { createStore, useStore } from "zustand";
import { devtools, redux } from "zustand/middleware";
{
"root": true,
"plugins": [
"@nx",
"prettier",
"prefer-arrow",
"simple-import-sort",
"fp-ts-strict",
"fp"
],
@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;
@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 / 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 / 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
}
}
}
function makeActionCreator(type, ...argNames) {
return function(...args) {
const action = { type }
argNames.forEach((arg, index) => {
action[argNames[index]] = args[index]
})
return 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)) {
@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]);
};