diff --git a/src/components/createForm/actions.ts b/src/components/createForm/actions.ts index 773a7ca..5b98d9c 100644 --- a/src/components/createForm/actions.ts +++ b/src/components/createForm/actions.ts @@ -40,8 +40,8 @@ export const createHalfsie = (formData) => ( throw new Error('There was an issue saving your Halfise.'); }) - .then((response) => { - const { balance } = JSON.parse(response.body); + .then((responseBody) => { + const { balance } = responseBody; const { userName: user } = authStore; dispatch(addLog({ user, ...log })); diff --git a/src/components/createForm/createForm.spec.js b/src/components/createForm/createForm.spec.js index 7d29eb9..e010585 100644 --- a/src/components/createForm/createForm.spec.js +++ b/src/components/createForm/createForm.spec.js @@ -1,4 +1,3 @@ - import React from 'react'; import { assert } from 'chai'; import { shallow } from 'enzyme'; @@ -10,18 +9,25 @@ import CreateForm from './createForm'; describe('Create Form', () => { const chance = new Chance(); + const logStore = { log: [] }; it('should render', () => { - const wrapper = shallow(); + const wrapper = shallow(); assert.equal(wrapper.find('h1').text(), 'Create a new Halfsie'); }); + it('should not render without a log', () => { + const wrapper = shallow(); + + assert.equal(wrapper.html(), null); + }); + it('should be able to sign in a user', () => { const createStore = { pending: false }; const dispatch = sinon.spy(); const preventDefault = sinon.spy(); - const wrapper = shallow(); + const wrapper = shallow(); const form = wrapper.find('form'); form.simulate('submit', { preventDefault }); @@ -34,7 +40,7 @@ describe('Create Form', () => { const createStore = { pending: true }; const dispatch = sinon.spy(); const preventDefault = sinon.spy(); - const wrapper = shallow(); + const wrapper = shallow(); const form = wrapper.find('form'); form.simulate('submit', { preventDefault }); @@ -46,7 +52,7 @@ describe('Create Form', () => { it('should show an error message if the create form does not work', () => { const errorMessage = chance.word(); const createStore = { pending: true, errorMessage }; - const wrapper = shallow(); + const wrapper = shallow(); assert.equal(wrapper.find('.alert__error strong').text(), errorMessage); }); @@ -54,8 +60,8 @@ describe('Create Form', () => { it('should keep amount in state', () => { const value = chance.natural(); const dispatch = sinon.spy(); - const wrapper = shallow(); - const { getByLabelText } = render(); + const wrapper = shallow(); + const { getByLabelText } = render(); const amountInputDummy = wrapper.find('#amount'); const amountInput = getByLabelText('Amount'); @@ -69,8 +75,8 @@ describe('Create Form', () => { it('should keep description in state', () => { const value = chance.word(); const dispatch = sinon.spy(); - const wrapper = shallow(); - const { getByLabelText } = render(); + const wrapper = shallow(); + const { getByLabelText } = render(); const descriptionTextAreaDummy = wrapper.find('#description'); const descriptionTextArea = getByLabelText('Description'); @@ -88,7 +94,7 @@ describe('Create Form', () => { const mockRouter = { push }; render( - + ); @@ -96,4 +102,11 @@ describe('Create Form', () => { assert.isTrue(push.calledWith('/')); assert.isTrue(dispatch.calledOnce); }); + + it('should fech log if there is no log to be found', () => { + const dispatch = sinon.spy(); + render(); + + assert.isTrue(dispatch.calledTwice); + }); }); diff --git a/src/components/createForm/createForm.tsx b/src/components/createForm/createForm.tsx index 11070b1..510a056 100644 --- a/src/components/createForm/createForm.tsx +++ b/src/components/createForm/createForm.tsx @@ -3,14 +3,20 @@ import { useRouter } from 'next/router'; import Link from 'next/link'; import classnames from 'classnames'; import { createHalfsie, resetCreateForm } from './actions'; +import { getLog } from '../log/actions'; -export default function CreateFormComponent({ createStore, dispatch }) { +export default function CreateFormComponent({ createStore, logStore, dispatch }) { const { pending, errorMessage } = createStore; + const { log } = logStore; const router = useRouter(); const [amount, setAmount] = useState< string >(''); const [description, setDescription] = useState< string >(''); + useEffect(() => { + if (!log) dispatch(getLog()); + }, []); + useEffect(() => { if (createStore.needsRedirect) router.push('/'); @@ -23,6 +29,8 @@ export default function CreateFormComponent({ createStore, dispatch }) { e.preventDefault(); }; + if (!log) return null; + return (

Create a new Halfsie

diff --git a/src/lambdas/halfsie_createHalfsie/index.js b/src/lambdas/halfsie_createHalfsie/index.js index 347ffb9..b666f33 100644 --- a/src/lambdas/halfsie_createHalfsie/index.js +++ b/src/lambdas/halfsie_createHalfsie/index.js @@ -10,7 +10,7 @@ exports.handler = async (newHalfsieData) => { let newBalance; let statusCode = 200; let errorMessage; - const { accessToken, log } = newHalfsieData; + const { accessToken, log } = JSON.parse(newHalfsieData.body); const { user, errorMessage: userErrorMessage } = await getUser(accessToken); if (user) { @@ -35,5 +35,14 @@ exports.handler = async (newHalfsieData) => { } const body = JSON.stringify({ newBalance, errorMessage }); - return { statusCode, body }; + const response = { + statusCode, + headers: { + 'Access-Control-Allow-Origin': 'https://halfsies.troyblank.com', + 'Content-Type': 'application/json' + }, + body + }; + + return response; };