Skip to content

Commit

Permalink
Add currencies setting to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Dec 11, 2024
1 parent 0953d3e commit 4959914
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 113 deletions.
1 change: 1 addition & 0 deletions frontend/.env.docker.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ VITE_BC_API_HOST=http://localhost:4002
VITE_BC_RECAPTCHA_ENABLED=false
VITE_BC_RECAPTCHA_SITE_KEY=GOOGLE_RECAPTCHA_SITE_KEY
VITE_BC_DEFAULT_LANGUAGE=en
VITE_BC_BASE_CURRENCY=USD
VITE_BC_PAGE_SIZE=30
VITE_BC_CARS_PAGE_SIZE=15
VITE_BC_BOOKINGS_PAGE_SIZE=20
Expand Down
1 change: 1 addition & 0 deletions frontend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ VITE_BC_API_HOST=http://localhost:4002
VITE_BC_RECAPTCHA_ENABLED=false
VITE_BC_RECAPTCHA_SITE_KEY=GOOGLE_RECAPTCHA_SITE_KEY
VITE_BC_DEFAULT_LANGUAGE=en
VITE_BC_BASE_CURRENCY=USD
VITE_BC_PAGE_SIZE=30
VITE_BC_CARS_PAGE_SIZE=15
VITE_BC_BOOKINGS_PAGE_SIZE=20
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/common/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,11 @@ export const downloadURI = (uri: string, name: string = '') => {
link.click()
link.remove()
}

/**
* Return currency symbol.
*
* @param {string} code
* @returns {string|undefined}
*/
export const getCurrencySymbol = (code: string) => env._CURRENCIES.find((c) => c.code === code)?.symbol
36 changes: 31 additions & 5 deletions frontend/src/config/env.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as bookcarsTypes from ':bookcars-types'
import Const from './const'

//
// ISO 639-1 language codes and their labels
// https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
//
const LANGUAGES = [
type Language = { code: string, label: string }

/**
* ISO 639-1 language codes and their labels
* https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
*
* @type {Language[]}
*/
const LANGUAGES: Language[] = [
{
code: 'en',
label: 'English',
Expand All @@ -20,6 +24,25 @@ const LANGUAGES = [
},
]

type Currency = { code: string, symbol: string }

/**
* ISO 4217 currency codes and their symbols
* https://docs.stripe.com/currencies
*
* @type {Currency[]}
*/
const CURRENCIES: Currency[] = [
{
code: 'USD',
symbol: '$',
},
{
code: 'EUR',
symbol: '€',
}
]

const env = {
isMobile: window.innerWidth <= 960,
isProduction: import.meta.env.VITE_NODE_ENV === 'production',
Expand All @@ -30,6 +53,9 @@ const env = {
LANGUAGES: LANGUAGES.map((l) => l.code),
_LANGUAGES: LANGUAGES,
DEFAULT_LANGUAGE: String(import.meta.env.VITE_BC_DEFAULT_LANGUAGE || 'en'),
CURRENCIES: CURRENCIES.map((c) => c.code),
_CURRENCIES: CURRENCIES,
BASE_CURRENCY: String(import.meta.env.VITE_BC_BASE_CURRENCY || 'USD'),
PAGE_SIZE: Number.parseInt(String(import.meta.env.VITE_BC_PAGE_SIZE), 10) || 30,
CARS_PAGE_SIZE: Number.parseInt(String(import.meta.env.VITE_BC_CARS_PAGE_SIZE), 10) || 15,
BOOKINGS_PAGE_SIZE: Number.parseInt(String(import.meta.env.VITE_BC_BOOKINGS_PAGE_SIZE), 10) || 20,
Expand Down
24 changes: 24 additions & 0 deletions frontend/src/services/StripeService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as bookcarsTypes from ':bookcars-types'
import * as bookcarsHelper from ':bookcars-helper'
import axiosInstance from './axiosInstance'
import env from '@/config/env.config'

/**
* Create Checkout Session.
Expand Down Expand Up @@ -42,3 +44,25 @@ export const createPaymentIntent = (payload: bookcarsTypes.CreatePaymentPayload)
payload
)
.then((res) => res.data)

/**
* Set currency.
*
* @param {string} currency
*/
export const setCurrency = (currency: string) => {
localStorage.setItem('bc-fe-currency', currency)
}

/**
* Get currency.
*
* @returns {string}
*/
export const getCurrency = () => {
const currency = localStorage.getItem('bc-fe-currency')
if (currency && bookcarsHelper.checkCurrency(currency)) {
return currency
}
return env.BASE_CURRENCY
}
10 changes: 9 additions & 1 deletion packages/bookcars-helper/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as bookcarsTypes from ':bookcars-types'
import CurrencyConverter, { CurrencyCode } from ':currency-converter'
import CurrencyConverter, { CurrencyCode, currencies } from ':currency-converter'

/**
* Format a number.
Expand Down Expand Up @@ -352,6 +352,14 @@ export const convertPrice = async (amount: number, from: CurrencyCode, to: Curre
return res
}

/**
* Check if currency is supported.
*
* @param {string} currency
* @returns {boolean}
*/
export const checkCurrency = (currency: string) => currencies.findIndex((c) => c === currency) > -1

/**
* Check whether language is french
*
Expand Down
Loading

0 comments on commit 4959914

Please sign in to comment.