Skip to content

Commit

Permalink
connected submit to backend; added reusable modal for messages
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
lrgongora committed May 2, 2020
1 parent 027d92c commit d1f1785
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 53 deletions.
46 changes: 0 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/actions/modalPopup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { OPEN_MODAL, CLOSE_MODAL } from '../constants/actionTypes';

export const openModal = (message) => {
return {
type: OPEN_MODAL,
isOpen: true,
message: message
};
};

export const closeModal = () => {
return {
type: CLOSE_MODAL,
isOpen: false,
message: ''
};
};
52 changes: 52 additions & 0 deletions src/components/ModalPopup/ModalPopup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react'
import Popup from 'reactjs-popup'
import { connect } from 'react-redux'
import { closeModal } from '../../actions/modalPopup';

const mapStateToProps = state => {
return {
modal: state.modal
}}

function ModalPopup({modal, dispatch}) {

const popupStyle = {
content: {
position: "relative",
background: "rgb(255, 255, 255)",
width: "400px",
margin: "auto",
border: "10px solid #25646a",
fontSize: "1.5rem",
fontWeight: "1.5rem",
padding: "5px",
borderRadius: "4px",
height: "200px",
textAlign: "center",
display: "flex",
alignItems: "center",
justifyContent: "center"
}
}
const close = () => {
return dispatch(closeModal());
}


return (
<Popup
contentStyle={popupStyle.content}
open={modal.isOpen}
onClose={() => {close()}}>
<div className='popup'>
<p className='close-button' onClick={() => close()}>
&times;
</p>
<p className="popup-message">{modal.message}</p>
</div>
</Popup>
)
}

export default connect(mapStateToProps)(ModalPopup);

Empty file.
2 changes: 2 additions & 0 deletions src/constants/actionTypes.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const GET_VICTIMS_REQUEST = 'GET_VICTIMS_REQUEST';
export const OPEN_MODAL = 'OPEN_MODAL';
export const CLOSE_MODAL = 'CLOSE_MODAL';
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import './styles/normalize.css';
import './styles/global.scss';
import rootReducer from './reducers';
import * as serviceWorker from './serviceWorker';

import ModalPopup from './components/ModalPopup/ModalPopup'
import Home from './pages/Home';
import NotFound from './pages/NotFound';
import Victims from './pages/Victims';
Expand Down Expand Up @@ -42,6 +42,7 @@ const Routes = () => (
ReactDOM.render(
<Provider store={store}>
<Routes />
<ModalPopup />
</Provider>,
document.getElementById('root')
);
Expand All @@ -50,3 +51,4 @@ ReactDOM.render(
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

37 changes: 31 additions & 6 deletions src/pages/Submit.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import React, { useRef, useEffect } from 'react';
import { useForm } from 'react-hook-form';
import { connect } from 'react-redux';
import { openModal } from '../actions/modalPopup'
import MainLayout from '../components/MainLayout';
import './Submit.scss';
import langs from '../data/languages.js';

import data from '../data/countries.json';

const Submit = () => {
const Submit = ({ dispatch }) => {
const nameRef = useRef();
const { register, handleSubmit, errors } = useForm()

const handleFormSubmit = (data) => {
console.log(data);
const handleFormSubmit = (data, e) => {
fetch(`${process.env.REACT_APP_API_BASE}/report`, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(response => {
switch(response.status) {
case 200:
e.target.reset();
dispatch(openModal("The report was submitted successfully!"))
break;
case 400:
dispatch(openModal(`Something went wrong! Error: ${response.message}`))
break;
default:
dispatch(openModal("Something went wrong!"))
}})
.catch(err => {
dispatch(openModal("Something went wrong!"))
})

};

useEffect(() => {
Expand Down Expand Up @@ -45,7 +69,7 @@ const Submit = () => {
<input
id="email"
name="email"
type="text"
type="email"
ref={register({ required: true })}
/>
{errors.email &&
Expand Down Expand Up @@ -245,4 +269,5 @@ const Submit = () => {
);
};

export default Submit;
export default connect()(Submit);

2 changes: 2 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { combineReducers } from 'redux';
import victims from './victims';
import modal from './modalPopup';

export default combineReducers({
victims,
modal
});
18 changes: 18 additions & 0 deletions src/reducers/modalPopup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const initialState = {
isOpen: false,
message: ''
}

const modal = (state = initialState, action) => {
switch(action.type) {
case 'OPEN_MODAL':
return {isOpen: true, message: action.message}
case 'CLOSE_MODAL':
return {isOpen: false, message: action.message}
default:
return {isOpen: false, message: ''}
}

}

export default modal;

0 comments on commit d1f1785

Please sign in to comment.