Skip to content

Instantly share code, notes, and snippets.

View jasonleehodges's full-sized avatar

jasonleehodges

View GitHub Profile
@jasonleehodges
jasonleehodges / downloadCSV.cs
Created December 27, 2016 18:58 — forked from dhlavaty/downloadCSV.cs
Download CSV file from Google Spreadsheet (Google Drive) using minimum c# code
using System;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
public class WebClientEx : WebClient
{
public WebClientEx(CookieContainer container)
@jasonleehodges
jasonleehodges / datagen.scala
Last active January 26, 2018 17:42
Datagen in Scala - a rewrite of the python datagen that I did using faker to generate large amounts of faker data.
package datagen
import com.github.javafaker._
import java.io.File
import java.io.PrintWriter
import java.time.temporal.ChronoUnit.DAYS
import java.time._
import scala.util.Random
object DataGen {
@jasonleehodges
jasonleehodges / pageable.scala
Created November 10, 2018 15:13
Simulate pulling data down from a pageable api where you don't know how many pages there are in your request without using any mutable variables to keep track of what page you are on.
import scala.collection.mutable.Queue
object Page extends App {
val apiData = Queue(1000,1000,1000,1000,1000,10)
Stream.from(1).takeWhile(getPage(_).nonEmpty).foreach(_ => processData())
def getPage(x: Int): Queue[Int] = { return apiData }
def processData(){ println(apiData.dequeue()) }
@jasonleehodges
jasonleehodges / navigation.ts
Last active October 19, 2019 21:29
Connected Component with Higher-Ordered Component Wrapper
interface StateProps {
/* define props interface here */
}
interface DispatchProps {
/* define props interface here */
}
export type Props = StateProps & DispatchProps;
@jasonleehodges
jasonleehodges / navigation.ts
Created October 19, 2019 21:35
Simplified component with react-redux hooks
import React, { FunctionComponent } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { RootState } from '~/reducers';
import { selectUser, setUserAction } from 'actions/app';
export const Navigation: FunctionComponent = () => {
const user = useSelector((state: RootState) => selectUser(state));
const dispatch = useDispatch();
const setUser = (user: string) => () => dispatch(setUserAction(user));
@jasonleehodges
jasonleehodges / users.ts
Created October 19, 2019 21:37
Custom react-redux hooks
import { useSelector, useDispatch } from 'react-redux';
import { RootState } from '~/reducers';
import { selectUser, selectAllUsers } from 'actions/app';
import { setUserAction, setAllUsersAction } from '../actions/app';
export const useUserState = () => {
return {
user: useSelector((state: RootState) => selectUser(state)),
allUsers: useSelector((state: RootState) => selectAllUsers(state)),
}
@jasonleehodges
jasonleehodges / navigation.ts
Created October 19, 2019 21:38
Connected component simplified with custom react-redux hooks
import React, { FunctionComponent } from 'react';
import { useUserState, useUserDispatch } from '../../hooks/user';
export const Navigation: FunctionComponent = () => {
const { user } = useUserState();
const { setUser } = useUserDispatch();
return (
<div className={styles.Navigation} onClick={ setUser('me') }>
User name: { user }
@jasonleehodges
jasonleehodges / configure-store.ts
Created March 21, 2020 22:21
Configure Redux Store
import { AnyAction, applyMiddleware, compose, createStore } from 'redux';
import combinedReducers, { RootState } from '../reducers';
import thunk, { ThunkMiddleware } from 'redux-thunk';
const devToolsCompose = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
const composeEnhancers = (devToolsCompose !== null && devToolsCompose !== undefined)
? devToolsCompose
: compose;
export type ThunkWithRootState = ThunkMiddleware<RootState, AnyAction>;
@jasonleehodges
jasonleehodges / get-data-with-thunk.ts
Last active March 21, 2020 22:40
Example async action w/ Thunk
import { setData } from '../actions';
export const getDataWithThunk = () => {
return (dispatch) => {
fetch(url)
.then(resp => resp.json())
.then(data => dispatch(setData(data));
}
}
@jasonleehodges
jasonleehodges / get-data-with-singleton-store.ts
Last active March 21, 2020 22:39
Example of dispatching data with a singleton store (anti-pattern)
// typically this lives where the provider wraps the root component
import { store } from './location-of-instantiated-store';
import { setData } from '../actions';
export const getDataWithSingletonStore = () => {
fetch(url)
.then(resp => resp.json())
.then(data => store.dispatch(setData(data)));
};