Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added a currency switcher to the dashboard home screen

Damian Legawiec1839b006710d
diff --git a/docs/developer/core-concepts/reporting.mdx b/docs/developer/core-concepts/reporting.mdx
index 22dece8666..d53ac86df4 100644
--- a/docs/developer/core-concepts/reporting.mdx
+++ b/docs/developer/core-concepts/reporting.mdx
@@ -89,6 +89,13 @@ Spree.reporting.metric :total_sales,
forces a single-currency scope and arrives with a formatted `display` string;
a `:percent` metric arrives as the number a merchant reads (`42.5`), not the
fraction.
+
+ Amounts are **never converted**. A query answers in one currency — the one
+ it names, or the store's default — and every base a money metric reads must
+ filter by it, or two currencies end up added together under one symbol. A
+ store selling in several therefore has as many answers as it has
+ currencies, which is why the home screen offers a currency alongside its
+ channel and date range.
- `ratio: [:numerator, :denominator]` defines a **derived** metric, computed
after aggregation so it is correct for both rows and totals. Average order
value is `ratio: %i[total_sales orders]`, never `AVG()`.
diff --git a/packages/dashboard/e2e/home-dashboard.spec.ts b/packages/dashboard/e2e/home-dashboard.spec.ts
index 57203c5222..b25cbffdf8 100644
--- a/packages/dashboard/e2e/home-dashboard.spec.ts
+++ b/packages/dashboard/e2e/home-dashboard.spec.ts
@@ -27,6 +27,23 @@ test.describe('home dashboard', () => {
await expect(page.getByText(/^this period$/i)).toBeVisible()
})
+ // A store selling in several currencies sums each one separately — the
+ // figures are never converted — so the screen has to say which one it is
+ // showing and let the merchant change it.
+ test('scopes the figures to a currency the store sells in', async ({ page }) => {
+ const creds = await login(page)
+ await page.goto(HOME_PATH(creds.store_id))
+
+ const totalSales = page.getByRole('button', { name: /total sales/i })
+ await expect(totalSales).toContainText('$', { timeout: 15_000 })
+
+ await page.locator('#home-currency').click()
+ await page.getByRole('option', { name: /EUR/ }).click()
+
+ await expect(totalSales).toContainText('€', { timeout: 15_000 })
+ await expect(totalSales).not.toContainText('$')
+ })
+
test('renders the operations and rankings widgets', async ({ page }) => {
const creds = await login(page)
await page.goto(HOME_PATH(creds.store_id))
diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.json
index f9a8574798..71f42c013b 100644
--- a/packages/dashboard/src/locales/ar.json
+++ b/packages/dashboard/src/locales/ar.json
@@ -3274,6 +3274,9 @@
"unavailable": {
"title": "التقارير غير متاحة لدورك",
"description": "اطلب من المسؤول صلاحية «التقارير» (مع الوصول إلى الطلبات) لعرض أرقام المبيعات هنا."
+ },
+ "currency": {
+ "search": "ابحث عن العملات"
}
},
"orders": {
diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.json
index 749c22875d..417ffdff78 100644
--- a/packages/dashboard/src/locales/de.json
+++ b/packages/dashboard/src/locales/de.json
@@ -3249,6 +3249,9 @@
"unavailable": {
"title": "Berichte sind für Ihre Rolle nicht verfügbar",
"description": "Bitten Sie einen Administrator um die Berechtigung „Berichte“ (mit Zugriff auf Bestellungen), um hier Verkaufszahlen zu sehen."
+ },
+ "currency": {
+ "search": "Währungen suchen"
}
},
"orders": {
diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.json
index 7c0c7f2e2b..6f8f0bae12 100644
--- a/packages/dashboard/src/locales/en.json
+++ b/packages/dashboard/src/locales/en.json
@@ -3249,6 +3249,9 @@
"unavailable": {
"title": "Reports are unavailable for your role",
"description": "Ask an administrator for the “Reports” permission (with access to orders) to see sales figures here."
+ },
+ "currency": {
+ "search": "Search currencies"
}
},
"orders": {
diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.json
index 52cb8e4d37..a0dc442ee6 100644
--- a/packages/dashboard/src/locales/fr.json
+++ b/packages/dashboard/src/locales/fr.json
@@ -3249,6 +3249,9 @@
"unavailable": {
"title": "Les rapports ne sont pas disponibles pour votre rôle",
"description": "Demandez à un administrateur la permission « Rapports » (avec accès aux commandes) pour voir les chiffres de vente ici."
+ },
+ "currency": {
+ "search": "Rechercher une devise"
}
},
"orders": {
diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.json
index de949aa340..5736e365f8 100644
--- a/packages/dashboard/src/locales/pl.json
+++ b/packages/dashboard/src/locales/pl.json
@@ -3285,6 +3285,9 @@
"unavailable": {
"title": "Raporty są niedostępne dla Twojej roli",
"description": "Poproś administratora o uprawnienie „Raporty” (z dostępem do zamówień), aby zobaczyć tutaj dane sprzedaży."
+ },
+ "currency": {
+ "search": "Szukaj walut"
}
},
"orders": {
diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.json
index 2b283054ef..c8eb204984 100644
--- a/packages/dashboard/src/locales/zh-CN.json
+++ b/packages/dashboard/src/locales/zh-CN.json
@@ -3239,6 +3239,9 @@
"unavailable": {
"title": "您的角色无法查看报表",
"description": "请管理员授予“报表”权限(并可访问订单),以在此查看销售数据。"
+ },
+ "currency": {
+ "search": "搜索货币"
}
},
"orders": {
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx
index f868976e10..d1a15a3b5e 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/index.tsx
@@ -1,6 +1,13 @@
import type { DashboardCounter, DashboardCounters, ReportingQuery } from '@spree/admin-sdk'
import { SpreeError } from '@spree/admin-sdk'
-import { Can, resolveDatePreset, Subject, usePermissions, useStore } from '@spree/dashboard-core'
+import {
+ Can,
+ CurrencySelect,
+ resolveDatePreset,
+ Subject,
+ usePermissions,
+ useStore,
+} from '@spree/dashboard-core'
import {
Button,
Card,
@@ -75,7 +82,7 @@ const CHART_METRICS = ['total_sales', 'orders', 'average_order_value', 'units_so
function DashboardPage() {
const { t } = useTranslation()
const { permissions } = usePermissions()
- const { timezone } = useStore()
+ const { timezone, currencies, defaultCurrency } = useStore()
// Seeded on the store's calendar, not the browser's, and sent as bare dates
// so the server widens both edges to the store's whole day.
const [dateRange, setDateRange] = useState<DateRange>(() => {
@@ -83,6 +90,10 @@ function DashboardPage() {
return { from: parseISO(preset.from as string), to: parseISO(preset.to as string) }
})
const [channelId, setChannelId] = useState<string>(ALL_CHANNELS)
+ // Undefined until the merchant picks one, so the store's default applies as
+ // soon as it loads rather than being frozen at the empty first render.
+ const [pickedCurrency, setPickedCurrency] = useState<string>()
+ const currency = pickedCurrency ?? defaultCurrency
// Mirror the server's member-level authorization (Query#required_subjects):
// widgets whose dimensions the role cannot read are hidden instead of
@@ -96,11 +107,15 @@ function DashboardPage() {
const channelParam = channelId === ALL_CHANNELS ? undefined : channelId
// Shared by every widget query — the switcher and date range scope the whole screen.
- const scope: Pick<ReportingQuery, 'time_range' | 'filters'> = {
+ const scope: Pick<ReportingQuery, 'time_range' | 'filters' | 'currency'> = {
time_range: {
since: format(dateRange.from, 'yyyy-MM-dd'),
until: format(dateRange.to, 'yyyy-MM-dd'),
},
+ // Money metrics are summed in one currency, never converted — a store
+ // selling in several would otherwise read only its default one, with
+ // nothing on screen saying so.
+ ...(currency ? { currency } : {}),
...(channelParam
? { filters: [{ dimension: 'channel', op: 'eq' as const, value: channelParam }] }
: {}),
@@ -180,6 +195,16 @@ function DashboardPage() {
value={channelId}
onChange={setChannelId}
/>
+ {/* Only worth a control when there is a choice to make — a
+ single-currency store gets no extra chrome. */}
+ {currencies.length > 1 && (
+ <CurrencySelect
+ id="home-currency"
+ value={currency}
+ onChange={setPickedCurrency}
+ searchPlaceholder={t('admin.pages.home.currency.search')}
+ />
+ )}
<DateRangePicker value={dateRange} onChange={setDateRange} />
</div>
</div>