Skip to content

Commit 5657a52

Browse files
committed
fix: flash
1 parent 4352e0f commit 5657a52

6 files changed

Lines changed: 39 additions & 9 deletions

File tree

app/controllers/inertia_controller.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
class InertiaController < ApplicationController
4-
# Share data with all Inertia responses
5-
# see https://inertia-rails.dev/guide/shared-data
64
inertia_share flash: -> {
75
{
86
notice: flash[:notice],

app/controllers/inertia_example_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ def index
2020
def demo
2121
render inertia: {}
2222
end
23+
24+
def create
25+
flash[:notice] = "POST request successful! Toast triggered from backend."
26+
redirect_to controller: "inertia_example", action: "demo"
27+
end
2328
end

app/frontend/components/Layout.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
import { usePage } from '@inertiajs/react'
12
import type { ReactNode } from 'react'
3+
import { useEffect } from 'react'
4+
import { toast } from 'sonner'
5+
import { Toaster } from '@/components/ui/sonner'
26

37
interface LayoutProps {
48
children: ReactNode
59
}
610

711
export default function Layout({ children }: LayoutProps) {
8-
return children
12+
const { flash } = usePage().props as { flash?: { notice?: string; alert?: string } }
13+
14+
useEffect(() => {
15+
if (flash?.notice) toast.success(flash.notice)
16+
if (flash?.alert) toast.error(flash.alert)
17+
}, [flash?.notice, flash?.alert])
18+
19+
return (
20+
<>
21+
<Toaster position="top-right" richColors closeButton />
22+
{children}
23+
</>
24+
)
925
}

app/frontend/entrypoints/inertia.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ createInertiaApp({
4444

4545
const Component = page.default ?? page
4646

47-
const WrappedComponent: any = (props: any) => {
48-
return createElement(Layout, null, createElement(Component, props))
47+
if (!Component.layout) {
48+
Component.layout = (page: ReactNode) => createElement(Layout, null, page)
4949
}
5050

51-
WrappedComponent.layout =
52-
Component.layout || ((page: ReactNode) => createElement(Layout, null, page))
53-
54-
return WrappedComponent
51+
return Component
5552
},
5653

5754
setup({ el, App, props }) {

app/frontend/pages/inertia_example/demo.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { router } from '@inertiajs/react'
12
import { useEffect, useState } from 'react'
23
import {
34
Accordion,
@@ -152,6 +153,18 @@ const Demo = () => {
152153
</div>
153154
</div>
154155

156+
<Card>
157+
<CardHeader>
158+
<CardTitle className="text-base">Flash Messages Test</CardTitle>
159+
<CardDescription>Click buttons to trigger flash messages via Inertia</CardDescription>
160+
</CardHeader>
161+
<CardContent className="flex flex-wrap gap-2">
162+
<Button variant="default" onClick={() => router.post('/demo')}>
163+
Trigger POST Toast
164+
</Button>
165+
</CardContent>
166+
</Card>
167+
155168
<Tabs value={tabsValue} onValueChange={setTabsValue}>
156169
<TabsList>
157170
<TabsTrigger value="tab1">Form Controls</TabsTrigger>

config/routes/demo.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
root "inertia_example#index"
44
get "inertia-example", to: "inertia_example#index"
55
get "demo", to: "inertia_example#demo"
6+
post "demo", to: "inertia_example#create"

0 commit comments

Comments
 (0)