Skip to content

Commit

Permalink
Adds sterling pound option and doesn't allow copying budgets until sy…
Browse files Browse the repository at this point in the history
…nchronizing is done.
  • Loading branch information
BrunoBernardino committed Apr 17, 2021
1 parent 0e2ef4f commit 17b7f22
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
12 changes: 9 additions & 3 deletions components/Panels/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ const HelpButton = styled(Button)`
align-self: center;
`;

const currencyLabels = ['$', '€'];
const currencyValues: T.Currency[] = ['USD', 'EUR'];
const currencyLabels = ['$', '€', '£'];
const currencyValues: T.Currency[] = ['USD', 'EUR', 'GBP'];

const Settings = ({
currentCurrency,
Expand Down Expand Up @@ -113,6 +113,10 @@ const Settings = ({
}
};

const selectedCurrencyIndex = currencyValues.findIndex(
(_currency) => currency === _currency,
);

return (
<>
<SettingsButton
Expand All @@ -130,7 +134,9 @@ const Settings = ({
<Label>Currency</Label>
<StyledSegmentedControl
values={currencyLabels}
selectedIndex={currency === 'USD' ? 0 : 1}
selectedIndex={
selectedCurrencyIndex === -1 ? 0 : selectedCurrencyIndex
}
onChange={(selectedSegmentIndex: number) => {
setCurrency(currencyValues[selectedSegmentIndex]);
saveCurrency(currencyValues[selectedSegmentIndex]);
Expand Down
2 changes: 1 addition & 1 deletion components/SegmentedControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const Segment = styled.button<SegmentProps>`
selected ? colors().primaryButtonBackground : 'transparent'};
padding: 10px;
border-radius: 5px;
min-width: 50%;
min-width: 33%;
color: ${({ selected }) =>
selected ? colors().primaryButtonText : 'inherit'};
font-size: ${fontSizes.button}px;
Expand Down
11 changes: 11 additions & 0 deletions lib/data-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ export const budgetSchema: RxJsonSchema<T.Budget> = {
required: ['name', 'month', 'value'],
};

const _hasFinishedFirstSync = {
budgets: false,
expenses: false,
};

export const initializeDb = async (syncToken: string) => {
if (!syncToken) {
return null;
Expand Down Expand Up @@ -143,6 +148,7 @@ export const initializeDb = async (syncToken: string) => {

budgetsSync.complete$.subscribe((completed) => {
console.log('budgetsSync.complete$', completed);
_hasFinishedFirstSync.budgets = true;
});

budgetsSync.change$.subscribe((docData) => {
Expand All @@ -167,6 +173,7 @@ export const initializeDb = async (syncToken: string) => {

expensesSync.complete$.subscribe((completed) => {
console.log('expensesSync.complete$', completed);
_hasFinishedFirstSync.expenses = true;
});

expensesSync.change$.subscribe((docData) => {
Expand Down Expand Up @@ -643,6 +650,10 @@ export const copyBudgets = async (
originalMonth: string,
destinationMonth: string,
) => {
// Don't copy anything until we're done with the first sync
if (!_hasFinishedFirstSync.expenses || !_hasFinishedFirstSync.budgets) {
return;
}
const originalBudgets = await fetchBudgets(db, originalMonth);
const destinationBudgets = originalBudgets.map((budget) => {
const newBudget: T.Budget = { ...budget };
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface PlainObject {
[key: string]: any;
}

export type Currency = 'USD' | 'EUR';
export type Currency = 'USD' | 'EUR' | 'GBP';

export interface AuthToken {
syncToken: string;
Expand Down
3 changes: 3 additions & 0 deletions lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ describe('lib/utils', () => {
{ currency: 'EUR', number: 900.999, expected: '€901' },
{ currency: 'EUR', number: 900.991, expected: '€900.99' },
{ currency: 'USD', number: 50.11, expected: '$50.11' },
{ currency: 'GBP', number: 900.999, expected: '£901' },
{ currency: 'GBP', number: 900.991, expected: '£900.99' },
{ currency: 'GBP', number: 50.11, expected: '£50.11' },
];

for (const test of tests) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "budgetzen-app",
"version": "1.0.0",
"build": "WxpKARxZ",
"build": "dkGscrNV",
"author": "Bruno Bernardino <me@brunobernardino.com>",
"license": "UNLICENSED",
"repository": {
Expand Down

0 comments on commit 17b7f22

Please sign in to comment.