-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change data structure (primarily for budget object)
- Loading branch information
1 parent
4e835d9
commit 7157ffc
Showing
10 changed files
with
116 additions
and
80 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { getCategoryFrom } from './categories' | ||
import { addToList, updateInObject } from '../helpers/data-store-helpers' | ||
import { getCurrentYearMonthString } from '../helpers/dates' | ||
import { getObjectFromStorage, saveToStorage } from './storage' | ||
import { get, writable } from 'svelte/store' | ||
|
||
const BUDGET = 'budget' | ||
|
||
const budgetStore = writable({}) | ||
export {budgetStore as budget} | ||
|
||
const addCategoryToBudget = (categoryUuid, budgeted) => { | ||
updateBudget(categoryUuid, { | ||
budgeted: budgeted, | ||
remaining: budgeted, | ||
refilled: getCurrentYearMonthString() | ||
}) | ||
} | ||
|
||
const isExistingCategory = uuid => get(budgetStore).hasOwnProperty(uuid) | ||
|
||
export const loadBudget = () => { | ||
budgetStore.set(getObjectFromStorage(BUDGET)) | ||
} | ||
|
||
const saveBudget = () => saveToStorage(BUDGET, get(budgetStore)) | ||
|
||
export const setBudgetedForCategory = (uuid, budgeted) => { | ||
const budget = get(budgetStore) | ||
if (isExistingCategory(uuid)) { | ||
updateBudgetedForExistingCategory(uuid, budgeted) | ||
} else { | ||
addCategoryToBudget(uuid, budgeted) | ||
} | ||
} | ||
|
||
export const sortBudgetByCategory = (budget, categories) => { | ||
let list = [] | ||
for (var uuid in budget) { | ||
if (budget.hasOwnProperty(uuid)) { | ||
let category = getCategoryFrom(uuid, categories) | ||
list.push({ | ||
budgeted: budget[uuid].budgeted, | ||
remaining: budget[uuid].remaining, | ||
name: category.name, | ||
uuid: category.uuid, | ||
}); | ||
} | ||
} | ||
return list.sort((a, b) => (a.name || '').localeCompare(b.name || '')); | ||
} | ||
|
||
export const updateBudget = (categoryUuid, changes) => { | ||
updateInObject(categoryUuid, changes, budgetStore) | ||
saveBudget() | ||
} | ||
|
||
const updateBudgetedForExistingCategory = (categoryUuid, budgeted) => { | ||
const budget = get(budgetStore) | ||
let categoryAmounts = budget[categoryUuid] | ||
let previousBudgeted = categoryAmounts.budgeted | ||
let previousRemaining = categoryAmounts.remaining | ||
let remaining = previousRemaining + (budgeted - previousBudgeted) | ||
updateBudget(categoryUuid, {budgeted, remaining}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters