Skip to content

Commit

Permalink
feat: add redirect after checkout, protect success screen
Browse files Browse the repository at this point in the history
closes #181
  • Loading branch information
kavabunga committed Dec 27, 2023
1 parent 7d48e48 commit 8f43c51
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/pages/checkout/checkout-success/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import React from 'react';
import { useLocation } from 'react-router';
import { useLocation, useNavigate } from 'react-router';
import styles from './checkout-success.module.scss';
import OurBlock from '@components/our-block';
import { Link } from 'react-router-dom';
import OrderStatusTracker from '@components/order-status-tracker';

const CheckoutSuccess: React.FC = () => {
// TODO: Add check for active order and redirect
const [order, setOrder] = React.useState<string>('');
const location = useLocation();
const navigate = useNavigate();

React.useEffect(
() => location.state?.order && setOrder(location.state.order),
[location]
() =>
location.state?.order
? setOrder(location.state.order)
: navigate('/', { replace: true }),
[location, navigate]
);

return (
Expand Down
5 changes: 2 additions & 3 deletions src/pages/checkout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ const Checkout: React.FC = () => {

api
.usersOrderCreate(formData)
.then(() => {
navigate('/cart');
.then((res) => {
navigate('/cart/success', { state: { order: res.order_number } });
loadCartData();
alert('заказ оформлен');
})
.catch((error) => {
if (error.response && error.response.data && error.response.data.errors) {
Expand Down

0 comments on commit 8f43c51

Please sign in to comment.