Skip to content

Instantly share code, notes, and snippets.

View rosschapman's full-sized avatar

Ross Chapman rosschapman

View GitHub Profile
@rosschapman
rosschapman / light-handler-with-validator.js
Last active April 23, 2020 03:57
Light handler with single dispatch and validator
let isValid = (data) => {...}
let submitEntityForm = (data) => {
if (!isValid(data)) {
// add error notification...
};
dispatch('SUBMIT_ENTITY_FORM', data);
}
@rosschapman
rosschapman / light-handler-single-dispatch-ex.js
Last active April 22, 2020 22:36
Light handler with a single dispatch
let submitEntityForm = (data) => {
dispatch('SUBMIT_ENTITY_FORM', data);
}
let MyFormComponent = () => {
return {
<Form>
<Button type={'submit'} onClick={submitEntityForm}/>
</Form>
}
@rosschapman
rosschapman / SketchSystems.spec
Last active July 9, 2020 18:19
UCM React Basic Example
UCM React Basic Example
IDLE
input -> ACTIVE
ACTIVE
submit -> WAITING
blur -> IDLE
WAITING
invalid -> FORM_INVALID
resolve -> SUCCESS
reject -> HAS_ERROR
@rosschapman
rosschapman / weakly-bound-form-cb-test.js
Last active April 17, 2020 07:14
Weakly Bound Form Callback Test
describe('when editing an entity', () => {
it('posts the entity form and does all the right stuff afterward', () => {
stub(myModule, 'prePopulateForm');
dispatch = jest.fn();
postEntityForm();
expect(dispatch).toHaveBeenCalledTimes(6);
expect(prePopulateForm).toHaveBeenCalledTimes(1)
});
@rosschapman
rosschapman / weakly-bound-form-cb.js
Last active April 22, 2020 22:42
Weakly Bound Form Callback
// This is oversimplified. The real code for this callback would be a complicated graph
// of nested asynchronous and synchronous calls. Imagine at the edge of thes thunks each
// dispatched action mutates state.
let postEntityForm = (e, data) => {
await dispatch(saveEntity(data));
let entities = await dispatch(fetchEntities());
let taxPolicy = await dispatch(maybeFetchEntityTaxPolicy());
await dispatch(maybeUpdateEntityPriceSuggestions(taxPolicy, entities));
let isEditing = dispatch(getIsEditingFromState());
@rosschapman
rosschapman / rep-ex-3.jsx
Last active April 6, 2020 16:05
Repetitive example 3
handleCampaignClick = (campaign, actionName) => {
if (actionName === 'activate') {
const campaigns = this.props.campaigns;
const hasNoActivated = !campaigns.some(c => c.activated);
if (hasNoActivated) {
updateCampaignAndUpdateFeaturedCampaigns({
id: campaign.id,
activated: true
});
@rosschapman
rosschapman / rep-ex-2.jsx
Last active April 8, 2020 13:37
Repetition Example 2
handleCampaignClick = (campaign, actionName) => {
if (actionName === 'activate') {
const campaigns = this.props.campaigns;
const hasNoActivated = !campaigns.some(c => c.activated);
if (hasNoActivated) {
updateCampaign({
id: campaign.id,
value: true
});
@rosschapman
rosschapman / rep-ex-1.jsx
Last active April 6, 2020 03:37
Repetition Example 1
handleCampaignClick = (campaign, actionName) => {
if (actionName === 'activate') {
updateCampaign({
id: campaign.id,
value: true,
});
}
if (actionName === 'makeFeatured') {
updateFeaturedCampaigns({
@rosschapman
rosschapman / thunk-with-async-await-error.js
Last active July 14, 2019 16:40
Thunk "transaction" with synchronous error
export const saveStuffThunk = async (formData) => {
async(dispatch, getState) => {
try {
await saveParentEntity(getParentEntityFromForm(formData));
} catch (e) {
throw SubmissionError(e);
}
try {
await saveChildEntity(getChildEntityFromForm(formData));
@rosschapman
rosschapman / decompose-conditional-eg-result.js
Last active February 1, 2019 17:05
decompose-conditional-eg-result.js
import InventoryManagementView from '../components/InventoryManagementView'
import {eventIsSellable} from '../selectors/event';
const userCanSellEvent = eventIsSellable(this.props.event);
if (userCanSellEvent) {
return InventoryManagementView;
}