Skip to content

Commit b241003

Browse files
committed
feat(new tool): Currency Converter
Fix part of CorentinTh#571
1 parent 318fb6e commit b241003

File tree

9 files changed

+242
-13
lines changed

9 files changed

+242
-13
lines changed

components.d.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ declare module '@vue/runtime-core' {
6464
'CTextCopyable.demo': typeof import('./src/ui/c-text-copyable/c-text-copyable.demo.vue')['default']
6565
CTooltip: typeof import('./src/ui/c-tooltip/c-tooltip.vue')['default']
6666
'CTooltip.demo': typeof import('./src/ui/c-tooltip/c-tooltip.demo.vue')['default']
67+
CurrencyConverter: typeof import('./src/tools/currency-converter/currency-converter.vue')['default']
6768
DateTimeConverter: typeof import('./src/tools/date-time-converter/date-time-converter.vue')['default']
6869
'DemoHome.page': typeof import('./src/ui/demo/demo-home.page.vue')['default']
6970
DemoWrapper: typeof import('./src/ui/demo/demo-wrapper.vue')['default']
@@ -129,11 +130,12 @@ declare module '@vue/runtime-core' {
129130
MetaTagGenerator: typeof import('./src/tools/meta-tag-generator/meta-tag-generator.vue')['default']
130131
MimeTypes: typeof import('./src/tools/mime-types/mime-types.vue')['default']
131132
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
132-
NCode: typeof import('naive-ui')['NCode']
133133
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
134134
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
135+
NDatePicker: typeof import('naive-ui')['NDatePicker']
136+
NDivider: typeof import('naive-ui')['NDivider']
137+
NDynamicInput: typeof import('naive-ui')['NDynamicInput']
135138
NEllipsis: typeof import('naive-ui')['NEllipsis']
136-
NForm: typeof import('naive-ui')['NForm']
137139
NFormItem: typeof import('naive-ui')['NFormItem']
138140
NH1: typeof import('naive-ui')['NH1']
139141
NH3: typeof import('naive-ui')['NH3']
@@ -142,9 +144,9 @@ declare module '@vue/runtime-core' {
142144
NLayout: typeof import('naive-ui')['NLayout']
143145
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
144146
NMenu: typeof import('naive-ui')['NMenu']
145-
NScrollbar: typeof import('naive-ui')['NScrollbar']
146-
NSlider: typeof import('naive-ui')['NSlider']
147-
NSwitch: typeof import('naive-ui')['NSwitch']
147+
NP: typeof import('naive-ui')['NP']
148+
NSelect: typeof import('naive-ui')['NSelect']
149+
NTable: typeof import('naive-ui')['NTable']
148150
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
149151
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
150152
PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default']

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
"cron-validator": "^1.3.1",
5656
"cronstrue": "^2.26.0",
5757
"crypto-js": "^4.1.1",
58+
"currency-codes-ts": "^3.0.0",
59+
"currency-exchanger-js": "^1.0.4",
5860
"date-fns": "^2.29.3",
5961
"dompurify": "^3.0.6",
6062
"email-normalizer": "^1.0.0",

pnpm-lock.yaml

Lines changed: 63 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/composable/queryParams.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useRouteQuery } from '@vueuse/router';
22
import { computed } from 'vue';
3+
import { useStorage } from '@vueuse/core';
34

4-
export { useQueryParam };
5+
export { useQueryParam, useQueryParamOrStorage };
56

67
const transformers = {
78
number: {
@@ -16,6 +17,12 @@ const transformers = {
1617
fromQuery: (value: string) => value.toLowerCase() === 'true',
1718
toQuery: (value: boolean) => (value ? 'true' : 'false'),
1819
},
20+
object: {
21+
fromQuery: (value: string) => {
22+
return JSON.parse(value);
23+
},
24+
toQuery: (value: object) => JSON.stringify(value),
25+
},
1926
};
2027

2128
function useQueryParam<T>({ name, defaultValue }: { name: string; defaultValue: T }) {
@@ -33,3 +40,27 @@ function useQueryParam<T>({ name, defaultValue }: { name: string; defaultValue:
3340
},
3441
});
3542
}
43+
44+
function useQueryParamOrStorage<T>({ name, storageName, defaultValue }: { name: string; storageName: string; defaultValue: T }) {
45+
const type = typeof defaultValue;
46+
const transformer = transformers[type as keyof typeof transformers] ?? transformers.string;
47+
48+
const storageRef = useStorage(storageName, defaultValue);
49+
const proxyDefaultValue = transformer.toQuery(defaultValue as never);
50+
const proxy = useRouteQuery(name, proxyDefaultValue);
51+
52+
const r = ref(defaultValue);
53+
54+
watch(r,
55+
(value) => {
56+
proxy.value = transformer.toQuery(value as never);
57+
storageRef.value = value as never;
58+
},
59+
{ deep: true });
60+
61+
r.value = (proxy.value && proxy.value !== proxyDefaultValue
62+
? transformer.fromQuery(proxy.value) as unknown as T
63+
: storageRef.value as T) as never;
64+
65+
return r;
66+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<script setup lang="ts">
2+
import { code, countries, country } from 'currency-codes-ts';
3+
import converter from 'currency-exchanger-js';
4+
import moneysData from './moneys.json';
5+
import { useQueryParamOrStorage } from '@/composable/queryParams';
6+
7+
const allCurrencies = Object.entries(moneysData).map(([k, v]) => ({ value: k, label: v || k }));
8+
const otherCurrencies = useQueryParamOrStorage<{ name: string }[]>({ name: 'to', storageName: 'currency-conv:others', defaultValue: [{ name: 'usd' }] });
9+
const currentCurrency = useQueryParamOrStorage<string>({ name: 'from', storageName: 'currency-conv:cur', defaultValue: 'eur' });
10+
const amount = ref(1);
11+
const currentDatetime = ref(Date.now());
12+
13+
const convertedCurrencies = computedAsync<Record<string, number>>(async () => {
14+
const currentCurrencyValue = currentCurrency.value;
15+
const currentDatetimeValue = currentDatetime.value;
16+
const amountValue = amount.value;
17+
const otherCurrenciesValues = otherCurrencies.value;
18+
19+
let result = {};
20+
for (const targetCurrency of otherCurrenciesValues) {
21+
const value = await converter.convertOnDate(amountValue, currentCurrencyValue, targetCurrency.name, new Date(currentDatetimeValue));
22+
result = { ...result, [targetCurrency.name]: value };
23+
}
24+
return result;
25+
});
26+
27+
const countryToCurrenciesInput = ref('France');
28+
const allCountries = countries();
29+
const countryToCurrenciesOutput = computed(() => country(countryToCurrenciesInput.value));
30+
31+
const currencyToCountriesInput = ref('eur');
32+
const currencyToCountriesOutput = computed(() => code(currencyToCountriesInput.value));
33+
</script>
34+
35+
<template>
36+
<div>
37+
<c-card title="Currency Converter" mb-2>
38+
<c-select
39+
v-model:value="currentCurrency"
40+
label="From"
41+
label-position="left"
42+
searchable
43+
:options="allCurrencies"
44+
mb-2
45+
/>
46+
<n-form-item label="Amount:" label-placement="left" mb-2>
47+
<n-input-number v-model:value="amount" :min="0" />
48+
</n-form-item>
49+
50+
<n-form-item label="For Date:" label-placement="left" mb-2>
51+
<n-date-picker
52+
v-model:value="currentDatetime"
53+
type="date"
54+
/>
55+
</n-form-item>
56+
57+
<c-card title="Converted currencies">
58+
<n-dynamic-input
59+
v-model:value="otherCurrencies"
60+
show-sort-button
61+
:on-create="() => ({ name: 'eur' })"
62+
>
63+
<template #default="{ value }">
64+
<div flex flex-wrap items-center gap-1>
65+
<n-select
66+
v-model:value="value.name"
67+
filterable
68+
placeholder="Please select a currency"
69+
:options="allCurrencies"
70+
w-full
71+
/>
72+
<input-copyable readonly :value="convertedCurrencies ? convertedCurrencies[value.name] : 0" />
73+
</div>
74+
</template>
75+
</n-dynamic-input>
76+
</c-card>
77+
</c-card>
78+
79+
<c-card title="Country to Currencies" mb-2>
80+
<c-select
81+
v-model:value="countryToCurrenciesInput"
82+
label="Country"
83+
label-position="left"
84+
searchable
85+
:options="allCountries"
86+
/>
87+
88+
<n-divider />
89+
90+
<ul>
91+
<li v-for="(currency, ix) in countryToCurrenciesOutput" :key="ix">
92+
{{ currency.currency }} [{{ currency.code }}/{{ currency.number }} - {{ currency.digits }}digits] (also in: {{ currency.countries?.join(', ') }})
93+
</li>
94+
</ul>
95+
</c-card>
96+
97+
<c-card title="Currencies to Countries" mb-2>
98+
<c-select
99+
v-model:value="currencyToCountriesInput"
100+
label="Currency"
101+
label-position="left"
102+
searchable
103+
:options="allCurrencies"
104+
/>
105+
106+
<n-divider />
107+
108+
<n-p v-if="currencyToCountriesOutput">
109+
{{ currencyToCountriesOutput.currency }} [{{ currencyToCountriesOutput.code }}/{{ currencyToCountriesOutput.number }} - {{ currencyToCountriesOutput.digits }}digits]
110+
</n-p>
111+
112+
<ul v-if="currencyToCountriesOutput">
113+
<li v-for="(countryName, ix) in currencyToCountriesOutput.countries" :key="ix">
114+
{{ countryName }}
115+
</li>
116+
</ul>
117+
</c-card>
118+
</div>
119+
</template>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module 'currency-exchanger-js'{
2+
export function convertOnDate(value: number,fromCurrency: string,toCurrency: string,inputDate: Date): number;
3+
export function convert(value: number,fromCurrency: string,toCurrency: string): number;
4+
}

src/tools/currency-converter/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Currency } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Currency Converter',
6+
path: '/currency-converter',
7+
description: 'Convert currency values using ExchangeRate-API',
8+
keywords: ['currency', 'converter'],
9+
component: () => import('./currency-converter.vue'),
10+
icon: Currency,
11+
createdAt: new Date('2024-08-15'),
12+
});

0 commit comments

Comments
 (0)