Skip to content

Instantly share code, notes, and snippets.

View afraser's full-sized avatar

Adam Fraser afraser

  • Rocket Insights
  • Newburyport, MA
View GitHub Profile
@afraser
afraser / api-client.js
Last active July 28, 2022 16:01
api client that makes fetch work more like axios + cognito for auth
import Auth from '@aws-amplify/auth'
/**
USAGE:
> api.post(`users/${id}`, { email: 'asdf@asdf.com' })
> api.get('users/${id}')
> api.patch(`users/${id}`, { email: 'adam@asdf.com' })
> api.delete(`users/${id}`)
*/
class ApiClient {
@afraser
afraser / api.service.ts
Created July 28, 2022 15:52
Axios api wrapper using CancelTokenSource (Deprecated - see https://axios-http.com/docs/cancellation)
import axios, { CancelTokenSource } from 'axios'
import TokenService from './token.service'
export const api = axios.create({
baseURL: process.env.REACT_APP_API_BASE_URL || '',
withCredentials: true,
})
const requestCancelSourcesByKey: { [cancelKey: string]: CancelTokenSource } = {}
@afraser
afraser / useForm.jsx
Created June 29, 2022 18:52
simple form hook
import { useEffect, useMemo, useState } from 'react'
import {
get, set, every, isPlainObject, cloneDeep, cloneDeepWith, isEqual
} from 'lodash'
import { resolver } from 'js/util/validators'
const parseIntOrNull = (val) => {
const intVal = parseInt(val)
return isNaN(intVal) ? null : intVal
}
@afraser
afraser / apiclient.js
Created July 8, 2021 15:16
An axios-like wrapper around the fetch api that automatically aborts requests under a duplicate key. Provides an abort helper, and handles auth via aws-amplify.
import Auth from '@aws-amplify/auth'
/**
USAGE:
> api.post(`users/${id}`, { email: 'asdf@asdf.com' })
> api.get('users/${id}')
> api.patch(`users/${id}`, { email: 'adam@asdf.com' })
> api.delete(`users/${id}`)
*/
class ApiClient {
@afraser
afraser / FileDropzone.js
Created September 13, 2019 15:46
FileDropzone
import React, { useState, useEffect, useRef } from 'react'
import cx from 'classnames'
import css from './FileDropzone.sass'
export function FileDropzone ({ onDrop, children }) {
const [dragging, setDragging] = useState(0)
const dropRef = useRef()
const handleDrag = evt => {
@afraser
afraser / draftjs_inline_equations.js
Last active May 11, 2021 02:05
Demonstrates inline equations with copy/paste supported in draft.js
<!--
Copyright (c) 2013-present, Facebook, Inc. All rights reserved.
This file provided by Facebook is for non-commercial testing and evaluation
purposes only. Facebook reserves all rights not expressly granted.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
import React, { Component, PropTypes } from 'react'
import Toolbar from 'containers/Toolbar'
import InlineMath, { forceUpdateEquation } from 'containers/InlineMath'
import EquationEditor from 'components/EquationEditor'
import { Editor, EditorState, ContentState, SelectionState, Entity, CompositeDecorator, Modifier, convertToRaw, RichUtils } from 'draft-js'
const SUPPORTED_COMMANDS = [ 'italic' ]
function findTex(contentBlock, callback) {
contentBlock.findEntityRanges(
@afraser
afraser / cookie_monster.coffee
Last active August 29, 2015 14:16
How about a Backbone model as an interface for setting and getting guest cookies?
@afraser
afraser / dabblet.css
Created August 3, 2012 13:13
Scroll bug
/**
* Scroll bug
*/
*{
box-sizing: border-box;
}
input, textarea{
box-sizing: content-box;
}
@afraser
afraser / dabblet.css
Created August 3, 2012 12:59
The first commented line is your dabblet’s title
/**
* The first commented line is your dabblet’s title
*/
*{
+box-sizing(border-box)
}
input, textarea
+box-sizing(content-box)
.menu{x-sizing(border-box)