diff --git a/src/actions/statistics.js b/src/actions/statistics.js index 1dbf4125ed7c738aafdb64c2c297f153de749c0c..d51de7d259f6f63dcc8668734281eb8778c2819f 100644 --- a/src/actions/statistics.js +++ b/src/actions/statistics.js @@ -5,6 +5,7 @@ import { GET_TRAINEES, VISITOR_CHANGE, GET_NOTES_BY_EVENT, WRITE_EVENT, + ADD_EVENT, } from './types'; export const getEvents = () => ( @@ -79,9 +80,9 @@ export const submitVisitors = ({ id, visitors }) => ( } ); -export const writeEvent = event => ( +export const writeEvent = ({ target: { name, value } }) => ( (dispatch) => { - dispatch({ type: WRITE_EVENT, payload: event.target.value, target: event.name }); + dispatch({ type: WRITE_EVENT, payload: value, target: name }); } ); @@ -91,3 +92,25 @@ export const eventDate = (name, value) => ( dispatch({ type: WRITE_EVENT, payload: value, target: name }); } ); + +export const addEvent = ({ name, date }) => ( + async (dispatch) => { + try { + const response = await axios.post('/api/v1/events/', { + name, + date, + }); + if (response.data.id) { + alert('Sikeres mentĂŠs!'); + dispatch({ + type: ADD_EVENT, + payload: response.data, + }); + } else { + alert('MentĂŠs nem sikerĂźlt!'); + } + } catch (e) { + console.log(e); + } + } +); diff --git a/src/actions/types.js b/src/actions/types.js index ef3787c7b7a931c68747f2d73e86b5193b825d00..23fc2255af99c8deb37f4bf8e711d872ab9f0ea6 100644 --- a/src/actions/types.js +++ b/src/actions/types.js @@ -20,3 +20,4 @@ export const VISITOR_CHANGE = 'visitor_change'; export const GET_NOTES_BY_EVENT = 'get_notes_by_event'; export const WRITE_EVENT = 'write_event'; +export const ADD_EVENT = 'add_event'; diff --git a/src/components/forms/AddEventForm.js b/src/components/forms/AddEventForm.js index dec4ec1901cfc213d92a47ea6515cdfe54388901..1df5d902c88eb1c3dae0fe7c05d4dfb1e7782dd3 100644 --- a/src/components/forms/AddEventForm.js +++ b/src/components/forms/AddEventForm.js @@ -3,8 +3,8 @@ import { Modal, Button, Form, Input, TextArea, Icon } from 'semantic-ui-react'; import { connect } from 'react-redux'; import { DateTimeInput } from 'semantic-ui-calendar-react'; -import { clearWrite } from '../../actions/news'; -import { writeEvent, eventDate } from '../../actions/statistics' +// import { clearWrite } from '../../actions/news'; +import { writeEvent, eventDate, addEvent } from '../../actions/statistics' class AddEventForm extends Component { constructor(props) { @@ -59,6 +59,7 @@ class AddEventForm extends Component { <DateTimeInput name="date" label="DĂĄtum:" + dateFormat='YYYY-MM-DD' placeholder="Date" value={this.state.date} iconPosition="left" @@ -79,8 +80,8 @@ class AddEventForm extends Component { inverted color='green' onClick={() => { - this.props.addEvent(); - this.setState({ showModal: false }); + this.props.addEvent(this.props.newEvent); + this.setState({ showModal: false, date: '' }); }} > <Icon name='checkmark' /> Add @@ -93,4 +94,4 @@ class AddEventForm extends Component { const mapStateToProps = ({ events: { newEvent } }) => ({ newEvent }); -export default connect(mapStateToProps, { writeEvent, clearWrite, eventDate })(AddEventForm); +export default connect(mapStateToProps, { writeEvent, addEvent, eventDate })(AddEventForm); diff --git a/src/reducers/EventReducer.js b/src/reducers/EventReducer.js index 53371208abf3c4f7026677839d400980d573d09c..f6f079d6073cb07eb5a59387aec4f35c765b91b8 100644 --- a/src/reducers/EventReducer.js +++ b/src/reducers/EventReducer.js @@ -1,4 +1,4 @@ -import { GET_EVENTS, GET_EVENT_BY_ID, VISITOR_CHANGE, WRITE_EVENT } from '../actions/types'; +import { GET_EVENTS, GET_EVENT_BY_ID, VISITOR_CHANGE, WRITE_EVENT, ADD_EVENT } from '../actions/types'; const INITIAL_STATE = { newEvent: {} }; @@ -11,7 +11,9 @@ export default (state = INITIAL_STATE, action) => { case VISITOR_CHANGE: return { ...state, selectedEvent: { ...state.selectedEvent, visitors: action.payload } }; case WRITE_EVENT: - return { ...state, newEvent: { ...state.selectedEvent, [action.target]: action.payload } }; + return { ...state, newEvent: { ...state.newEvent, [action.target]: action.payload } }; + case ADD_EVENT: + return { ...state, events: [...state.events, action.payload] }; default: return state; }