Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is index.js from branch 9-action-creators at its desired state? #3

Open
guilsa opened this issue Feb 23, 2021 · 1 comment
Open

is index.js from branch 9-action-creators at its desired state? #3

guilsa opened this issue Feb 23, 2021 · 1 comment

Comments

@guilsa
Copy link

guilsa commented Feb 23, 2021

Starting on line 26 in 9-action-creators, I'm seeing a number of repetitive code all jumbled together which I don't suspect is correct. Thanks!

@a89529294
Copy link

Below is a version without the repetitive code:

// Library code
function createStore(reducer) {
// The store should have four parts
// 1. The state
// 2. Get the state. (getState)
// 3. Listen to changes on the state. (subscribe)
// 4. Update the state (dispatch)

let state;
let listeners = [];

const getState = () => state;

const subscribe = (listener) => {
listeners.push(listener);
return () => {
listeners = listeners.filter((l) => l !== listener);
};
};

const dispatch = (action) => {
state = reducer(state, action);
listeners.forEach((listener) => listener());
};

return {
getState,
subscribe,
dispatch,
};
}

// 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
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(
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));

goldfadenitay pushed a commit to goldfadenitay/redux-course-1 that referenced this issue Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants