With our new content type by the name of order created we can just narrow act to our content and setup
our submit order function and we're going to start by in fact running the function just so you can see
what kind of values we're gonna be passing in.
So again we are submit order and since we already set up a few post request using the ax heroes and
all that then of course I would want first to start well what kind of hours we're gonna be passing because
the actual functionality is gonna be almost exactly the same like we had before.
The difference is of course we're gonna pass on the token on I would want you to be aware of where we
are grabbing all the tokens whether that's user token or whether that is a strike token.
So first of all we need to decide Well when are we gonna call this function.
Well I think it would make the most sense if we would call the function or submit or when we have our
token correct.
Because there's no point to do that if we have some kind of error.
So instead of console logging the response I am going to remove it and I'm going to say first of all
that I would want to set error to an empty string because that way I know that everything one successful
someone would set error and I'm gonna send it back to an empty string.
Then we're going to the structure the I.D. from the token because we have the token.
And then what we're looking for is this I.D. That's the property we're looking for.
So right away we're going to the structure not seven seconds and we're looking for the I.D. It is coming
from the token and now we're going to have our submit order it is going to be a synchronous function.
So again we're gonna set up some kind of variable left order is gonna be equal to a wait and then we're
gonna have our submit order so submit order and then within the submit order function we're going to
pass in the object with these particular values.
So first of all going to have the name and again name is just name input that we have within our form.
Next we're gonna have a total.
And in this case you know what I'm just gonna type in property with a value because I think is just
gonna make a bit more sense.
So I'm gonna have name is equal to name even though we can use a shorthand.
In this case I think it is a little bit easier if we write it out everything the long way because that
way you for sure know which one is the actual property that we're gonna have in the function more essentially
a parameter and which one is the value that we're passing in.
So we're going to have a parameter by the name of total and that is going to be equal to our card total.
So in that case of course it is exactly the same.
So we can just pass in the total that I would want to set up.
Of course my items remember within the orders we also have the items where we dumped our Jason there.
So I'm going to call this as a parameter of items.
And then the value is gonna be my card.
So remember we do structured out the card so whatever the card actual array was not the total but actual
items.
And then we're gonna set it equal to an items and then also we're gonna have a stripe token I.D. parameter
and user token parameter.
So we're gonna have a stripe token token I.D. and that is gonna be equal to I.D. that we're getting
back.
So we have our token and then I would want to get of course my stripe token I.D. so I'm gonna passing
the entry.
And lastly we have the user token because of course when we're communicating with our backend we need
to let strappy know of which is the user that is submitting the order.
So there's gonna be a parameter by the name of user token.
And we're gonna have here the value of user token where we're getting the user.
We do structured it from the user context.
So we have our user user has the token property and we're just getting that token.
Now once we have set up everything we're gonna have two options either are or is going to return an
error or it's going to be a success.
So again we're going to do one more if statement and I'm going to say if we order in that case of course
it was a success because they're fun checking if it is true.
And then of course we're also going to have else.
So that way we're just going to display that there's some kind of error.
Now we're gonna start I guess with a else because that way we're just going to show a different alert
so we can go to show alert then we're gonna pass in again the message.
So we're going to go with the message and we're going to say there was an error with your order with
your order and then we're gonna have a dot.
So please try again.
Online exclamation mark.
And for the type we need to have it as a danger because of course we would want it in the red color.
So we're gonna go with danger.
And if you were a success if everything went well then we're gonna show alert that your order is complete.
So let's go show alert.
Then we're passing in the message again.
So we're going to go with the message and your order.
And of course I need to have quotation marks your order is complete and we would want to clear the cart.
That is something that we do structured from our court context so we can just have clear cart.
That is the method.
And then we have history push.
So we have history push and I would want to navigate to let's say home page someone go with history
push.
Navigating to a home page and then of course if already my order is successful I can just return from
the function and just say that's it I'm done.
I would want to return.
So we have two cases again.
Either the order is gonna be successful or there's going to be some kind of error if there is error.
We're just shown the alert in this case.
It doesn't make sense to shoulder because it's not like we're showing it within the paragraph.
And then if everything went well then we're going to have show alert with message.
Your order is complete.
Then we clear the card as well as navigate to a homepage.
Of course we haven't set up our function yet.