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 { 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> | |
| >; |
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 { | |
| createContext, | |
| PropsWithChildren, | |
| ReducerAction, | |
| useContext, | |
| useMemo, | |
| useRef, | |
| } from "react"; | |
| import { createStore, useStore } from "zustand"; | |
| import { devtools, redux } from "zustand/middleware"; |
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
| { | |
| "root": true, | |
| "plugins": [ | |
| "@nx", | |
| "prettier", | |
| "prefer-arrow", | |
| "simple-import-sort", | |
| "fp-ts-strict", | |
| "fp" | |
| ], |
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
| const array = [ | |
| { id: 1, | |
| name: 'Bob' }, | |
| { id: 2, | |
| name: 'Lucy' }, | |
| { id: 3, | |
| name: 'John' } | |
| ]; | |
| const [first, ...rest] = array; |
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
| const secureRedirectMiddleware = ({ dev }) => (req, res, next) => { | |
| if(dev || req.headers['x-forwarded-proto'] === 'https'){ | |
| next(); | |
| } else { | |
| res.redirect(301, `https://${req.hostname}${req.originalUrl}`); | |
| } | |
| }; |
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
| 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(); |
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
| function createReducer(initialState, handlers) { | |
| return function reducer(state = initialState, action) { | |
| if (handlers.hasOwnProperty(action.type)) { | |
| return handlers[action.type](state, action) | |
| } else { | |
| return state | |
| } | |
| } | |
| } |
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
| function makeActionCreator(type, ...argNames) { | |
| return function(...args) { | |
| const action = { type } | |
| argNames.forEach((arg, index) => { | |
| action[argNames[index]] = args[index] | |
| }) | |
| return action | |
| } | |
| } |
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
| 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)) { |
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
| 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]); | |
| }; |
NewerOlder