Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Extract budget file related server handlers from main.ts to server/budgetfiles/app.ts #4547

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ export function DuplicateFileModal({
await dispatch(
duplicateBudget({
id: 'id' in file ? file.id : undefined,
cloudId:
sync === 'cloudSync' && 'cloudFileId' in file
? file.cloudFileId
: undefined,
oldName: file.name,
newName,
cloudSync: sync === 'cloudSync',
Expand Down
14 changes: 8 additions & 6 deletions packages/desktop-client/src/components/settings/Export.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ export function ExportBudget() {

const response = await send('export-budget');

if ('error' in response) {
if ('error' in response && response.error) {
setError(response.error);
setIsLoading(false);
console.log('Export error code:', response.error);
return;
}

window.Actual.saveFile(
response.data,
`${format(new Date(), 'yyyy-MM-dd')}-${budgetName}.zip`,
t('Export budget'),
);
if (response.data) {
window.Actual.saveFile(
response.data,
`${format(new Date(), 'yyyy-MM-dd')}-${budgetName}.zip`,
t('Export budget'),
);
}
setIsLoading(false);
}

Expand Down
34 changes: 25 additions & 9 deletions packages/loot-core/src/client/budgets/budgetsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ export const duplicateBudget = createAppAsyncThunk(
async (
{
id,
cloudId,
oldName,
newName,
managePage,
Expand All @@ -202,6 +201,10 @@ export const duplicateBudget = createAppAsyncThunk(
}: DuplicateBudgetPayload,
{ dispatch },
) => {
if (!id) {
throw new Error('Unable to duplicate a budget that is not local.');
}

try {
await dispatch(
setAppState({
Expand All @@ -214,7 +217,6 @@ export const duplicateBudget = createAppAsyncThunk(

await send('duplicate-budget', {
id,
cloudId,
newName,
cloudSync,
open: loadBudget,
Expand Down Expand Up @@ -313,14 +315,18 @@ export const downloadBudget = createAppAsyncThunk(
);

const { id, error } = await send('download-budget', {
fileId: cloudFileId,
replace,
cloudFileId,
});

if (error) {
if (error.reason === 'decrypt-failure') {
const opts = {
hasExistingKey: error.meta && error.meta.isMissingKey,
hasExistingKey: Boolean(
error.meta &&
typeof error.meta === 'object' &&
'isMissingKey' in error.meta &&
error.meta.isMissingKey,
),
cloudFileId,
onSuccess: () => {
dispatch(downloadBudget({ cloudFileId, replace }));
Expand All @@ -338,8 +344,16 @@ export const downloadBudget = createAppAsyncThunk(
'This file will be replaced. This probably happened because files were manually ' +
'moved around outside of Actual.',
{
id: error.meta.id,
name: error.meta.name,
id:
error.meta &&
typeof error.meta === 'object' &&
'id' in error.meta &&
error.meta.id,
name:
error.meta &&
typeof error.meta === 'object' &&
'name' in error.meta &&
error.meta.name,
},
),
);
Expand All @@ -353,15 +367,17 @@ export const downloadBudget = createAppAsyncThunk(
}
return null;
} else {
if (!id) {
throw new Error('No id returned from download.');
}
await Promise.all([
dispatch(loadGlobalPrefs()),
dispatch(loadAllFiles()),
dispatch(loadBudget({ id })),
]);
await dispatch(setAppState({ loadingText: null }));
return id;
}

return id;
},
);

Expand Down
2 changes: 1 addition & 1 deletion packages/loot-core/src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ handlers['api/download-budget'] = async function ({ syncId, password }) {

// Download the remote file (no need to perform a sync as the file will already be up-to-date)
const result = await handlers['download-budget']({
fileId: remoteBudget.fileId,
cloudFileId: remoteBudget.fileId,
});
if (result.error) {
console.log('Full error details', result.error);
Expand Down
Loading
Loading