Skip to content

Instantly share code, notes, and snippets.

@salmanx
Created January 14, 2019 15:45
Show Gist options
  • Save salmanx/e99eda330967d158d3ef5461f0278808 to your computer and use it in GitHub Desktop.
Save salmanx/e99eda330967d158d3ef5461f0278808 to your computer and use it in GitHub Desktop.
import { createStore, combineReducers} from "redux";
import uuid from 'uuid/v1';
// NOTE:
// to create store we need to pass reducer in createStore() method
// reducer is a function with state and action params - testReducer(state = demostate, action)
// reducer will return result based on action.type
// we need to pass demoState in params
// state of the application would look like this demoState
const demoState = {
expenses: [
{
id: 'abc',
description: 'This is description',
note: 'This is note',
amount: 87666,
createdAt: 0
}
],
filters: {
text: 'rent',
sortBy: 'amount', // or date
startDate: undefined,
endDate: undefined
}
}
// Create Actions
// - ADD_EXPENSE
const addExpense = ({
description = '',
note = '',
amount = 0,
createdAt = 0
} = {}) =>
({
type: 'ADD_EXPENSE',
expense: {
id: uuid(),
description,
note,
amount,
createdAt
}
});
// - REMOVE_EXPENSE
const removeExpense = ({ id } = {}) => ({
type: 'REMOVE_EXPENSE',
id
});
// - EDIT_EXPENSE
const editExpense = (id, updates) => ({
type: 'EDIT_EXPENSE',
id,
updates
});
// - SET_TEXT_FILTER
const setTextFilter = (text = '') => ({
type: 'SET_TEXT_FILTER',
text
});
// - SORT_BY_DATE
const sortByDate = () => ({
type: 'SORT_BY_DATE'
});
// - SORT_BY_AMOUNT
const sortByAmount = () => ({
type: 'SORT_BY_AMOUNT'
});
// - SET_START_DATE
const setStartDate = (startDate) => ({
type: 'SET_START_DATE',
startDate
});
// - SET_END_DATE
const setEndDate = (endDate) => ({
type: 'SET_START_DATE',
endDate
});
// EXPENSE REDUCERS
const expensesReducerDefaultState = [];
const expensesReducer = (state = expensesReducerDefaultState, action) => {
switch (action.type) {
case 'ADD_EXPENSE':
// return state.concat(action.expense);
return [
...state,
action.expense
];
case 'REMOVE_EXPENSE':
return state.filter(({id}) => id !== action.id)
case 'EDIT_EXPENSE':
return state.map((expense) => {
if (expense.id === action.id) {
return {
...expense,
...action.updates
}
} else {
return expense;
}
})
default:
return state;
}
}
// FILTER REDUCERS
const filtersReducerDefaultState = {
text: '',
sortBy: 'date', // or date
startDate: undefined,
endDate: undefined
}
const filtersReducer = (state = filtersReducerDefaultState, action) => {
switch (action.type) {
case 'SET_TEXT_FILTER':
return {
...state,
text: action.text
}
case 'SORT_BY_DATE':
return {
...state,
sortBy: 'date'
}
case 'SORT_BY_AMOUNT':
return {
...state,
sortBy: 'amount'
};
case 'SET_START_DATE':
return {
...state,
startDate: action.startDate
};
case 'SET_END_DATE':
return {
...state,
endDate: action.endDate
};
default:
return state;
}
}
// Get visible expenses
const getVisibleExpenses = (expenses, { text, sortBy, startDate, endDate }) => {
return expenses.filter((expense) => {
const startDateMatch = typeof startDate !== 'number' || expense.createdAt >= startDate;
const endDateMatch = typeof endDate !== 'number' || expense.createdAt <= endDate;
const textMatch = expense.description.toLowerCase().includes(text.toLowerCase());
return startDateMatch && endDateMatch && textMatch;
}).sort((a, b) => {
if (sortBy === 'date') {
return a.createdAt < b.createdAt ? 1 : -1;
} else if(sortBy === 'amount'){
return a.amount < b.amount ? 1 : 1;
}
})
}
// Create Store
const store = createStore(
combineReducers({
expenses: expensesReducer,
filters: filtersReducer
})
);
store.subscribe(() => {
const state = store.getState();
const visibleExpenses = getVisibleExpenses(state.expenses, state.filters);
console.log(visibleExpenses);
});
const expenseOne = store.dispatch(addExpense({ description: 'rent', amount: 500, createdAt: -21000}));
const expenseTwo = store.dispatch(addExpense({ description: 'cofee', amount: 100, createdAt: -1000}));
// store.dispatch(removeExpense({id: expenseOne.expense.id}));
// store.dispatch(editExpense(expenseTwo.expense.id, {amount: 700}));
store.dispatch(setTextFilter('Rent'));
// store.dispatch(setTextFilter());
//
// store.dispatch(sortByAmount());
// store.dispatch(sortByDate());
// store.dispatch(setStartDate(0));
// store.dispatch(setStartDate());
// store.dispatch(setEndDate(1250));
// Test spred operator for object using babel-plugin-transform-object-rest-spread
// const user = {
// name: "salman",
// age: "27"
// }
//
// console.log({
// ...user,
// location: "Dhaka"
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment