Skip to content

Commit

Permalink
Refactor auth section into AuthSection component and integrate transl…
Browse files Browse the repository at this point in the history
…ation hook for login button text. Fix translation key formatting by replacing hyphens with underscores. Update login loader to use dynamic language setting.
  • Loading branch information
jamalsoueidan committed Jun 13, 2024
1 parent 691af21 commit 1288328
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 58 deletions.
121 changes: 65 additions & 56 deletions app/components/LayoutWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
IconShoppingCartPlus,
} from '@tabler/icons-react';
import {Suspense, type ReactNode} from 'react';
import {TranslationProvider} from '~/providers/Translation';
import {TranslationProvider, useTranslations} from '~/providers/Translation';
import {type loader} from '~/root';
import {Footer} from './Footer';
import logo from '/Artboard4.svg';
Expand Down Expand Up @@ -159,61 +159,7 @@ export function LayoutWrapper({children}: {children: ReactNode}) {
);
})}
</Flex>
<Flex
justify="flex-end"
align="center"
gap="sm"
visibleFrom="sm"
miw="150px"
>
<Suspense>
<Await resolve={data.isLoggedIn}>
{(isLoggedIn) =>
isLoggedIn ? (
<ActionIcon
variant="outline"
size="lg"
color="black"
component={Link}
to="/account"
data-testid="login-button"
>
<IconKey />
</ActionIcon>
) : (
<Button
variant="outline"
size="sm"
color="black"
component={Link}
to="/account"
data-testid="login-button"
>
Log ind
</Button>
)
}
</Await>
</Suspense>
<Suspense>
<Await resolve={data.cart}>
{(cart) =>
cart?.totalQuantity && cart?.totalQuantity > 0 ? (
<ActionIcon
variant="outline"
size="lg"
color="black"
component={Link}
to="/cart"
data-testid="cart-button"
>
<IconShoppingCartPlus />
</ActionIcon>
) : null
}
</Await>
</Suspense>
</Flex>
<AuthSection />
</AppShell.Header>

<AppShell.Navbar px={4}>
Expand Down Expand Up @@ -292,3 +238,66 @@ export function LayoutWrapper({children}: {children: ReactNode}) {
</TranslationProvider>
);
}

function AuthSection() {
const data = useLoaderData<typeof loader>();
const {t} = useTranslations();

return (
<Flex
justify="flex-end"
align="center"
gap="sm"
visibleFrom="sm"
miw="150px"
>
<Suspense>
<Await resolve={data.isLoggedIn}>
{(isLoggedIn) =>
isLoggedIn ? (
<ActionIcon
variant="outline"
size="lg"
color="black"
component={Link}
to="/account"
data-testid="login-button"
>
<IconKey />
</ActionIcon>
) : (
<Button
variant="outline"
size="sm"
color="black"
component={Link}
to="/account"
data-testid="login-button"
>
{t('login')}
</Button>
)
}
</Await>
</Suspense>
<Suspense>
<Await resolve={data.cart}>
{(cart) =>
cart?.totalQuantity && cart?.totalQuantity > 0 ? (
<ActionIcon
variant="outline"
size="lg"
color="black"
component={Link}
to="/cart"
data-testid="cart-button"
>
<IconShoppingCartPlus />
</ActionIcon>
) : null
}
</Await>
</Suspense>
</Flex>
);
}
2 changes: 1 addition & 1 deletion app/providers/Translation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const convertJsonStructure = (data?: TranslationsFragment[] | null) => {
const result: Record<string, string> = {};

data?.forEach((node) => {
const key = node.handle;
const key = node.handle.replace(/-/g, '_');
const value = node.value?.value;
if (key && value) {
result[key] = value;
Expand Down
2 changes: 1 addition & 1 deletion app/routes/account_.login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type {LoaderFunctionArgs} from '@shopify/remix-oxygen';

export async function loader({context}: LoaderFunctionArgs) {
return context.customerAccount.login({
uiLocales: 'DA',
uiLocales: context.storefront.i18n.language,
});
}

0 comments on commit 1288328

Please sign in to comment.