Skip to content

Instantly share code, notes, and snippets.

@casesandberg
casesandberg / store.ts
Last active February 22, 2021 04:25
Redux Typekit API
import { createAction, createAsyncAction, createReducer, ActionReturnTypes, createSelector } from 'redux-typekit'
import { Issue } from '@mm-backend/issues/model'
export const selector = 'issues'
export const actions = {
add: createAction<'issues/ADD', { issue: Issue }>('issues/ADD'),
}
export type actions = ActionReturnTypes<typeof actions>
@casesandberg
casesandberg / untyped.js
Created August 11, 2019 02:21
Seems like I am doing a lot of boilerplate here for TypeScript, any suggestions on making it more concise?
// This is how I would traditionally do it
export const selector = 'toasts'
export const actionTypes = {
ADD: `${selector}/ADD`,
SHOW: `${selector}/SHOW`,
HIDE: `${selector}/HIDE`,
}
export const reducer = (state = {}, action) => {
// This is an automated testing file for this repo: https://github.com/ConsenSys-Academy/event-ticket-exercise
var EventTicketsV2 = artifacts.require('EventTicketsV2')
let catchRevert = require("./exceptionsHelpers.js").catchRevert
contract('EventTicketV2', function(accounts) {
const deployAccount = accounts[0]
const firstAccount = accounts[3]
const secondAccount = accounts[4]
@casesandberg
casesandberg / hn.json
Last active June 12, 2018 20:49
Interview Fixture Data - Hacker News Clone
{
"posts": [
{
"id": 17257143,
"title": "Learn Reinforcement Learning from scratch",
"points": 66,
"user": "e_ameisen",
"time": 1528387842,
"time_ago": "an hour ago",
"comments_count": 1,
// want to be able to ensure that the second param (key) exists as a key on the objects of the first type (array)
export const arrayToKeyedObj = (array: Array<{}>, key: string): {} => {
return array.reduce((acc, item) => {
acc[item[key]] = item
return acc
}, {})
}
// Uses Context and transforms string values ('tiny' => 4px)
connectToStyles(({ padding }) => {
table: {
...padding({ left: ‘tiny’ })
}
))
// Uses Context and passes values for use explicitly
connectToStyles(({ padding, sizes }) => {
table: {
@casesandberg
casesandberg / spec.js
Last active September 25, 2017 23:37
React Color Edible Input Focus Test
import { mount } from 'enzyme'
// Should Focus
const input = mount(<EdibleInput focusHexInputOnMount />);
assert(input.find('input').node === document.activeElement);
// Shouldn't Focus
const input = mount(<EdibleInput focusHexInputOnMoun={ false } />);
assert(input.find('input').node !== document.activeElement);
@casesandberg
casesandberg / components>tabs>Tabs.js
Last active April 18, 2017 00:49
Simplify Redux in React
import React from 'react'
import { connect } from 'react-redux'
import { store } from 'new-package'
import { reducer, path, actions, selectors } from './actions'
const Tabs ({ activeIndex, select }) => {
// Only register the reducer when tabs is used
store.register(path, reducer)
return (
@casesandberg
casesandberg / Popup.js
Last active April 28, 2018 10:11
Popups Managed Via Redux:
import React from 'react'
import reactCSS from 'reactcss'
import { connect } from 'react-redux'
import { actions, selectors } from './reducer'
export const Popup = ({ children, visible, onClose, name }) => {
const styles = reactCSS({
'default': {
wrap: {
position: 'absolute',
import React, { PropTypes } from 'react';
import { Hoverable } from '@bufferapp/redux-hover';
const MyComponent = ({
hovered, // managed by redux-hover
onMouseEnter,
onMouseLeave,
}) =>
<div
onMouseEnter={onMouseEnter}