Skip to content

Instantly share code, notes, and snippets.

@HERRKIN
HERRKIN / Luminux.js
Created April 4, 2018 16:08
Test code file for Luminux, should be run with babel-node
const binarySearch = (items, x) => {
let low = 0
let high = items.length - 1
while (low <= high) {
const mid = parseInt((low + high) / 2, 10)
const current = items[mid]
if (current > x) {
high = mid - 1
} else if (current < x) {
low = mid + 1
@HERRKIN
HERRKIN / drawer.action.js
Last active March 29, 2018 17:04
linkTo action for drawer links
export default function* linkTo({ payload }) {
const { route, params } = payload
const action = ['Products', 'Configuration', 'Profile'].includes(route)
? NavigationActions.reset({
index: 0,
key: route,
actions: [NavigationActions.navigate({ routeName: route, params })],
})
: NavigationActions.navigate({ routeName: route, params })
yield put(action)
@HERRKIN
HERRKIN / RouteConnect.js
Created March 29, 2018 16:55
HOC that avoids rendering or update unless necesary when using react-navigation
import React, { Component } from 'react'
import { connect } from 'react-redux'
import _ from 'lodash'
export const renderer = (route, Wrapped) =>
class extends Component {
shouldComponentUpdate(props) {
if (!props.render || this.props.currentRoute === 'DrawerOpen') { return false }
const thisProps = JSON.stringify(_.omit(this.props, ['nav', 'navigation', 'screenProps']))
const nextProps = JSON.stringify(_.omit(props, ['nav', 'navigation', 'screenProps']))
@HERRKIN
HERRKIN / apollo.js
Created February 3, 2017 16:03
apollo configuration file
import ApolloClient, {createNetworkInterface} from 'apollo-client'
import {addTypenameToSelectionSet} from 'apollo-client/queries/queryTransform'
import baseURL from './url' // we could use a string url here for example localhost:port/
const getLoginToken = function () {
return window.localStorage['Meteor.loginToken']
}
export const createMeteorNetworkInterface = () => {
let uri = baseURL + '/graphql'
@HERRKIN
HERRKIN / index.js
Created January 17, 2017 22:52
Mutations index.js adding toggleCompleted
import {Resolvers as Auth} from 'meteor/nicolaslopezj:apollo-accounts'
import addTodo from './addTodo'
import toggleCompleted from './toggleCompleted'
export default {
...Auth(),
addTodo,
toggleCompleted
}
@HERRKIN
HERRKIN / toggleCompleted.js
Created January 17, 2017 22:40
toggleCompleted Mutation
import {Todos} from '../../../../lib/Collections'
export default function (root, options, context) {
console.log(options)
if (!context.userId) {
throw new Error('Unknown User (not logged in)')
}
// validate the todo belongs to the user
const todo = Todos.findOne({$and: [{_id: options.todoId}, {owner: context.userId}]})
// if not found throw Error
if (!todo) {
@HERRKIN
HERRKIN / index.js
Created January 17, 2017 21:49
updated Mutation index.js
import {SchemaMutations as Auth} from 'meteor/nicolaslopezj:apollo-accounts'
import addTodo from './addTodo.graphql'
import toggleCompleted from './toggleCompleted.graphql'
export default `
type Mutation {
${Auth()}
${addTodo}
${toggleCompleted}
}
`
@HERRKIN
HERRKIN / index.js
Created January 10, 2017 23:07
Resolvers index.js
import Query from './Query'
import Mutation from './Mutations'
import User from './User'
export default {
...User,
Query,
Mutation
}
@HERRKIN
HERRKIN / User.js
Last active January 13, 2017 15:15
User resolver for the tutorial
import {Todos} from '../../../../lib/Collections'
export default {
User: {
todos (user) {
return Todos.find({owner: user._id}).fetch()
}
}
}
@HERRKIN
HERRKIN / addTodo.js
Last active January 13, 2017 15:16
addTodo resolver
import {Todos} from '../../../../lib/Collections'
export default function (root, options, context) {
// it wont let anonymous users create a todo
if (!context.userId) {
throw new Error('Unknown User (not logged in)')
}
// pack the todo adding the userId as the owner id
const newTodo = {
text: options.text,