Skip to content

Commit

Permalink
fix: Use redirect function from next-intl instead from next/navigation (
Browse files Browse the repository at this point in the history
  • Loading branch information
karolkarolka committed Jan 28, 2025
1 parent bc60913 commit 2dd27af
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { redirect } from "next/navigation";
import { getTranslations } from "next-intl/server";
import { getLocale, getTranslations } from "next-intl/server";

import { redirect } from "@/i18n/routing";
import { paths } from "@/lib/paths";
import { authService } from "@/services";

Expand All @@ -11,6 +11,7 @@ export default async function ConfirmAccountRegistrationPage(props: {
const t = await getTranslations();
const email = searchParams?.email;
const token = searchParams?.token;
const locale = await getLocale();

if (!email) {
return t("auth.confirm-missing-email");
Expand All @@ -23,7 +24,10 @@ export default async function ConfirmAccountRegistrationPage(props: {
const data = await authService.confirmAccount(searchParams);

if (data.isSuccess) {
redirect(paths.signIn.asPath({ query: { confirmationSuccess: "true" } }));
redirect({
href: paths.signIn.asPath({ query: { confirmationSuccess: "true" } }),
locale,
});
}

if ("errors" in data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { redirect } from "next/navigation";
import { getTranslations } from "next-intl/server";
import { getLocale, getTranslations } from "next-intl/server";

import { getAccessToken } from "@/auth";
import { redirect } from "@/i18n/routing";
import { paths } from "@/lib/paths";
import { getCurrentRegion } from "@/regions/server";
import { userService } from "@/services";
Expand All @@ -12,6 +12,7 @@ export default async function ConfirmEmailChangePage(props: {
const searchParams = await props.searchParams;
const token = searchParams?.token ?? "";
const accessToken = await getAccessToken();
const locale = await getLocale();

const [region, t] = await Promise.all([
getCurrentRegion(),
Expand All @@ -26,7 +27,7 @@ export default async function ConfirmEmailChangePage(props: {
});

if (data?.user?.id && !data?.errors.length) {
redirect(paths.signIn.asPath());
redirect({ href: paths.signIn.asPath(), locale });
}

if (data?.errors) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { getLocale } from "next-intl/server";

import { getAccessToken } from "@/auth";
import { COOKIE_KEY } from "@/config";
import { redirect } from "@/i18n/routing";
import { deleteCheckoutIdCookie } from "@/lib/actions/checkout";
import { paths } from "@/lib/paths";
import { getCurrentRegion } from "@/regions/server";
Expand All @@ -16,13 +17,12 @@ import { DeliveryMethodForm } from "./form";

export default async function Page() {
const checkoutId = (await cookies()).get(COOKIE_KEY.checkoutId)?.value;

const accessToken = await getAccessToken();

const locale = await getLocale();
const region = await getCurrentRegion();

if (!checkoutId) {
redirect(paths.cart.asPath());
redirect({ href: paths.cart.asPath(), locale });
}

const { checkout } = await checkoutService.checkoutGet({
Expand All @@ -33,7 +33,7 @@ export default async function Page() {

if (!checkout) {
await deleteCheckoutIdCookie();
redirect(paths.cart.asPath());
redirect({ href: paths.cart.asPath(), locale });
}

if (checkout.problems.insufficientStock.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { getLocale } from "next-intl/server";

import type { CountryCode } from "@nimara/codegen/schema";

import { getAccessToken } from "@/auth";
import { COOKIE_KEY } from "@/config";
import { redirect } from "@/i18n/routing";
import { deleteCheckoutIdCookie } from "@/lib/actions/checkout";
import { paths } from "@/lib/paths";
import { getCurrentRegion } from "@/regions/server";
Expand All @@ -23,13 +24,14 @@ export default async function Page(props: { searchParams: SearchParams }) {
const searchParams = await props.searchParams;
const checkoutId = (await cookies()).get(COOKIE_KEY.checkoutId)?.value;
const accessToken = await getAccessToken();
const locale = await getLocale();

const region = await getCurrentRegion();

const marketCountryCode = region.market.countryCode;

if (!checkoutId) {
redirect(paths.cart.asPath());
redirect({ href: paths.cart.asPath(), locale });
}

const { checkout } = await checkoutService.checkoutGet({
Expand All @@ -40,7 +42,7 @@ export default async function Page(props: { searchParams: SearchParams }) {

if (!checkout) {
await deleteCheckoutIdCookie();
redirect(paths.cart.asPath());
redirect({ href: paths.cart.asPath(), locale });
}

if (checkout.problems.insufficientStock.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { getLocale } from "next-intl/server";

import { getAccessToken } from "@/auth";
import { COOKIE_KEY } from "@/config";
import { redirect } from "@/i18n/routing";
import { deleteCheckoutIdCookie } from "@/lib/actions/checkout";
import { paths } from "@/lib/paths";
import { getCurrentRegion } from "@/regions/server";
Expand All @@ -16,11 +17,11 @@ import { UserDetailsForm } from "./form";

export default async function Page() {
const checkoutId = (await cookies()).get(COOKIE_KEY.checkoutId)?.value;

const locale = await getLocale();
const region = await getCurrentRegion();

if (!checkoutId) {
redirect(paths.cart.asPath());
redirect({ href: paths.cart.asPath(), locale });
}

const accessToken = await getAccessToken();
Expand All @@ -43,7 +44,7 @@ export default async function Page() {

if (!checkout) {
await deleteCheckoutIdCookie();
redirect(paths.cart.asPath());
redirect({ href: paths.cart.asPath(), locale });
}

if (checkout.problems.insufficientStock.length) {
Expand Down
21 changes: 9 additions & 12 deletions apps/storefront/src/app/[locale]/(checkout)/checkout/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { getLocale } from "next-intl/server";

import { getAccessToken } from "@/auth";
import { COOKIE_KEY } from "@/config";
import { redirect } from "@/i18n/routing";
import { deleteCheckoutIdCookie } from "@/lib/actions/checkout";
import { paths } from "@/lib/paths";
import { getCurrentRegion } from "@/regions/server";
Expand All @@ -11,14 +12,15 @@ import { checkoutService, userService } from "@/services";
export default async function Page() {
const checkoutId = (await cookies()).get(COOKIE_KEY.checkoutId)?.value;
const accessToken = await getAccessToken();
const locale = await getLocale();

const [region, user] = await Promise.all([
getCurrentRegion(),
userService.userGet(accessToken),
]);

if (!checkoutId) {
redirect(paths.cart.asPath());
redirect({ href: paths.cart.asPath(), locale });
}

const { checkout } = await checkoutService.checkoutGet({
Expand All @@ -27,27 +29,22 @@ export default async function Page() {
countryCode: region.market.countryCode,
});

// TODO handle different cases
// if checkout is empty? what than? how to check it?
// checkout?.order?.status === "complete"; // how to check it
// in case of gone or its an order? different redirect in this cases?

if (!checkout) {
await deleteCheckoutIdCookie();
redirect(paths.cart.asPath());
redirect({ href: paths.cart.asPath(), locale });
}

if (!checkout.email && !user?.email) {
redirect(paths.checkout.userDetails.asPath());
redirect({ href: paths.checkout.userDetails.asPath(), locale });
}

if (user?.email || !checkout.shippingAddress) {
redirect(paths.checkout.shippingAddress.asPath());
redirect({ href: paths.checkout.shippingAddress.asPath(), locale });
}

if (!checkout.deliveryMethod) {
redirect(paths.checkout.deliveryMethod.asPath());
redirect({ href: paths.checkout.deliveryMethod.asPath(), locale });
}

redirect(paths.checkout.payment.asPath());
redirect({ href: paths.checkout.payment.asPath(), locale });
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"use server";

import { redirect } from "next/navigation";
import { getLocale } from "next-intl/server";

import { getAccessToken } from "@/auth";
import { redirect } from "@/i18n/routing";
import { paths } from "@/lib/paths";
import { getStoreUrl } from "@/lib/server";
import { getCurrentRegion } from "@/regions/server";
import { userService } from "@/services";

export async function requestUserAccountDeletion() {
const region = await getCurrentRegion();

const locale = await getLocale();
const accessToken = await getAccessToken();

const data = await userService.accountRequestDeletion({
Expand All @@ -20,12 +21,16 @@ export async function requestUserAccountDeletion() {
});

if (data?.errors.length) {
redirect(
paths.account.privacySettings.asPath({ query: { error: "true" } }),
);
redirect({
href: paths.account.privacySettings.asPath({ query: { error: "true" } }),
locale,
});
}

redirect(
paths.account.privacySettings.asPath({ query: { emailSent: "true" } }),
);
redirect({
href: paths.account.privacySettings.asPath({
query: { emailSent: "true" },
}),
locale,
});
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { redirect } from "next/navigation";
import { getLocale } from "next-intl/server";

import { redirect } from "@/i18n/routing";
import { getCheckoutOrRedirect } from "@/lib/checkout";
import { paths, QUERY_PARAMS } from "@/lib/paths";
import { checkoutService, paymentService } from "@/services";
Expand All @@ -10,6 +11,7 @@ type SearchParams = Promise<Record<string, string>>;

export default async function Page(props: { searchParams: SearchParams }) {
const searchParams = await props.searchParams;
const locale = await getLocale();
const checkout = await getCheckoutOrRedirect();

let errors: { code: string; type: string }[] = [];
Expand All @@ -25,26 +27,28 @@ export default async function Page(props: { searchParams: SearchParams }) {
});

if (orderCreateData.orderId) {
redirect(
paths.order.confirmation.asPath({
redirect({
href: paths.order.confirmation.asPath({
id: orderCreateData.orderId,
query: { [QUERY_PARAMS.orderPlace]: "true" },
}),
);
locale,
});
} else {
errors = orderCreateData.errors;
}
} else {
const error = paymentResultData.errors?.[0];
const errorCode = error ? `${error.type}.${error.code}` : "payment.default";

redirect(
paths.checkout.payment.asPath({
redirect({
href: paths.checkout.payment.asPath({
query: {
[QUERY_PARAMS.errorCode]: errorCode,
},
}),
);
locale,
});
}

return (
Expand Down
11 changes: 7 additions & 4 deletions apps/storefront/src/app/[locale]/(main)/search/actions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use server";

import { redirect } from "next/navigation";
import { getLocale } from "next-intl/server";

import { DEFAULT_SORT_BY } from "@/config";
import { redirect } from "@/i18n/routing";
import { paths } from "@/lib/paths";

const passThroughParams = ["sortBy", "limit", "q"] as const;
Expand All @@ -13,6 +14,7 @@ export const handleFiltersFormSubmit = async (
) => {
const formClear = formData.has("clear");
const params = new URLSearchParams();
const locale = await getLocale();

formData.forEach((value, key) => {
if (value && typeof value === "string" && !formClear) {
Expand Down Expand Up @@ -42,9 +44,10 @@ export const handleFiltersFormSubmit = async (
}
});

return redirect(
paths.search.asPath({
return redirect({
href: paths.search.asPath({
query: Object.fromEntries(params),
}),
);
locale,
});
};
9 changes: 7 additions & 2 deletions apps/storefront/src/components/account-menu/actions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"use server";

import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { getLocale } from "next-intl/server";

import { signOut } from "@/auth";
import { redirect } from "@/i18n/routing";
import { handleLogout } from "@/lib/actions/auth";
import { paths } from "@/lib/paths";
import { errorService } from "@/services";

export async function logout() {
await handleLogout();
const locale = await getLocale();

try {
await signOut();
Expand All @@ -18,5 +20,8 @@ export async function logout() {
}

revalidatePath(paths.home.asPath());
redirect(paths.home.asPath({ query: { loggedOut: "true" } }));
redirect({
href: paths.home.asPath({ query: { loggedOut: "true" } }),
locale,
});
}
Loading

0 comments on commit 2dd27af

Please sign in to comment.