Skip to content

Commit

Permalink
update sa toast mutation and change dashboard to route
Browse files Browse the repository at this point in the history
  • Loading branch information
psiddharthdesign committed Jul 29, 2024
1 parent d55881c commit b75a556
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 73 deletions.
51 changes: 0 additions & 51 deletions src/app/(dynamic-pages)/(authenticated-pages)/dashboard/page.tsx

This file was deleted.

19 changes: 19 additions & 0 deletions src/app/(dynamic-pages)/(authenticated-pages)/dashboard/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getInitialOrganizationToRedirectTo } from '@/data/user/organizations';
import { toSiteURL } from '@/utils/helpers';
import { NextRequest, NextResponse } from 'next/server';

export const dynamic = 'force-dynamic';

export async function GET(req: NextRequest) {
try {
const initialOrganization = await getInitialOrganizationToRedirectTo();
if (initialOrganization.status === 'error') {
return NextResponse.redirect(toSiteURL('/500'));
}
return NextResponse.redirect(new URL(`/org/${initialOrganization.data}`, req.url));
} catch (error) {
console.error('Failed to load dashboard:', error);
// Redirect to an error page or show an error message
return NextResponse.redirect(toSiteURL('/500'));
}
}
2 changes: 1 addition & 1 deletion src/app/(dynamic-pages)/(login-pages)/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function Login({

useDidMount(() => {
console.log('prefetching dashboard')
router.prefetch('/dashboard', {
router.prefetch('/org/123', {
kind: PrefetchKind.AUTO
})
})
Expand Down
47 changes: 26 additions & 21 deletions src/hooks/useSAToastMutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type UseMutationResult,
} from "@tanstack/react-query";
import { useRef } from "react";
import { useWillUnmount } from "rooks";
import { toast } from "sonner";

type MutationFn<TData, TVariables> = MutationFunction<TData, TVariables>;
Expand Down Expand Up @@ -43,9 +44,18 @@ export function useSAToastMutation<
options?: Omit<ToastMutationOptions<SAPayload<TData>, TError, TVariables>, 'onSuccess' | 'onError'> & {
onSuccess?: (data: SASuccessPayload<TData>, variables: TVariables, context: unknown) => void;
onError?: (error: Error, variables: TVariables, context: unknown) => void;
dismissOnSuccess?: boolean;
dismissOnError?: boolean;
},
): UseMutationResult<SAPayload<TData>, TError, TVariables> {
const toastIdRef = useRef<string | number | null>(null);

useWillUnmount(() => {
if (toastIdRef.current) {
toast.dismiss(toastIdRef.current);
}
});

return useMutation<SAPayload<TData>, TError, TVariables>(mutationFn, {
...options,
onMutate: async (variables) => {
Expand All @@ -56,7 +66,6 @@ export function useSAToastMutation<
: "Loading...";

toastIdRef.current = toast.loading(loadingMessage);

if (options?.onMutate) {
await options.onMutate(variables);
}
Expand All @@ -66,30 +75,25 @@ export function useSAToastMutation<
const isHandledError = data.status === "error";
// error scenario
if (isHandledError) {
const baseErrorMessage = data.message;
const errorMessage = options?.errorMessage
? typeof options.errorMessage === "function"
? options.errorMessage(
new Error(baseErrorMessage) as TError,
variables,
)
: (options.errorMessage ?? "Error!") + `: ${baseErrorMessage}`
: "Error!";
toast.error(errorMessage, {
throw new Error(data.message);
}
if (options?.dismissOnSuccess && toastIdRef.current) {
toast.dismiss(toastIdRef.current);

} else {
// success scenario
const successMessage = options?.successMessage
? typeof options.successMessage === "function"
? options.successMessage(data, variables)
: options.successMessage
: "Success!";
toast.success(successMessage, {
id: toastIdRef.current ?? undefined,
dismissible: true,
});
return;
}
// success scenario
const successMessage = options?.successMessage
? typeof options.successMessage === "function"
? options.successMessage(data, variables)
: options.successMessage
: "Success!";
toast.success(successMessage, {
id: toastIdRef.current ?? undefined,
});
toastIdRef.current = null;

if (options?.onSuccess) {
options.onSuccess(data, variables, context);
}
Expand All @@ -103,6 +107,7 @@ export function useSAToastMutation<
console.warn("[useSAToastMutation]", errorMessage);
toast.error(errorMessage, {
id: toastIdRef.current ?? undefined,
dismissible: true,
});
toastIdRef.current = null;
if (options?.onError) {
Expand Down

0 comments on commit b75a556

Please sign in to comment.