Dec 29, 2022
0
Level 3
Inertia redirect back with status show me failed status for first ever
Hi, I'm posting a new order in the front end react and in my controller I do all things, but when I return back with status true or false the front-end show me the false status for first ever why?
Controller
public function create(Request $request)
{
$tableNumber = $request->input('tableNumber');
$menuName = $request->input('menuName');
$products = $request->input('products');
$total = $request->input('total');
$status = false;
try {
$menu = Menu::where('name', unslug($menuName))->first();
$order = new Order();
$order->table = $tableNumber;
$order->menu_id = $menu->id;
total_price = $total;
$order->save();
foreach ($products as $product) {
$dish = Dish::where('name', $product['dish_title'])->first();
$order->dishes()->attach($dish, ['quantity' => $product['quantity']]);
}
$users = User::all();
Notification::send($users, new OrderPlacedNotification($order));
} catch(Throwable $e) {
$status = false;
}
return Redirect::route('create-order', ['tableNumber' => $tableNumber, 'menuName' => $menuName])->with('status', $status);
}
Shared with Inertia:
public function share(Request $request)
{
return array_merge(parent::share($request), [
'notifications' => [
'unreadNotifications' => $request->user() ? $request->user()->unreadNotifications()->get() : null
],
'flash' => [
'status' => $request->session()->get('status')
],
'auth' => [
'user' => $request->user(),
],
'ziggy' => function () use ($request) {
return array_merge((new Ziggy)->toArray(), [
'location' => $request->url(),
]);
},
]);
}
front-end:
import LayoutApp from '@/Layouts/LayoutApp';
import { Head, usePage } from '@inertiajs/inertia-react';
import PageTitle from '@/Components/PageTitle';
import OrderHorizontalCard from '@/Components/OrderHorizontalCard';
import ShoppingCart from '@/Components/ShoppingCart';
import { useState } from 'react';
import ToastMessage from '@/Components/ToastMessage';
const MakeOrderPage = (props) => {
const tableNumber = props.table_number;
const menu = props.menu[0];
const dishes = props.menu[0].dishes;
const orderStatus = props.flash.status;
const [menuOpen, setMenuOpen] = useState(false);
const [productCounter, setProductCounter] = useState(0);
const [products, setProducts] = useState([]);
const [total, setTotal] = useState(0);
const [pulse, setPulse] = useState(false);
const [toastVisible, setToastVisible] = useState(false);
const openMenu = () => {
setMenuOpen(!menuOpen);
setPulse(false);
}
return (
<>
<Head title={menu.name} />
<PageTitle text={`Ordine tavolo: ${tableNumber}`} className={'text-center'}/>
<div className="cards-container max-w-xs md:max-w-lg flex flex-col items-center justify-center md:py-3 space-y-9 mx-auto">
{dishes.map(dish => {
return(
<OrderHorizontalCard key={dish.id} title={dish.name} description={dish.description} image={dish.image} price={dish.price} category={dish.dish_category.name} productCounter={productCounter} setProductCounter={setProductCounter} products={products} setProducts={setProducts} total={total} setTotal={setTotal} pulse={pulse} setPulse={setPulse} />
)
})}
</div>
<ShoppingCart menuOpen={menuOpen} setMenuOpen={setMenuOpen} tableNumber={tableNumber} menuName={menu.slug} products={products} setProducts={setProducts} productCounter={productCounter} setProductCounter={setProductCounter} total={total} setTotal={setTotal} pulse={pulse} setToastVisible={setToastVisible} />
<div className={` fixed bottom-4 -left-4 text-end w-full`}>
{productCounter ? (
<span className="absolute right-0 -top-3 badge mb-3 bg-red-800 rounded-full px-2 py-1 text-center object-right-top text-white text-xs">{productCounter}</span>
) : ''}
<button title="Concludi l'ordine" onClick={openMenu} className={`${pulse ? 'animate-pulse' : ''} p-1 w-12 h-12 bg-blue-800 rounded-full hover:bg-blue-900 mouse transition ease-in duration-200 focus:outline-none shadow-sm shadow-slate-900`}>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-6 h-6 inline-block">
<path stroke='#fff' strokeLinecap="round" strokeLinejoin="round" d="M15.75 10.5V6a3.75 3.75 0 10-7.5 0v4.5m11.356-1.993l1.263 12c.07.665-.45 1.243-1.119 1.243H4.25a1.125 1.125 0 01-1.12-1.243l1.264-12A1.125 1.125 0 015.513 7.5h12.974c.576 0 1.059.435 1.119 1.007zM8.625 10.5a.375.375 0 11-.75 0 .375.375 0 01.75 0zm7.5 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z" />
</svg>
</button>
</div>
{toastVisible ? (
<ToastMessage success={orderStatus} message={orderStatus ? 'Ordine creato' : 'Errore nella creazione dell\'ordine'} setToastVisible={setToastVisible} />
) : (
''
)}
</>
);
}
MakeOrderPage.layout = page => <LayoutApp header={<h2 className="font-semibold text-xl text-gray-800 leading-tight">Ordina a tavolo</h2>} children={page}></LayoutApp>
export default MakeOrderPage;
Please or to participate in this conversation.