i am new to Reactjs, i'm almost done with my project. i've been doing a hotel booking system. but i have a problem with my checkout page. after i submitted a form from booking page it will redirected to checking out page. now how can i be able to get the reservation data from query? i'm using a param in my other pages but how can i pass an id when it was redirected and no href to pass the id. can anyone help me with this, i'm runnung out of time and i'm still stuck here. thank you in advance.
here is my CheckOutPage:
const CheckoutPage = (props) => {
const [isRedirected, setIsRedirected] = useState(false)
if (isRedirected) {
return <Redirect to='/transaction'/>
}
return (
<ConfirmationMessage setIsRedirected={setIsRedirected}/>
)
}
const ConfirmationMessage = ({setIsRedirected}) => {
const { user } = useContext(UserContext)
const [name, setName] = useState('')
const [roomPrice, setRoomPrice] = useState(0)
let options = {
headers: {
Authorization: 'Bearer ' + user.token
}
}
useEffect(() => {
GQLClient(options).request(Query.getUser).then((data) => {
setName(data.getUser.name)
})
GQLClient({}).request(Query.getRooms).then((data) => {
setRoomPrice(data.getRoom.roomPrice)
})
}, [])
return(
<Container>
<Row> .....
here's in my schema.js
type Query {
getReservationForm: ReservationForm
}
type ReservationForm {
id: ID!
startDate: String!
endDate: String!
name: String!
email: String!
contactDetail: String!
roomNumber: String!
roomPrice: Float!
}
in my resolvers
Query: {
getReservationForm: (parent, args) => {
return ReservationForm.findById(args.id)
}
},
and in my query
getReservationForm: `
query (
$id: String!
) {
getReservationForm (
id: $id
) {
startDate
endDate
name
email
contactDetail
roomNumber
roomPrice
}
}
If you are using Redirect, you can actually pass some data:
<Redirect to={{
pathname: '/checkout',
state: { first: 'James', last: 'Bond', ... }
}}
/>
You can then access this data in your CheckoutPage:
console.log(this.props.location.state);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments