-
-
Save codecademydev/7d09d52914a0df94c953bd69383700c0 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createSlice } from '@reduxjs/toolkit'; | |
export const CATEGORIES = ['housing', 'food', 'transportation', 'utilities', 'clothing', 'healthcare', 'personal', 'education', 'entertainment']; | |
const initialState = CATEGORIES.map(category => ({ category: category, amount: 0 })) | |
const budgetsSlice = createSlice({ | |
name: 'budgets', | |
initialState: initialState, | |
reducers: { | |
editBudget: (state, action) => { | |
const index = state.findIndex(obj => obj.category === action.payload.category); | |
state[index] = action.payload; | |
} | |
}, | |
}); | |
export const selectBudgets = (state) => state.budgets; | |
export const { editBudget } = budgetsSlice.actions; | |
export default budgetsSlice.reducer; | |
// export const editBudget = (budget) => { | |
// return { | |
// type: 'budgets/editBudget', | |
// payload: budget | |
// } | |
// } | |
// const budgetsReducer = (state = initialState, action) => { | |
// switch (action.type) { | |
// case 'budgets/editBudget': | |
// const newBudgets = state.map(budget => { | |
// if (budget.category === action.payload.category) { | |
// return action.payload; | |
// } | |
// return budget; | |
// }) | |
// return newBudgets; | |
// default: | |
// return state; | |
// } | |
// } | |
// export const selectBudgets = (state) => state.budgets; | |
// export default budgetsReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createSlice } from '@reduxjs/toolkit'; | |
export const CATEGORIES = ['housing', 'food', 'transportation', 'utilities', 'clothing', 'healthcare', 'personal', 'education', 'entertainment']; | |
const initialState = Object.fromEntries(CATEGORIES.map(category => [category, []])) | |
export const selectTransactions = (state) => state.transactions; | |
export const selectFlattenedTransactions = (state) => Object.values(state.transactions).reduce((a,b) => [...a, ...b], []); | |
const transactionsSlice = createSlice({ | |
name:'transactions', | |
initialState: initialState, | |
reducers: { | |
addTransaction: (state, action)=>{ | |
state[action.payload.category].push(action.payload); | |
}, | |
deleteTransaction: (state, action)=>{ | |
state[action.payload.category].filter(obj => obj.id !== action.payload.id); | |
}, | |
} | |
}); | |
export const {addTransaction, deleteTransaction } = transactionsSlice.actions; | |
export default transactionsSlice.reducer; | |
// export const addTransaction = (transaction) => { | |
// return { | |
// type: 'transactions/addTransaction', | |
// payload: transaction | |
// } | |
// } | |
// export const deleteTransaction = (transaction) => { | |
// return { | |
// type: 'transactions/deleteTransaction', | |
// payload: transaction | |
// } | |
// } | |
// const transactionsReducer = (state = initialState, action) => { | |
// let newTransactionsForCategory; | |
// switch (action.type) { | |
// case 'transactions/addTransaction': | |
// newTransactionsForCategory = [...state[action.payload.category].slice(), action.payload] | |
// return { ...state, [action.payload.category]: newTransactionsForCategory} | |
// case 'transactions/deleteTransaction': | |
// const deletedIndex = state[action.payload.category].findIndex(transaction => transaction.id === action.payload.id); | |
// newTransactionsForCategory = state[action.payload.category].filter((item, index) => index !== deletedIndex) | |
// return { ...state, [action.payload.category]: newTransactionsForCategory} | |
// default: | |
// return state; | |
// } | |
// } | |
// export default transactionsReducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment