Skip to content

Instantly share code, notes, and snippets.

@lrpinto
Last active June 1, 2021 22:09
Show Gist options
  • Save lrpinto/3d52bbd08bf204dc1b6c5083ca0d290a to your computer and use it in GitHub Desktop.
Save lrpinto/3d52bbd08bf204dc1b6c5083ca0d290a to your computer and use it in GitHub Desktop.
Create store
//--------- App code ---------//
const ADD_TODO = 'ADD_TODO'
const REMOVE_TODO = 'REMOVE_TODO'
const TOGGLE_TODO = 'TOGGLE_TODO'
const ADD_GOAL = 'ADD_GOAL'
const REMOVE_GOAL = 'REMOVE_GOAL'
function addTodoAction (todo) {
return {
type: ADD_TODO,
todo,
}
}
function removeTodoAction (id) {
return {
type: REMOVE_TODO,
id,
}
}
function toggleTodoAction (id) {
return {
type: TOGGLE_TODO,
id,
}
}
function addGoalAction (goal) {
return {
type: ADD_GOAL,
goal,
}
}
function removeGoalAction (id) {
return {
type: REMOVE_GOAL,
id,
}
}
/**
* Reducer
*/
function todos (state = [], action) {
switch(action.type) {
case ADD_TODO :
return state.concat([action.todo])
case REMOVE_TODO :
return state.filter((todo) => todo.id !== action.id)
case TOGGLE_TODO :
return state.map((todo) => todo.id !== action.id ? todo :
Object.assign({}, todo, { complete: !todo.complete }))
default :
return state
}
}
function goals (state = [], action) {
switch(action.type) {
case ADD_GOAL :
return state.concat([action.goal])
case REMOVE_GOAL:
return state.filter((goal) => goal.id !== action.id)
default :
return state
}
}
function app (state = {}, action) {
return {
todos: todos(state.todos, action),
goals: goals(state.goals, action),
}
}
const store = createStore(app)
store.subscribe(()=>{
console.log('The new state is: ', store.getState())
})
store.dispatch({
type: ADD_TODO,
todo: {
id: 0,
name: 'Learn Redux',
complete: false
}
})
store.dispatch({
type: ADD_TODO,
todo: {
id: 1,
name: 'Read Book',
complete: true
}
})
const unsubscribe = store.subscribe(()=>{
console.log('The store was updated')
})
unsubscribe()
//--------- Library code ---------//
/**
* Factory function that creates store objects
*/
function createStore (reducer) {
// The store should have four parts
// 1. The state
// 2. Get the state.
// 3. Listen to changes on the state.
// 4. Update the state
let listeners = []
let state
// returns the state
const getState = () => state
// listens to changes
const subscribe = (listener) => {
listeners.push(listener)
return () => {
listeners = listeners.filter((l) => (l !== listener))
}
}
// update state
const dispatch = (action) => {
state = reducer(state, action)
listeners.forEach((listener) => listener())
}
return {
getState,
subscribe,
dispatch
}
}
store.dispatch(addTodoAction({
id: 0,
name: 'Walk the dog',
complete: false,
}))
store.dispatch(addTodoAction({
id: 1,
name: 'Wash the car',
complete: false,
}))
store.dispatch(addTodoAction({
id: 2,
name: 'Go to the gym',
complete: true,
}))
store.dispatch(removeTodoAction(1))
store.dispatch(toggleTodoAction(0))
store.dispatch(addGoalAction({
id: 0,
name: 'Learn Redux'
}))
store.dispatch(addGoalAction({
id: 1,
name: 'Lose 20 pounds'
}))
store.dispatch(removeGoalAction(0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment