Skip to content

Commit a7db909

Browse files
committed
Use flash data
1 parent 6cc0c95 commit a7db909

7 files changed

Lines changed: 316 additions & 252 deletions

File tree

Gemfile.lock

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
action_text-trix (2.1.15)
4+
action_text-trix (2.1.16)
55
railties
66
actioncable (8.1.1)
77
actionpack (= 8.1.1)
@@ -80,13 +80,13 @@ GEM
8080
ast (2.4.3)
8181
authentication-zero (4.0.3)
8282
base64 (0.3.0)
83-
bcrypt (3.1.20)
83+
bcrypt (3.1.21)
8484
bcrypt_pbkdf (1.1.2)
8585
bigdecimal (4.0.1)
8686
bindex (0.8.1)
87-
bootsnap (1.20.0)
87+
bootsnap (1.20.1)
8888
msgpack (~> 1.2)
89-
brakeman (7.1.1)
89+
brakeman (7.1.2)
9090
racc
9191
builder (3.3.0)
9292
bundler-audit (0.9.3)
@@ -135,7 +135,7 @@ GEM
135135
activesupport (>= 6.1)
136136
i18n (1.14.8)
137137
concurrent-ruby (~> 1.0)
138-
inertia_rails (3.15.0)
138+
inertia_rails (3.16.0)
139139
railties (>= 6)
140140
io-console (0.8.2)
141141
irb (1.16.0)
@@ -181,8 +181,7 @@ GEM
181181
marcel (1.1.0)
182182
matrix (0.4.3)
183183
mini_mime (1.1.5)
184-
mini_portile2 (2.8.9)
185-
minitest (6.0.0)
184+
minitest (6.0.1)
186185
prism (~> 1.5)
187186
msgpack (1.8.0)
188187
mutex_m (0.3.0)
@@ -201,22 +200,19 @@ GEM
201200
net-protocol
202201
net-ssh (7.3.0)
203202
nio4r (2.7.5)
204-
nokogiri (1.18.10)
205-
mini_portile2 (~> 2.8.2)
203+
nokogiri (1.19.0-aarch64-linux-gnu)
206204
racc (~> 1.4)
207-
nokogiri (1.18.10-aarch64-linux-gnu)
205+
nokogiri (1.19.0-aarch64-linux-musl)
208206
racc (~> 1.4)
209-
nokogiri (1.18.10-aarch64-linux-musl)
207+
nokogiri (1.19.0-arm-linux-gnu)
210208
racc (~> 1.4)
211-
nokogiri (1.18.10-arm-linux-gnu)
209+
nokogiri (1.19.0-arm-linux-musl)
212210
racc (~> 1.4)
213-
nokogiri (1.18.10-arm-linux-musl)
211+
nokogiri (1.19.0-arm64-darwin)
214212
racc (~> 1.4)
215-
nokogiri (1.18.10-arm64-darwin)
213+
nokogiri (1.19.0-x86_64-linux-gnu)
216214
racc (~> 1.4)
217-
nokogiri (1.18.10-x86_64-linux-gnu)
218-
racc (~> 1.4)
219-
nokogiri (1.18.10-x86_64-linux-musl)
215+
nokogiri (1.19.0-x86_64-linux-musl)
220216
racc (~> 1.4)
221217
ostruct (0.6.3)
222218
parallel (1.27.0)
@@ -234,7 +230,7 @@ GEM
234230
psych (5.3.1)
235231
date
236232
stringio
237-
public_suffix (7.0.0)
233+
public_suffix (7.0.2)
238234
puma (7.1.0)
239235
nio4r (~> 2.0)
240236
raabro (1.4.0)
@@ -317,14 +313,14 @@ GEM
317313
rubocop-ast (>= 1.48.0, < 2.0)
318314
ruby-progressbar (~> 1.7)
319315
unicode-display_width (>= 2.4.0, < 4.0)
320-
rubocop-ast (1.48.0)
316+
rubocop-ast (1.49.0)
321317
parser (>= 3.3.7.2)
322-
prism (~> 1.4)
318+
prism (~> 1.7)
323319
rubocop-performance (1.26.1)
324320
lint_roller (~> 1.1)
325321
rubocop (>= 1.75.0, < 2.0)
326322
rubocop-ast (>= 1.47.1, < 2.0)
327-
rubocop-rails (2.34.2)
323+
rubocop-rails (2.34.3)
328324
activesupport (>= 4.2.0)
329325
lint_roller (~> 1.1)
330326
rack (>= 1.1)
@@ -360,16 +356,14 @@ GEM
360356
fugit (~> 1.11)
361357
railties (>= 7.1)
362358
thor (>= 1.3.1)
363-
sorbet-runtime (0.6.12872)
364-
sqlite3 (2.8.1)
365-
mini_portile2 (~> 2.8.0)
366-
sqlite3 (2.8.1-aarch64-linux-gnu)
367-
sqlite3 (2.8.1-aarch64-linux-musl)
368-
sqlite3 (2.8.1-arm-linux-gnu)
369-
sqlite3 (2.8.1-arm-linux-musl)
370-
sqlite3 (2.8.1-arm64-darwin)
371-
sqlite3 (2.8.1-x86_64-linux-gnu)
372-
sqlite3 (2.8.1-x86_64-linux-musl)
359+
sorbet-runtime (0.6.12873)
360+
sqlite3 (2.9.0-aarch64-linux-gnu)
361+
sqlite3 (2.9.0-aarch64-linux-musl)
362+
sqlite3 (2.9.0-arm-linux-gnu)
363+
sqlite3 (2.9.0-arm-linux-musl)
364+
sqlite3 (2.9.0-arm64-darwin)
365+
sqlite3 (2.9.0-x86_64-linux-gnu)
366+
sqlite3 (2.9.0-x86_64-linux-musl)
373367
sshkit (1.25.0)
374368
base64
375369
logger
@@ -392,7 +386,7 @@ GEM
392386
unicode-emoji (4.2.0)
393387
uri (1.1.1)
394388
useragent (0.16.11)
395-
vite_rails (3.0.19)
389+
vite_rails (3.0.20)
396390
railties (>= 5.1, < 9)
397391
vite_ruby (~> 3.0, >= 3.2.2)
398392
vite_ruby (3.9.2)

app/controllers/inertia_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
class InertiaController < ApplicationController
44
inertia_config default_render: true
5-
inertia_share flash: -> { flash.to_hash },
6-
auth: {
5+
inertia_share auth: {
76
user: -> { Current.user.as_json(only: %i[id name email verified created_at updated_at]) },
87
session: -> { Current.session.as_json(only: %i[id]) }
98
}

app/frontend/hooks/use-flash.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1-
import { router } from "@inertiajs/react"
1+
import { router, usePage } from "@inertiajs/react"
2+
import { useEffect, useRef } from "react"
23
import { toast } from "sonner"
34

4-
import type { Flash } from "@/types"
5+
import type { FlashData } from "@/types"
56

6-
export const useFlash = () => {
7-
router.on("beforeUpdate", (event) => {
8-
const flash = event.detail.page.props.flash as Flash
9-
if (flash.alert) {
10-
toast.error(flash.alert)
11-
}
12-
if (flash.notice) {
13-
toast(flash.notice)
7+
function showFlash(flash: FlashData) {
8+
if (flash.alert) {
9+
toast.error(flash.alert)
10+
}
11+
if (flash.notice) {
12+
toast(flash.notice)
13+
}
14+
}
15+
16+
export function useFlash() {
17+
const { flash } = usePage()
18+
const toastShowed = useRef(false)
19+
20+
useEffect(() => {
21+
if (!toastShowed.current) {
22+
toastShowed.current = true
23+
showFlash(flash)
1424
}
15-
event.detail.page.props.flash = {}
16-
})
25+
}, [flash])
26+
27+
useEffect(() => {
28+
return router.on("flash", (event) => {
29+
const flash = event.detail.flash
30+
showFlash(flash)
31+
})
32+
}, [])
1733
}

app/frontend/ssr/ssr.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ createServer((page) =>
3535

3636
return page
3737
},
38+
39+
defaults: {
40+
form: {
41+
forceIndicesArrayFormatInFormData: false,
42+
},
43+
future: {
44+
useScriptElementForInitialPage: true,
45+
useDataInertiaHeadAttribute: true,
46+
useDialogForErrorModal: true,
47+
preserveEqualProps: true,
48+
},
49+
},
50+
3851
setup: ({ App, props }) => createElement(App, props),
3952
}),
4053
)

app/frontend/types/global.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { SharedData } from "./index"
1+
import type { FlashData, SharedData } from "./index"
22

33
declare module "@inertiajs/core" {
44
export interface InertiaConfig {
55
sharedPageProps: SharedData
6+
flashDataType: FlashData
67
errorValueType: string[]
78
}
89
}

app/frontend/types/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ export interface NavItem {
1717
isActive?: boolean
1818
}
1919

20-
export interface Flash {
20+
export interface FlashData {
2121
alert?: string
2222
notice?: string
2323
}
2424

2525
export interface SharedData {
2626
auth: Auth
27-
flash: Flash
2827
}
2928

3029
export interface User {

0 commit comments

Comments
 (0)