Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home in Boston, sometimes in DC

Eric Brookfield ericdfields

View GitHub Profile
View prepareModelForRails.js
import reduce from 'lodash/reduce'
import isArray from 'lodash/isArray'
const prepareModelForRails = (model) => {
if (typeof model !== 'object') {
return model
} else {
if (isArray(model)) {
return model.map( m => prepareModelForRails(m) )
} else {
return reduce(model, (result, value, key) => {
View editPersonComponent.js
EditPersonForm = reduxForm({
form: 'person',
touchOnChange: true
})(EditPersonForm);
import { createStore, combineReducers } from 'redux'
import { reducer as formReducer } from 'redux-form'
const reducers = {
form: formReducer
View phoneNumbers.js
const renderPhoneNumbers = ({ fields, meta: { touched, error, submitFailed } }) => (
<ul>
<li>
<button type="button" onClick={() => fields.push({})}>Add Phone Number</button>
{(touched || submitFailed) && error && <span>{error}</span>}
</li>
{fields.map((member, index) => {
const field = fields.get(index)
if (field && field._destroy) {
return false
View form.js
class EditPersonForm extends Component {
render() {
const { handleSubmit } = this.props
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="first_name">First Name</label>
<Field name="first_name" component="input" />
</div>
<div>
View example.js
ajax
.post( parseHref(this.props) )
.setCsrfToken()
.send( updatedPayload(payload,entity) )
.end( (err,response) => {
if (err) {
return standardError()
}
this.setDisabled()
this.props.callback && this.props.callback()
View ajax.js
const ajax = require('superagent')
require('superagent-rails-csrf')(ajax)
export default ajax
@ericdfields
ericdfields / gist:3d4ed9c7f7b559289a102207facd61a7
Created Feb 3, 2017
Add multiple items to an Amazon Cart with a single button
View gist:3d4ed9c7f7b559289a102207facd61a7
I've seen links around the web for a list of items and a single 'add to amazon cart' button.
I don't know how to do this more easily through an amazon-provided UI, but it seems that you can hack it together through URL params easy enough, like so:
https://www.amazon.com/gp/aws/cart/add.html?AssociateTag=your_tag&tag=your_tagQ&ASIN.1=B012NH05UW&Quantity.1=1&ASIN.2=B012M8LXQW&Quantity.2=1
* ASINs are the string after /dp/ in amazon URLs. (amazon.com/dp/string_is_here)
* Add as URL params w/ incrementing identifiers and quantity couplets (ASIN.1, Quantity.1, ASIN.2, Quantity.2…)
@ericdfields
ericdfields / component.js
Last active Jan 10, 2017
Loading Inline SVG into React
View component.js
/*
Use CSS for styling via the awesome styled-components library
https://styled-components.com
*/
import styled from 'styled-components'
const LoadingImage = require('loading-svg/loading-spin.svg')
const LoadingComponent = styled(LoadingImage)`
@ericdfields
ericdfields / HOCComponents.js
Last active Sep 28, 2016
Replace nested react component using higher order component
View HOCComponents.js
export class FaxDocumentViewerApp extends Component {
defaultProps = {
documentTitle: (doc) => `${doc.fax_type}${moment(doc.created_at).format('MMM D YYYY [@] h:mm a')}`
}
render = () => {
<OriginalComponent {...this.props} />
}
}
@ericdfields
ericdfields / app.js
Last active Aug 29, 2015
Resolving local UI state with Flux using Flummox and Promises
View app.js
import Flux from 'path/to/flux.js'
window.MyApp.flux = new Flux()
You can’t perform that action at this time.