diff --git a/packages/app-mobile/components/ScreenHeader.tsx b/packages/app-mobile/components/ScreenHeader.tsx index 1a0f2a896..539ec2291 100644 --- a/packages/app-mobile/components/ScreenHeader.tsx +++ b/packages/app-mobile/components/ScreenHeader.tsx @@ -21,7 +21,7 @@ import { FolderEntity } from '@joplin/lib/services/database/types'; import { State } from '@joplin/lib/reducer'; import CustomButton from './CustomButton'; import FolderPicker from './FolderPicker'; -import { getTrashFolderId, itemIsInTrash } from '@joplin/lib/services/trash'; +import { itemIsInTrash } from '@joplin/lib/services/trash'; import restoreItems from '@joplin/lib/services/trash/restoreItems'; import { ModelType } from '@joplin/lib/BaseModel'; @@ -588,7 +588,7 @@ class ScreenHeaderComponent extends PureComponent f.id !== getTrashFolderId())} + folders={Folder.getRealFolders(this.props.folders)} /> ); } else { diff --git a/packages/app-mobile/components/screens/folder.js b/packages/app-mobile/components/screens/folder.js index 961474242..d8de092f5 100644 --- a/packages/app-mobile/components/screens/folder.js +++ b/packages/app-mobile/components/screens/folder.js @@ -10,7 +10,6 @@ const { dialogs } = require('../../utils/dialogs.js'); const { _ } = require('@joplin/lib/locale'); const { default: FolderPicker } = require('../FolderPicker'); const TextInput = require('../TextInput').default; -const { getTrashFolderId } = require('@joplin/lib/services/trash'); class FolderScreenComponent extends BaseScreenComponent { static navigationOptions() { @@ -108,7 +107,7 @@ class FolderScreenComponent extends BaseScreenComponent { f.id !== getTrashFolderId())} + folders={Folder.getRealFolders(this.props.folders)} selectedFolderId={this.state.folder.parent_id} onValueChange={newValue => this.parent_changeValue(newValue)} mustSelect diff --git a/packages/lib/models/Folder.test.ts b/packages/lib/models/Folder.test.ts index 1eaec5d3b..c877abe8a 100644 --- a/packages/lib/models/Folder.test.ts +++ b/packages/lib/models/Folder.test.ts @@ -353,6 +353,27 @@ describe('models/Folder', () => { await expectThrow(async () => Folder.delete(folder2.id, { toTrash: true, toTrashParentId: folder2.id })); }); + it('should filter out the trash folder and the deleted folders', async () => { + let folders: FolderEntity[] = []; + + expect(Folder.getRealFolders(folders).length).toBe(0); + + const folder1 = await Folder.save({}); + const folder2 = await Folder.save({ parent_id: folder1.id }); + const folder3 = await Folder.save({ parent_id: folder2.id }); + + folders = await Folder.all(); + expect(Folder.getRealFolders(folders).sort((a, b) => a.created_time - b.created_time)).toEqual([folder1, folder2, folder3]); + + await Folder.delete(folder2.id, { toTrash: true }); + folders = await Folder.all(); + expect(Folder.getRealFolders(folders)).toEqual([folder1]); + + await Folder.delete(folder1.id, { toTrash: true }); + folders = await Folder.all(); + expect(Folder.getRealFolders(folders).length).toBe(0); + }); + it('should tell if at least one folder other than trash and deleted exists', async () => { let folders: FolderEntity[] = []; expect(Folder.atLeastOneRealFolderExists(folders)).toBe(false); diff --git a/packages/lib/models/Folder.ts b/packages/lib/models/Folder.ts index f9fe3efd3..faaf2bb7a 100644 --- a/packages/lib/models/Folder.ts +++ b/packages/lib/models/Folder.ts @@ -936,10 +936,15 @@ export default class Folder extends BaseItem { return !!folders.find(f => !!f.icon); } + public static getRealFolders(folders: FolderEntity[]) { + // returns all folders other than trash folder and deleted folders + const trashFolderId = getTrashFolderId(); + return folders.filter((folder) => folder.id !== trashFolderId && folder.deleted_time === 0); + } + public static atLeastOneRealFolderExists(folders: FolderEntity[]) { // returns true if at least one folder exists other than trash folder and deleted folders - const trashFolderId = getTrashFolderId(); - return folders.filter((folder) => folder.id !== trashFolderId && folder.deleted_time === 0).length > 0; + return this.getRealFolders(folders).length > 0; } }