@@ -3,11 +3,15 @@ import React, { Component } from 'react';
33import Modal from '../components/Modal/Modal' ;
44import Backdrop from '../components/Backdrop/Backdrop' ;
55
6+ import AuthContext from '../context/auth-context' ;
7+
68export default class Events extends Component {
79 state = {
810 creating : false
911 } ;
1012
13+ static contextType = AuthContext ;
14+
1115 constructor ( props ) {
1216 super ( props ) ;
1317 this . titleElRef = React . createRef ( ) ;
@@ -29,15 +33,13 @@ export default class Events extends Component {
2933
3034 if (
3135 title . trim ( ) . length === 0 ||
32- price . trim ( ) . length == = 0 ||
36+ price < = 0 ||
3337 date . trim ( ) . length === 0 ||
3438 description . trim ( ) . length === 0
3539 ) {
3640 return ;
3741 }
3842
39- const event = { title, price, date, description } ;
40-
4143 const requestBody = {
4244 query : `
4345 mutation {
@@ -62,11 +64,14 @@ export default class Events extends Component {
6264 `
6365 } ;
6466
67+ const token = this . context . token ;
68+
6569 fetch ( 'http://localhost:5000/api' , {
6670 method : 'POST' ,
6771 body : JSON . stringify ( requestBody ) ,
6872 headers : {
69- 'Content-Type' : 'application/json'
73+ 'Content-Type' : 'application/json' ,
74+ Authorization : `Bearer ${ token } `
7075 }
7176 } )
7277 . then ( response => {
@@ -77,13 +82,6 @@ export default class Events extends Component {
7782 } )
7883 . then ( responseData => {
7984 console . log ( responseData ) ;
80- if ( responseData . data . login . token ) {
81- this . context . login (
82- responseData . data . login . token ,
83- responseData . data . login . userId ,
84- responseData . data . login . tokenExpiration
85- ) ;
86- }
8785 } )
8886 . catch ( err => {
8987 console . log ( err ) ;
0 commit comments