Skip to content

Instantly share code, notes, and snippets.

@james-gardner
james-gardner / createCrudHooks.js
Created March 8, 2022 11:23 — forked from anthowen/createCrudHooks.js
A naive, but efficient starter to generate crud hooks for React Query
export default function createCrudHooks({
baseKey,
indexFn,
singleFn,
createFn,
updateFn,
deleteFn,
}) {
const useIndex = (config) => useQuery([baseKey], indexFn, config)
const useSingle = (id, config) =>
import React, { useReducer, useState, useEffect } from 'react'
import PropTypes from 'prop-types'
/** */
export const articleReducer = (state, action) => {
switch(action.type) {
case 'SET_CURRENT_ARTICLE':
return {
...state,
currentArticleId: action.payload.articleId
@james-gardner
james-gardner / SomethingUsefulProvider.js
Last active August 10, 2020 08:10
useReducer + context
import React, { useReducer, useContext, createContext } from 'react'
import PropTypes from 'prop-types'
const SomethingUsefulContext = createContext()
export const reducer = ( state, action ) => {
switch(action.type) {
default:
throw new Error(`Unrecognised action type: ${action.type} `)
}
@james-gardner
james-gardner / module.js
Created March 17, 2020 16:40
useReducer vs Custom Hook
import React, { createContext, useReducer, useContext } from 'react'
import PropTypes from 'prop-types'
const ServiceRequestContext = createContext()
export const initialState = {
serviceRequests: new Map(),
selectedTicketId: null,
pendingRequest: null
}
@james-gardner
james-gardner / hooks.js
Created March 17, 2020 15:33 — forked from fnky/hooks.js
React Hooks: useReducer with actions and selectors (Redux-like)
function useSelectors(reducer, mapStateToSelectors) {
const [state] = reducer;
const selectors = useMemo(() => mapStateToSelectors(state), [state]);
return selectors;
}
function useActions(reducer, mapDispatchToActions) {
const [, dispatch] = reducer;
const actions = useMemo(() => mapDispatchToActions(dispatch), [dispatch]);
return actions;
@james-gardner
james-gardner / http.js
Created July 26, 2019 09:14
Example HTTP Call
function getFruit() {
try {
// Axios will trip on all HTTP error codes, pushing you into the catch.
const res = await axios.get('http://www.example.fake/fruit');
} catch (err) {
// Examine error for http status code.
if (err.status) {
// Give more explanation if the API provides it.
throw new Error(`Call failed because: $err.status`);
}
@james-gardner
james-gardner / dnsmasq macOS.md
Created July 11, 2019 10:09 — forked from brablc/dnsmasq macOS.md
Setup dnsmasq on OS X

Never touch your local /etc/hosts file in OS X again

To setup your computer to work with *.dev domains, e.g. project.dev, awesome.dev and so on, without having to add to your hosts file each time.

Requirements

Install

@james-gardner
james-gardner / redact.js
Created July 8, 2018 19:00
MongoDB Redact
db.request_log__v2_0.aggregate([{
"$redact": {
"$cond": [{
"$and": [{
"$eq": [{
"$year": "$created_at"
}, 2018]
}, {
"$eq": [{
"$month": "$created_at"
@james-gardner
james-gardner / Strategy.js
Last active June 1, 2018 09:02
Custom passport strategy
const passport = require('passport-strategy');
const url = require('url');
const querystring = require('querystring');
const util = require('util');
const axios = require('axios');
/**/
function TenantStrategy(options, verify) {
this.name = 'tenant';
@james-gardner
james-gardner / 1_kubernetes_on_macOS.md
Created February 26, 2018 20:53 — forked from kevin-smets/1_kubernetes_on_macOS.md
Local Kubernetes setup on macOS with minikube on VirtualBox and local Docker registry

Requirements

Minikube requires that VT-x/AMD-v virtualization is enabled in BIOS. To check that this is enabled on OSX / macOS run:

sysctl -a | grep machdep.cpu.features | grep VMX

If there's output, you're good!

Prerequisites