Skip to content

Instantly share code, notes, and snippets.

@closrks
closrks / actions.js
Last active December 23, 2016 02:05
Exploring consistent react-router hooks
import makeActionCreator from '../utils/makeActionCreator';
export const ENTER_PAGE_REQUEST = 'ENTER_PAGE_REQUEST';
export const enterPageRequest = makeActionCreator(ENTER_PAGE_REQUEST);
export const ENTER_PAGE_SUCCESS = 'ENTER_PAGE_SUCCESS';
export const enterPageSuccess = makeActionCreator(ENTER_PAGE_SUCCESS);
export const ENTER_PAGE_ERROR = 'ENTER_PAGE_ERROR';
export const enterPageError = makeActionCreator(ENTER_PAGE_ERROR);
@closrks
closrks / reset_pwd.js
Created December 17, 2016 05:30
An example of what I mean
export const handlers {
[SHOW_ERROR_MODAL]: {
// only called after specific case is proven not to exists
// the specific cases will exists below like such
},
/* RESET PASSWORD SPECIFIC STATE */
[RESET_PASSWORD_INITIAL_STATE]: {
// props = props sent from container
},
const openRollFowardModal = makeActionCreator('OPEN');
function openRollFowardModalAndFetchFiles() {
return dispatch => new Promise((resolve, reject) => {
dispatch(openRollFowardModal());
disaptch(fetchFilesRequest());
RollForwardAPI.fetchFiles()
.then(() => dispatch(fetchFilesSuccess())
.catch(() => dispatch(fetchFilesError('It looks like there no files to roll forward.', error);
});
export function getUsers() {
return dispatch => (
new Promise((resolve, reject) => {
dispatch(getUsersRequest());
UsersAPI.getUsers()
.then((data) => {
dispatch(getUsersSuccess(data));
resolve(data);
})
.catch((err) => {
@closrks
closrks / learned.js
Last active March 2, 2016 19:47
Guidelines
/*
Routes
1) should do finds
2) should do saves/updates
3) should handle control flow
4) should name anonymous functions in control flow
5) should namespace routers {Namespace}Router
--> PublicRouter, AdminRouter...
6) should have all of the routes in a top level namespace
--> sample route /{namespace}/:id/{namespace_sub_resource}
@closrks
closrks / mongoose-exec-stream.js
Last active January 7, 2016 09:58
mongoose exec vs stream
// callback
UserModel.find({}, function(err, results) {
if (err) throw err;
// do something with results
});
// exec
var query = UserModel.find({});
query.exec(function(err, results) {
if (err) throw err;
@closrks
closrks / mongoose-middleware.js
Created January 7, 2016 01:23
mongoose middleware examples
// complex validation
UserSchema.pre('save', function(next) {
UserModel.find({ username: this.username }, function(err, user) {
if (!user) {
return next();
}
return next(new Error('username already exist!'));
});
});
@closrks
closrks / spec
Created December 17, 2015 20:28
spec
'use strict';
// intregration test
var app = require('../../server');
var request = require('supertest');
var should = require('should');
var mongoose = require('mongoose');
describe('review-note', function() {
describe('GET /review_notes', function() {
@closrks
closrks / gist:74a261a6ce5c84fd63a5
Created January 28, 2015 21:42
turn flat array into tree
var data = ['a','b','c','d','e','f','g','h'];
var tree = {};
function isEmpty(data) { return data.length === 0; }
function isRoot(tree) { return !Object.keys(tree).length; }
function isSubtree(tree) { return Object.keys(tree).length; }
var makeTree = function (data, tree) {
if (isEmpty(data)) return tree;
@closrks
closrks / gist:e03b7e6d5c12a279db0a
Created November 13, 2014 01:04
recursive no global and addition
function add(a,b) {
if (a === 0) {
return b;
} else {
return add(--a, ++b)
}
}
alert(add(1,2))
alert(add(45,2))