Skip to content

Commit

Permalink
Copying of budgets and limiting future month navigation.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoBernardino committed Dec 4, 2020
1 parent 9e942bb commit b11015e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
35 changes: 33 additions & 2 deletions components/Panels/All.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { RxDatabase } from 'rxdb';
import LogoutLink from 'modules/auth/LogoutLink';
import { Loading } from 'components';
import { getUserInfo, showNotification } from 'lib/utils';
import { initializeDb, fetchBudgets, fetchExpenses } from 'lib/data-utils';
import {
initializeDb,
fetchBudgets,
fetchExpenses,
copyBudgets,
} from 'lib/data-utils';
import * as T from 'lib/types';

import Navigation from './Navigation';
Expand Down Expand Up @@ -41,7 +46,7 @@ const All = () => {
const [expenses, setExpenses] = useState<T.Expense[]>([]);
const db = useRef<RxDatabase>(null);

const reloadData = async () => {
const reloadData = async ({ isComingFromEmptyState = false } = {}) => {
setIsLoading(true);

const fetchedBudgets = await fetchBudgets(db.current, monthInView);
Expand All @@ -50,6 +55,25 @@ const All = () => {
const fetchedExpenses = await fetchExpenses(db.current, monthInView);
setExpenses(fetchedExpenses);

// If this is for the current or next month and there are no budgets, create budgets based on the previous/current month.
if (fetchedBudgets.length === 0 && !isComingFromEmptyState) {
const currentMonth = moment().format('YYYY-MM');
const nextMonth = moment().add(1, 'month').format('YYYY-MM');
const previousMonth = moment().subtract(1, 'month').format('YYYY-MM');

if (monthInView === nextMonth) {
await copyBudgets(db.current, currentMonth, nextMonth);
await reloadData({ isComingFromEmptyState: true });
return;
}

if (monthInView === currentMonth) {
await copyBudgets(db.current, previousMonth, currentMonth);
await reloadData({ isComingFromEmptyState: true });
return;
}
}

showNotification(
'Data is continuously synchronizing in the background. Navigate between months to see the latest data.',
);
Expand All @@ -58,6 +82,13 @@ const All = () => {
};

const changeMonthInView = async (month: string) => {
const nextMonth = moment().add(1, 'month').format('YYYY-MM');

if (month > nextMonth) {
showNotification('Cannot travel further into the future!', 'error');
return;
}

setIsLoading(true);

setMonthInView(month);
Expand Down
9 changes: 9 additions & 0 deletions components/Panels/Budgets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ interface BudgetsToShow extends T.Budget {
expensesCost: number;
}

// user-agent sniffing sucks, but I couldn't figure out why this problem only happens on Safari (macOS and iOS)
const safariFix =
typeof navigator !== 'undefined' &&
navigator.userAgent &&
navigator.userAgent.includes('Safari')
? 'max-block-size: 100%;'
: '';

const Container = styled.section`
display: flex;
flex-direction: column;
flex: 1;
margin: 0 10px;
max-height: 80vh;
overflow: auto;
${safariFix}
`;

const AddButton = styled(Button)`
Expand Down
9 changes: 9 additions & 0 deletions components/Panels/Expenses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ import * as T from 'lib/types';

interface ExpensesProps extends T.PanelProps {}

// user-agent sniffing sucks, but I couldn't figure out why this problem only happens on Safari (macOS and iOS)
const safariFix =
typeof navigator !== 'undefined' &&
navigator.userAgent &&
navigator.userAgent.includes('Safari')
? 'max-block-size: 100%;'
: '';

const Container = styled.section`
display: flex;
flex-direction: column;
flex: 1;
max-height: 80vh;
overflow: auto;
${safariFix}
`;

const FiltersContainer = styled.section`
Expand Down
19 changes: 19 additions & 0 deletions lib/data-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,22 @@ export const importData = async (

return false;
};

export const copyBudgets = async (
db: RxDatabase,
originalMonth: string,
destinationMonth: string,
) => {
const originalBudgets = await fetchBudgets(db, originalMonth);
const destinationBudgets = originalBudgets.map((budget) => {
const newBudget: T.Budget = { ...budget };
newBudget.id = `${Date.now().toString()}:${Math.random()}`;
newBudget.month = destinationMonth;
delete newBudget._rev;
return newBudget;
});

if (destinationBudgets.length > 0) {
await db.budgets.bulkInsert(destinationBudgets);
}
};

0 comments on commit b11015e

Please sign in to comment.