export const ACTIONS = { ADD_DIGIT: 'add-digit', CHOOSE_OPERATION: 'choose-operation', CLEAR: 'clear', EVALUATE: 'evaluate', }; function reducer(state, { type, payload }) { switch (type) { case ACTIONS.ADD_DIGIT: if (state.overwrite) { return { ...state, currentOperand: payload.digit, overwrite: false, }; } if (payload.digit === '0' && state.currentOperand === '0') { return state; } if (payload.digit === '.' && state.currentOperand.includes('.')) { return state; } return { ...state, currentOperand: `${state.currentOperand || ''}${payload.digit}` }; case ACTIONS.CHOOSE_OPERATION: if (state.currentOperand == null && state.previousOperand == null) { return state; } if (state.currentOperand == null) { return { ...state, operation: payload.operation, }; } if (state.previousOperand == null) { return { ...state, operation: payload.operation, previousOperand: state.currentOperand, currentOperand: null, }; } return { ...state, previousOperand: evaluate(state), operation: payload.operation, currentOperand: null, }; case ACTIONS.CLEAR: return {}; case ACTIONS.EVALUATE: if (state.operation == null || state.currentOperand == null || state.previousOperand == null) { return state; } return { ...state, overwrite: true, previousOperand: null, operation: null, currentOperand: evaluate(state), }; default: return state; } }