Open DhruviSherathiya opened 2 years ago
Event.js
import React, { Component } from 'react'; import Modal from '../components/Modal/Modal'; import Backdrop from '../components/Backdrop/Backdrop'; import EventList from '../components/Events/EventList/EventList'; import Spinner from '../components/Spinner/Spinner'; import AuthContext from '../context/auth-context'; import './Events.css'; class EventsPage extends Component { state = { creating: false, events: [], isLoading: false, selectedEvent: null }; static contextType = AuthContext; constructor(props) { super(props); this.titleElRef = React.createRef(); this.priceElRef = React.createRef(); this.dateElRef = React.createRef(); this.descriptionElRef = React.createRef(); } componentDidMount() { this.fetchEvents(); } startCreateEventHandler = () => { this.setState({ creating: true }); }; modalConfirmHandler = () => { this.setState({ creating: false }); const title = this.titleElRef.current.value; const price = +this.priceElRef.current.value; const date = this.dateElRef.current.value; const description = this.descriptionElRef.current.value; if ( title.trim().length === 0 || price < 0 || date.trim().length === 0 || description.trim().length === 0 ) { return; } const event = { title, price, date, description }; console.log(event); const requestBody = { query: ` mutation { createEvent(eventInput: {title: "${title}", description: "${description}", price: ${price}, date: "${date}"}) { _id title description date price } } ` }; const token = this.context.token; fetch('http://localhost:8000/graphql', { method: 'POST', body: JSON.stringify(requestBody), headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + token } }) .then(res => { if (res.status !== 200 && res.status !== 201) { throw new Error('Failed!'); } return res.json(); }) .then(resData => { this.setState(prevState => { const updatedEvents = [...prevState.events]; updatedEvents.push({ _id: resData.data.createEvent._id, title: resData.data.createEvent.title, description: resData.data.createEvent.description, date: resData.data.createEvent.date, price: resData.data.createEvent.price, creator: { _id: this.context.userId } }); return { events: updatedEvents }; }); }) .catch(err => { console.log(err); }); }; modalCancelHandler = () => { this.setState({ creating: false, selectedEvent: null }); }; fetchEvents() { this.setState({ isLoading: true }); const requestBody = { query: ` query { events { _id title description date price creator { _id email } } } ` }; fetch('http://localhost:8000/graphql', { method: 'POST', body: JSON.stringify(requestBody), headers: { 'Content-Type': 'application/json' } }) .then(res => { if (res.status !== 200 && res.status !== 201) { throw new Error('Failed!'); } return res.json(); }) .then(resData => { const events = resData.data.events; this.setState({ events: events, isLoading: false }); }) .catch(err => { console.log(err); this.setState({ isLoading: false }); }); } // to display details of the events showDetailHandler = eventId => { this.setState(prevState => { const selectedEvent = prevState.events.find(e => e._id === eventId); return { selectedEvent: selectedEvent }; }); }; bookEventHandler = () => {}; render() { return ({(this.state.creating || this.state.selectedEvent) && ); } } export default EventsPage;} {this.state.creating && ( )} {this.state.selectedEvent && ( )} {this.context.token && ( {this.state.selectedEvent.title}
${this.state.selectedEvent.price} -{' '} {new Date(this.state.selectedEvent.date).toLocaleDateString()}
{this.state.selectedEvent.description}
)} {this.state.isLoading ? (Share your own Events!
) : ( )}
I am getting below error while adding new event. Uncaught TypeError: Cannot read properties of null (reading '_id')