Skip to content

Instantly share code, notes, and snippets.

@jepras
jepras / Arguments optional
Created October 8, 2018 06:57
First creating a function to check if number. Then divide into if arg length is 2 or 1. If 1, return new function that takes in a second argument and then check + add.
function addTogether() {
// Function to check if a number is actually a number
// and return undefined otherwise.
var checkNum = function(num) {
if (typeof num !== 'number') {
return undefined;
} else
return num;
};
@jepras
jepras / Cash Register
Created October 11, 2018 07:00
Work in progress
function checkCashRegister(price, cash, cid) {
var change = 0;
var difference = cash - price;
console.log(difference)
// Create object with status & change key
var cashreg = {
status: "",
change: []
}
@jepras
jepras / Register a Store Listener
Created October 17, 2018 16:32
Used store.subscribe to invoke a callback function that simply adds +1 to count. Store.subscribe fires automatically when an action is dispatched.
const ADD = 'ADD';
const reducer = (state = 0, action) => {
switch(action.type) {
case ADD:
return state + 1;
default:
return state;
}
};
@jepras
jepras / Send Action Data to the Store
Created October 17, 2018 16:33
returning an action object in addNoteText action that feeds into notesReducer and returns associated text to type.
const ADD_NOTE = 'ADD_NOTE';
const notesReducer = (state = 'Initial State', action) => {
switch(action.type) {
// change code below this line
case ADD_NOTE:
return action.text
// change code above this line
default:
return state;
@jepras
jepras / Use Middleware to handle Asynchronous functions
Created October 17, 2018 16:51
Create action dispatchers requestingData & received data. These feed into the handleAsync asynchronous function that first dispatches the first, and after 2,5 dispatches the second. Also added Redux.applyMiddleware(ReduxThunk.default) as second parameter to store variable
const REQUESTING_DATA = 'REQUESTING_DATA'
const RECEIVED_DATA = 'RECEIVED_DATA'
const requestingData = () => { return {type: REQUESTING_DATA} }
const receivedData = (data) => { return {type: RECEIVED_DATA, users: data.users} }
const handleAsync = () => {
return function(dispatch) {
// dispatch request action here
dispatch(requestingData());
@jepras
jepras / Write a counter with Redux
Created October 17, 2018 17:22
First, set action types to string. Then create action creators with type. Type is used in the reducer to decide case & what to return. Lastly add reducer to store.
const INCREMENT = 'INCREMENT'; // define a constant for increment action types
const DECREMENT = 'DECREMENT'; // define a constant for decrement action types
const counterReducer = (state = 0, action) => {
switch(action.type) {
case INCREMENT:
return state + 1
case DECREMENT:
return state - 1
default:
@jepras
jepras / Redux: Remove an Item from an Array
Created October 17, 2018 17:44
Use spread operator. Start to slice from beginning until index, then add a slice starting from index + 1 until end.
const immutableReducer = (state = [0,1,2,3,4,5], action) => {
switch(action.type) {
case 'REMOVE_ITEM':
// don't mutate state here or the tests will fail
return [
...state.slice(0, action.index),
...state.slice(action.index + 1, state.length)
]
default:
return state;
@jepras
jepras / Redux: Copy an Object with Object.assign
Created October 17, 2018 17:50
Use Object.assign to take in an empty object to create shallow copy, then take in state as parameter and update one of the parameters within.
const defaultState = {
user: 'CamperBot',
status: 'offline',
friends: '732,982',
community: 'freeCodeCamp'
};
const immutableReducer = (state = defaultState, action) => {
switch(action.type) {
case 'ONLINE':
@jepras
jepras / React and Redux: Manage State Locally First
Created October 18, 2018 12:15
Basic setup for inputting a value and rendering it as a list.
class DisplayMessages extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
messages: []
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
};
@jepras
jepras / React and Redux: Extract State Logic to Redux
Created October 19, 2018 12:28
Basic Redux setup with store, actionCreator & reducer that adds to immutable array
// define ADD, addMessage(), messageReducer(), and store here:
const ADD = 'ADD';
const messageReducer = (state = [], action) => {
switch(action.type) {
case ADD:
return [
...state,
action.message
]