1- import { type SessionStorage , createCookieSessionStorage , data as dataFn , redirect } from "react-router" ;
1+ import { type SessionStorage , createCookieSessionStorage , data as dataFn , redirect , replace } from "react-router" ;
22import {
33 FLASH_SESSION ,
44 type FlashSessionValues ,
@@ -49,6 +49,18 @@ async function redirectWithFlash(
4949 } ) ;
5050}
5151
52+ async function replaceWithFlash (
53+ url : string ,
54+ flash : FlashSessionValues ,
55+ init ?: ResponseInit ,
56+ customSession ?: SessionStorage ,
57+ ) {
58+ return replace ( url , {
59+ ...init ,
60+ headers : await flashMessage ( flash , init ?. headers , customSession ) ,
61+ } ) ;
62+ }
63+
5264async function dataWithFlash < T > (
5365 data : T ,
5466 flash : FlashSessionValues ,
@@ -90,6 +102,18 @@ const redirectWithToastFactory = ({ type, session }: BaseFactoryType) => {
90102 } ;
91103} ;
92104
105+ const replaceWithToastFactory = ( { type, session } : BaseFactoryType ) => {
106+ return (
107+ replaceUrl : string ,
108+ messageOrToast : string | ToastMessageWithoutType ,
109+ init ?: ResponseInit ,
110+ customSession ?: SessionStorage ,
111+ ) => {
112+ const finalInfo = typeof messageOrToast === "string" ? { message : messageOrToast } : messageOrToast ;
113+ return replaceWithFlash ( replaceUrl , { toast : { ...finalInfo , type } } , init , customSession ?? session ) ;
114+ } ;
115+ } ;
116+
93117/**
94118 * Helper method used to get the toast data from the current request and purge the flash storage from the session
95119 * @param request Current request
@@ -134,10 +158,17 @@ export const createToastUtilsWithCustomSession = (session: SessionStorage) => {
134158 redirectWithToast : ( redirectUrl : string , toast : ToastMessage , init ?: ResponseInit ) => {
135159 return redirectWithFlash ( redirectUrl , { toast } , init , session ) ;
136160 } ,
161+ replaceWithToast : ( replaceUrl : string , toast : ToastMessage , init ?: ResponseInit ) => {
162+ return replaceWithFlash ( replaceUrl , { toast } , init , session ) ;
163+ } ,
137164 redirectWithSuccess : redirectWithToastFactory ( { type : "success" , session } ) ,
138165 redirectWithError : redirectWithToastFactory ( { type : "error" , session } ) ,
139166 redirectWithInfo : redirectWithToastFactory ( { type : "info" , session } ) ,
140167 redirectWithWarning : redirectWithToastFactory ( { type : "warning" , session } ) ,
168+ replaceWithSuccess : replaceWithToastFactory ( { type : "success" , session } ) ,
169+ replaceWithError : replaceWithToastFactory ( { type : "error" , session } ) ,
170+ replaceWithInfo : replaceWithToastFactory ( { type : "info" , session } ) ,
171+ replaceWithWarning : replaceWithToastFactory ( { type : "warning" , session } ) ,
141172 getToast : ( request : Request ) => getToast ( request , session ) ,
142173 } ;
143174} ;
@@ -211,6 +242,24 @@ export const redirectWithToast = (
211242 return redirectWithFlash ( redirectUrl , { toast } , init , customSession ) ;
212243} ;
213244
245+ /**
246+ * Helper method used to replace the current route with a toast notification
247+ *
248+ * If thrown it needs to be awaited
249+ * @param replaceUrl Replace URL
250+ * @param toast Toast message and it's type
251+ * @param init Additional response options (status code, additional headers etc)
252+ * @returns Returns replace response with toast cookie set
253+ */
254+ export const replaceWithToast = (
255+ replaceUrl : string ,
256+ toast : ToastMessage ,
257+ init ?: ResponseInit ,
258+ customSession ?: SessionStorage ,
259+ ) => {
260+ return replaceWithFlash ( replaceUrl , { toast } , init , customSession ) ;
261+ } ;
262+
214263/**
215264 * Helper method used to redirect the user to a new page with an error toast notification
216265 *
@@ -222,6 +271,17 @@ export const redirectWithToast = (
222271 */
223272export const redirectWithError = redirectWithToastFactory ( { type : "error" } ) ;
224273
274+ /**
275+ * Helper method used to replace the current route with an error toast notification
276+ *
277+ * If this method is thrown it needs to be awaited, otherwise it can just be returned
278+ * @param replaceUrl Replace url
279+ * @param message Message to be shown as info
280+ * @param init Additional response options (status code, additional headers etc)
281+ * @returns Returns replace response with toast cookie set
282+ */
283+ export const replaceWithError = replaceWithToastFactory ( { type : "error" } ) ;
284+
225285/**
226286 * Helper method used to redirect the user to a new page with a success toast notification
227287 *
@@ -233,6 +293,17 @@ export const redirectWithError = redirectWithToastFactory({ type: "error" });
233293 */
234294export const redirectWithSuccess = redirectWithToastFactory ( { type : "success" } ) ;
235295
296+ /**
297+ * Helper method used to replace the current route with a success toast notification
298+ *
299+ * If this method is thrown it needs to be awaited, otherwise it can just be returned
300+ * @param replaceUrl Replace url
301+ * @param message Message to be shown as info
302+ * @param init Additional response options (status code, additional headers etc)
303+ * @returns Returns replace response with toast cookie set
304+ */
305+ export const replaceWithSuccess = replaceWithToastFactory ( { type : "success" } ) ;
306+
236307/**
237308 * Helper method used to redirect the user to a new page with a warning toast notification
238309 *
@@ -244,6 +315,17 @@ export const redirectWithSuccess = redirectWithToastFactory({ type: "success" })
244315 */
245316export const redirectWithWarning = redirectWithToastFactory ( { type : "warning" } ) ;
246317
318+ /**
319+ * Helper method used to replace the current route with a warning toast notification
320+ *
321+ * If this method is thrown it needs to be awaited, otherwise it can just be returned
322+ * @param replaceUrl Replace url
323+ * @param message Message to be shown as info
324+ * @param init Additional response options (status code, additional headers etc)
325+ * @returns Returns replace response with toast cookie set
326+ */
327+ export const replaceWithWarning = replaceWithToastFactory ( { type : "warning" } ) ;
328+
247329/**
248330 * Helper method used to redirect the user to a new page with a info toast notification
249331 *
@@ -254,3 +336,14 @@ export const redirectWithWarning = redirectWithToastFactory({ type: "warning" })
254336 * @returns Returns redirect response with toast cookie set
255337 */
256338export const redirectWithInfo = redirectWithToastFactory ( { type : "info" } ) ;
339+
340+ /**
341+ * Helper method used to replace the current route with an info toast notification
342+ *
343+ * If this method is thrown it needs to be awaited, otherwise it can just be returned
344+ * @param replaceUrl Replace url
345+ * @param message Message to be shown as info
346+ * @param init Additional response options (status code, additional headers etc)
347+ * @returns Returns replace response with toast cookie set
348+ */
349+ export const replaceWithInfo = replaceWithToastFactory ( { type : "info" } ) ;
0 commit comments