Skip to content

Commit

Permalink
Budgets slice
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-jeremy committed Jan 8, 2025
1 parent fb42bd2 commit 147278b
Show file tree
Hide file tree
Showing 34 changed files with 674 additions and 588 deletions.
5 changes: 2 additions & 3 deletions packages/desktop-client/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { BrowserRouter } from 'react-router-dom';

import {
addNotification,
closeBudget,
loadBudget,
loadGlobalPrefs,
signOut,
sync,
Expand Down Expand Up @@ -44,6 +42,7 @@ import { Modals } from './Modals';
import { ResponsiveProvider } from './responsive/ResponsiveProvider';
import { SidebarProvider } from './sidebar/SidebarProvider';
import { UpdateNotification } from './UpdateNotification';
import { closeBudget, loadBudget } from 'loot-core/client/budgets/budgetsSlice';

function AppInner() {
const [budgetId] = useMetadataPref('id');
Expand Down Expand Up @@ -91,7 +90,7 @@ function AppInner() {
);
const budgetId = await send('get-last-opened-backup');
if (budgetId) {
await dispatch(loadBudget(budgetId));
await dispatch(loadBudget({ id: budgetId }));

// Check to see if this file has been remotely deleted (but
// don't block on this in case they are offline or something)
Expand Down
3 changes: 2 additions & 1 deletion packages/desktop-client/src/components/LoggedInUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect, useRef, type CSSProperties } from 'react';
import { Trans, useTranslation } from 'react-i18next';
import { useLocation } from 'react-router-dom';

import { closeBudget, getUserData, signOut } from 'loot-core/client/actions';
import { getUserData, signOut } from 'loot-core/client/actions';
import { type RemoteFile, type SyncedLocalFile } from 'loot-core/types/file';
import { type TransObjectLiteral } from 'loot-core/types/util';

Expand All @@ -20,6 +20,7 @@ import { Text } from './common/Text';
import { View } from './common/View';
import { PrivacyFilter } from './PrivacyFilter';
import { useMultiuserEnabled, useServerURL } from './ServerContext';
import { closeBudget } from 'loot-core/client/budgets/budgetsSlice';

type LoggedInUserProps = {
hideIfNoServer?: boolean;
Expand Down
15 changes: 5 additions & 10 deletions packages/desktop-client/src/components/manager/BudgetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ import React, {
import { Trans, useTranslation } from 'react-i18next';

import {
closeAndDownloadBudget,
closeAndLoadBudget,
createBudget,
downloadBudget,
getUserData,
loadAllFiles,
loadBudget,
pushModal,
} from 'loot-core/client/actions';
import {
Expand Down Expand Up @@ -53,6 +47,7 @@ import { Tooltip } from '../common/Tooltip';
import { View } from '../common/View';
import { useResponsive } from '../responsive/ResponsiveProvider';
import { useMultiuserEnabled } from '../ServerContext';
import { closeAndDownloadBudget, closeAndLoadBudget, createBudget, downloadBudget, loadAllFiles, loadBudget } from 'loot-core/client/budgets/budgetsSlice';

function getFileDescription(file: File, t: (key: string) => string) {
if (file.state === 'unknown') {
Expand Down Expand Up @@ -559,14 +554,14 @@ export function BudgetList({ showHeader = true, quickSwitchMode = false }) {

if (!id) {
if (isRemoteFile) {
await dispatch(downloadBudget(file.cloudFileId));
await dispatch(downloadBudget({ cloudFileId: file.cloudFileId }));
} else {
await dispatch(loadBudget(file.id));
await dispatch(loadBudget({ id: file.id }));
}
} else if (!isRemoteFile && file.id !== id) {
await dispatch(closeAndLoadBudget(file.id));
await dispatch(closeAndLoadBudget({ fileId: file.id }));
} else if (isRemoteFile) {
await dispatch(closeAndDownloadBudget(file.cloudFileId));
await dispatch(closeAndDownloadBudget({ cloudFileId: file.cloudFileId }));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React, { useState, useEffect, useCallback } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { createBudget, loggedIn, signOut } from 'loot-core/client/actions';
import { loggedIn, signOut } from 'loot-core/client/actions';
import {
isNonProductionEnvironment,
isElectron,
Expand All @@ -20,6 +20,7 @@ import { View } from '../common/View';
import { useServerURL, useSetServerURL } from '../ServerContext';

import { Title } from './subscribe/common';
import { createBudget } from 'loot-core/client/budgets/budgetsSlice';

export function ConfigServer() {
const { t } = useTranslation();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { createBudget, pushModal } from 'loot-core/client/actions';
import { pushModal } from 'loot-core/client/actions';

import { useAppDispatch } from '../../redux';
import { styles, theme } from '../../style';
Expand All @@ -10,6 +10,7 @@ import { Link } from '../common/Link';
import { Paragraph } from '../common/Paragraph';
import { Text } from '../common/Text';
import { View } from '../common/View';
import { createBudget } from 'loot-core/client/budgets/budgetsSlice';

export function WelcomeScreen() {
const { t } = useTranslation();
Expand Down Expand Up @@ -95,7 +96,7 @@ export function WelcomeScreen() {
<Button
variant="primary"
autoFocus
onPress={() => dispatch(createBudget())}
onPress={() => dispatch(createBudget({}))}
>
{t('Start fresh')}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import React, { useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { createBudget } from 'loot-core/src/client/actions/budgets';
import { send } from 'loot-core/src/platform/client/fetch';

import { useNavigate } from '../../../hooks/useNavigate';
Expand All @@ -17,6 +16,7 @@ import { useRefreshLoginMethods } from '../../ServerContext';

import { useBootstrapped, Title } from './common';
import { ConfirmPasswordForm } from './ConfirmPasswordForm';
import { createBudget } from 'loot-core/client/budgets/budgetsSlice';

export function Bootstrap() {
const { t } = useTranslation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useTranslation, Trans } from 'react-i18next';

import { css } from '@emotion/css';

import { loadAllFiles, loadGlobalPrefs, sync } from 'loot-core/client/actions';
import { loadGlobalPrefs, sync } from 'loot-core/client/actions';
import { send } from 'loot-core/src/platform/client/fetch';
import { getCreateKeyError } from 'loot-core/src/shared/errors';

Expand All @@ -25,6 +25,7 @@ import { Paragraph } from '../common/Paragraph';
import { Text } from '../common/Text';
import { View } from '../common/View';
import { useResponsive } from '../responsive/ResponsiveProvider';
import { loadAllFiles } from 'loot-core/client/budgets/budgetsSlice';

type CreateEncryptionKeyModalProps = {
options: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { closeBudget, popModal } from 'loot-core/client/actions';
import { popModal } from 'loot-core/client/actions';
import { send } from 'loot-core/platform/client/fetch';
import * as asyncStorage from 'loot-core/platform/server/asyncStorage';
import { getOpenIdErrors } from 'loot-core/shared/errors';
Expand All @@ -16,6 +16,7 @@ import { Modal, ModalCloseButton, ModalHeader } from '../common/Modal';
import { View } from '../common/View';
import { OpenIdForm } from '../manager/subscribe/OpenIdForm';
import { useRefreshLoginMethods } from '../ServerContext';
import { closeBudget } from 'loot-core/client/budgets/budgetsSlice';

type OpenIDEnableModalProps = {
onSave?: () => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { closeBudget } from 'loot-core/client/actions';

import { useAppDispatch } from '../../redux';
import { Button } from '../common/Button2';
import { Link } from '../common/Link';
import { Modal, ModalHeader, ModalTitle } from '../common/Modal';
import { Paragraph } from '../common/Paragraph';
import { Text } from '../common/Text';
import { View } from '../common/View';
import { closeBudget } from 'loot-core/client/budgets/budgetsSlice';

export function OutOfSyncMigrationsModal() {
const dispatch = useAppDispatch();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { closeBudget, popModal } from 'loot-core/client/actions';
import { popModal } from 'loot-core/client/actions';
import { send } from 'loot-core/platform/client/fetch';
import * as asyncStorage from 'loot-core/src/platform/server/asyncStorage';

Expand All @@ -22,6 +22,7 @@ import {
useMultiuserEnabled,
useRefreshLoginMethods,
} from '../ServerContext';
import { closeBudget } from 'loot-core/client/budgets/budgetsSlice';

type PasswordEnableModalProps = {
onSave?: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Trans, useTranslation } from 'react-i18next';

import {
addNotification,
closeAndLoadBudget,
popModal,
} from 'loot-core/client/actions';
import { send } from 'loot-core/platform/client/fetch';
Expand All @@ -22,6 +21,7 @@ import { Stack } from '../common/Stack';
import { Text } from '../common/Text';
import { View } from '../common/View';
import { FormField, FormLabel } from '../forms';
import { closeAndLoadBudget } from 'loot-core/client/budgets/budgetsSlice';

type TransferOwnershipProps = {
onSave?: () => void;
Expand Down Expand Up @@ -186,7 +186,7 @@ export function TransferOwnership({
try {
await onSave();
await dispatch(
closeAndLoadBudget((currentFile as Budget).id),
closeAndLoadBudget({ fileId: (currentFile as Budget).id }),
);
close();
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { deleteBudget } from 'loot-core/client/actions';
import { type File } from 'loot-core/src/types/file';

import { useAppDispatch } from '../../../redux';
Expand All @@ -10,6 +9,7 @@ import { ButtonWithLoading } from '../../common/Button2';
import { Modal, ModalCloseButton, ModalHeader } from '../../common/Modal';
import { Text } from '../../common/Text';
import { View } from '../../common/View';
import { deleteBudget } from 'loot-core/client/budgets/budgetsSlice';

type DeleteFileProps = {
file: File;
Expand Down Expand Up @@ -70,10 +70,10 @@ export function DeleteFileModal({ file }: DeleteFileProps) {
onPress={async () => {
setLoadingState('cloud');
await dispatch(
deleteBudget(
'id' in file ? file.id : undefined,
file.cloudFileId,
),
deleteBudget({
id: 'id' in file ? file.id : undefined,
cloudFileId: file.cloudFileId,
}),
);
setLoadingState(null);

Expand Down Expand Up @@ -136,7 +136,7 @@ export function DeleteFileModal({ file }: DeleteFileProps) {
}}
onPress={async () => {
setLoadingState('local');
await dispatch(deleteBudget(file.id));
await dispatch(deleteBudget({ id: file.id }));
setLoadingState(null);

close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React, { useEffect, useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import {
addNotification,
duplicateBudget,
uniqueBudgetName,
validateBudgetName,
} from 'loot-core/client/actions';
import { addNotification } from 'loot-core/client/actions';
import { type File } from 'loot-core/src/types/file';

import { useAppDispatch } from '../../../redux';
Expand All @@ -24,6 +19,8 @@ import {
} from '../../common/Modal';
import { Text } from '../../common/Text';
import { View } from '../../common/View';
import { send } from 'loot-core/platform/client/fetch';
import { duplicateBudget } from 'loot-core/client/budgets/budgetsSlice';

type DuplicateFileProps = {
file: File;
Expand Down Expand Up @@ -241,3 +238,14 @@ export function DuplicateFileModal({
</Modal>
);
}

function validateBudgetName(name: string): {
valid: boolean;
message?: string;
} {
return send('validate-budget-name', { name });
}

function uniqueBudgetName(name: string): string {
return send('unique-budget-name', { name });
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { loadAllFiles, pushModal } from 'loot-core/client/actions';
import { pushModal } from 'loot-core/client/actions';

import { useGlobalPref } from '../../../hooks/useGlobalPref';
import { SvgPencil1 } from '../../../icons/v2';
Expand All @@ -11,6 +11,7 @@ import { Button } from '../../common/Button2';
import { Modal, ModalCloseButton, ModalHeader } from '../../common/Modal';
import { Text } from '../../common/Text';
import { View } from '../../common/View';
import { loadAllFiles } from 'loot-core/client/budgets/budgetsSlice';

function FileLocationSettings() {
const [documentDir, _setDocumentDirPref] = useGlobalPref('documentDir');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import React, { useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { importBudget } from 'loot-core/src/client/actions/budgets';

import { useNavigate } from '../../../hooks/useNavigate';
import { useAppDispatch } from '../../../redux';
import { styles, theme } from '../../../style';
Expand All @@ -12,6 +10,7 @@ import { ButtonWithLoading } from '../../common/Button2';
import { Modal, ModalCloseButton, ModalHeader } from '../../common/Modal';
import { Paragraph } from '../../common/Paragraph';
import { View } from '../../common/View';
import { importBudget } from 'loot-core/client/budgets/budgetsSlice';

function getErrorMessage(error: string): string {
switch (error) {
Expand Down Expand Up @@ -46,7 +45,7 @@ export function ImportActualModal() {
setImporting(true);
setError(null);
try {
await dispatch(importBudget(res[0], 'actual'));
await dispatch(importBudget({ filepath: res[0], type: 'actual' }));
navigate('/budget');
} catch (err) {
setError(err.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import React, { useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { importBudget } from 'loot-core/src/client/actions/budgets';

import { useNavigate } from '../../../hooks/useNavigate';
import { useAppDispatch } from '../../../redux';
import { styles, theme } from '../../../style';
Expand All @@ -12,6 +10,7 @@ import { ButtonWithLoading } from '../../common/Button2';
import { Modal, ModalCloseButton, ModalHeader } from '../../common/Modal';
import { Paragraph } from '../../common/Paragraph';
import { View } from '../../common/View';
import { importBudget } from 'loot-core/client/budgets/budgetsSlice';

function getErrorMessage(error: string): string {
switch (error) {
Expand All @@ -38,7 +37,7 @@ export function ImportYNAB4Modal() {
setImporting(true);
setError(null);
try {
await dispatch(importBudget(res[0], 'ynab4'));
await dispatch(importBudget({ filepath: res[0], type: 'ynab4' }));
navigate('/budget');
} catch (err) {
setError(err.message);
Expand Down
Loading

0 comments on commit 147278b

Please sign in to comment.