text: import database from '../firebase/firebase';
  
  export const addExpense = expense => ({
    type: 'ADD_EXPENSE',
    expense
  });
  
  export const startAddExpense = (expenseData = {}) => {
    // set default values
    const {
      description = '',
      note = '',
      amount = 0,
      createdAt = 0
    } = expenseData;
  
    return (dispatch, getState) => {
      const userId = getState().auth.uid;
      const expense = {
        description,
        note,
        amount,
        createdAt
      };
  
      // add expense to firebase and then dispatch action
      return database.ref(`users/${userId}/expenses`).push(expense).then((ref) => {
        dispatch(addExpense({
          id: ref.key,
          ...expense
        }));
      });
    };
  };
  
  export const removeExpense = ({ id } = {}) => ({
    type: 'REMOVE_EXPENSE',
    id
  });
  
  export const startRemoveExpense = ({ id } = {}) => {
    return (dispatch, getState) => {
      const userId = getState().auth.uid;
      return database.ref(`users/${userId}/expenses/${id}`).remove().then(() => {
        dispatch(removeExpense({ id }));
      });
    };
  };
  
  export const editExpense = (id, updates) => ({
    type: 'EDIT_EXPENSE',
    id,
    updates
  });
  
  export const startEditExpense = (id, updates) => {
    return (dispatch, getState) => {
      const userId = getState().auth.uid;
      return database.ref(`users/${userId}/expenses/${id}`).update(updates).then(() => {
        dispatch(editExpense(id, updates));
      });
    };
  };
  
  export const setExpenses = expenses => ({
    type: 'SET_EXPENSES',
    expenses
  });
  
  export const startSetExpenses = () => {
    return (dispatch, getState) => {
      const userId = getState().auth.uid;
      return database.ref(`users/${userId}/expenses`).once('value')
        .then((snapshot) => {
          const expenses = [];
          snapshot.forEach((childSnapshot) => {
            expenses.push({
              id: childSnapshot.key,
              ...childSnapshot.val()
            });
          });
          dispatch(setExpenses(expenses));
        });
    };
  };
  