Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add currency form #472

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions apps/web/vibes/soul/primitives/navigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import * as Popover from '@radix-ui/react-popover';
import { clsx } from 'clsx';
import debounce from 'lodash.debounce';
import { ArrowRight, ChevronDown, Search, SearchIcon, ShoppingBag, User } from 'lucide-react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import React, {
forwardRef,
Ref,
Expand All @@ -29,6 +27,9 @@ import { Logo } from '@/vibes/soul/primitives/logo';
import { Price } from '@/vibes/soul/primitives/price-label';
import { ProductCard } from '@/vibes/soul/primitives/product-card';

import { Link } from '~/components/link';
import { usePathname } from '~/i18n/routing';
Comment on lines +30 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These imports are Catalyst-only


interface Link {
label: string;
href: string;
Expand All @@ -47,6 +48,11 @@ interface Locale {
label: string;
}

interface Currency {
id: string;
label: string;
}

type Action<State, Payload> = (
state: Awaited<State>,
payload: Awaited<Payload>,
Expand All @@ -71,6 +77,7 @@ export type SearchResult =
};

type LocaleAction = Action<SubmissionResult | null, FormData>;
type CurrencyAction = Action<SubmissionResult | null, FormData>;
type SearchAction<S extends SearchResult> = Action<
{
searchResults: S[] | null;
Expand All @@ -92,6 +99,9 @@ interface Props<S extends SearchResult> {
locales?: Locale[];
activeLocaleId?: string;
localeAction?: LocaleAction;
currencies?: Currency[];
activeCurrencyId?: string;
currencyAction?: CurrencyAction;
logo?: Streamable<string | { src: string; alt: string } | null>;
logoWidth?: number;
logoHeight?: number;
Expand Down Expand Up @@ -264,6 +274,9 @@ export const Navigation = forwardRef(function Navigation<S extends SearchResult>
activeLocaleId,
localeAction,
locales,
currencies,
activeCurrencyId,
currencyAction,
searchHref,
searchParamName = 'query',
searchAction,
Expand Down Expand Up @@ -560,6 +573,15 @@ export const Navigation = forwardRef(function Navigation<S extends SearchResult>
locales={locales as [Locale, Locale, ...Locale[]]}
/>
) : null}

{/* Currency Dropdown */}
{currencies && currencies.length > 1 && currencyAction ? (
<CurrencyForm
action={currencyAction}
activeCurrencyId={activeCurrencyId}
currencies={currencies as [Currency, ...Currency[]]}
/>
) : null}
</div>
</div>

Expand Down Expand Up @@ -861,3 +883,61 @@ function LocaleForm({
</DropdownMenu.Root>
);
}

function CurrencyForm({
action,
currencies,
activeCurrencyId,
}: {
activeCurrencyId?: string;
action: CurrencyAction;
currencies: [Currency, ...Currency[]];
}) {
const [lastResult, formAction] = useActionState(action, null);
const activeCurrency = currencies.find((currency) => currency.id === activeCurrencyId);

useEffect(() => {
if (lastResult?.error) console.log(lastResult.error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use alerts here together with useForm from conform to avoid reaching into lastResult which is meant to be opaque

}, [lastResult?.error]);

return (
<DropdownMenu.Root>
<DropdownMenu.Trigger
className={clsx('flex items-center gap-1 text-xs uppercase', navButtonClassName)}
>
{activeCurrency?.label ?? currencies[0].label}
<ChevronDown size={16} strokeWidth={1.5} />
</DropdownMenu.Trigger>
<DropdownMenu.Portal>
<DropdownMenu.Content
align="end"
className="z-50 max-h-80 overflow-y-scroll rounded-xl bg-[var(--nav-locale-background,hsl(var(--background)))] p-2 shadow-xl data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 @4xl:w-32 @4xl:rounded-2xl @4xl:p-2"
sideOffset={16}
>
{currencies.map((currency) => (
<DropdownMenu.Item
className={clsx(
'cursor-default rounded-lg bg-[var(--nav-locale-link-background,transparent)] px-2.5 py-2 font-[family-name:var(--nav-locale-link-font-family,var(--font-family-body))] text-sm font-medium text-[var(--nav-locale-link-text,hsl(var(--contrast-400)))] outline-none ring-[var(--nav-focus,hsl(var(--primary)))] transition-colors hover:bg-[var(--nav-locale-link-background-hover,hsl(var(--contrast-100)))] hover:text-[var(--nav-locale-link-text-hover,hsl(var(--foreground)))]',
{
'text-[var(--nav-locale-link-text-selected,hsl(var(--foreground)))]':
currency.id === activeCurrencyId,
},
)}
key={currency.id}
onSelect={() => {
// eslint-disable-next-line @typescript-eslint/require-await
startTransition(async () => {
const formData = new FormData();
formData.append('id', currency.id);
formAction(formData);
});
}}
>
{currency.label}
</DropdownMenu.Item>
))}
</DropdownMenu.Content>
</DropdownMenu.Portal>
</DropdownMenu.Root>
);
}