Skip to content

Commit

Permalink
Added saving the import settings on success [#563]
Browse files Browse the repository at this point in the history
 * Directory and max. results are saved when comics are found.
 * Ingoring metadata and deleting blocked pages are saved on import.
  • Loading branch information
mcpierce committed Dec 18, 2020
1 parent 7a3dd29 commit 539a9d5
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 107 deletions.
Expand Up @@ -16,41 +16,43 @@
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

import { Component, OnInit } from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {
AbstractControl,
FormBuilder,
FormGroup,
Validators,
Validators
} from '@angular/forms';
import { LoggerService } from '@angular-ru/logger';
import { Store } from '@ngrx/store';
import { loadComicFiles } from '@app/library/actions/comic-import.actions';
import { selectUser } from '@app/user/selectors/user.selectors';
import { Subscription } from 'rxjs';
import { getUserPreference } from '@app/user';
import {LoggerService} from '@angular-ru/logger';
import {Store} from '@ngrx/store';
import {loadComicFiles} from '@app/library/actions/comic-import.actions';
import {selectUser} from '@app/user/selectors/user.selectors';
import {Subscription} from 'rxjs';
import {getUserPreference} from '@app/user';
import {filter} from 'rxjs/operators';
import {
USER_PREFERENCE_IMPORT_MAXIMUM,
USER_PREFERENCE_IMPORT_ROOT_DIRECTORY,
} from '@app/user/user.constants';
import { filter } from 'rxjs/operators';
DEFAULT_IMPORT_MAXIMUM_RESULTS,
DEFAULT_IMPORT_ROOT_DIRECTORY,
IMPORT_MAXIMUM_RESULTS_PREFERENCE,
IMPORT_ROOT_DIRECTORY_PREFERENCE
} from '@app/library/library.constants';

@Component({
selector: 'cx-import-toolbar',
templateUrl: './import-toolbar.component.html',
styleUrls: ['./import-toolbar.component.scss'],
styleUrls: ['./import-toolbar.component.scss']
})
export class ImportToolbarComponent implements OnInit {
loadFilesForm: FormGroup;
userSubscription: Subscription;

const;
maximumOptions = [
{ label: 'load-comic-files.maximum.all-files', value: 0 },
{ label: 'load-comic-files.maximum.10-files', value: 10 },
{ label: 'load-comic-files.maximum.50-files', value: 50 },
{ label: 'load-comic-files.maximum.100-files', value: 100 },
{ label: 'load-comic-files.maximum.1000-files', value: 1000 },
{label: 'load-comic-files.maximum.all-files', value: 0},
{label: 'load-comic-files.maximum.10-files', value: 10},
{label: 'load-comic-files.maximum.50-files', value: 50},
{label: 'load-comic-files.maximum.100-files', value: 100},
{label: 'load-comic-files.maximum.1000-files', value: 1000}
];

constructor(
Expand All @@ -60,33 +62,34 @@ export class ImportToolbarComponent implements OnInit {
) {
this.loadFilesForm = this.formBuilder.group({
directory: ['', Validators.required],
maximum: ['', Validators.required],
maximum: ['', Validators.required]
});
this.userSubscription = this.store
.select(selectUser)
.pipe(filter((user) => !!user))
.subscribe((user) => {
.pipe(filter(user => !!user))
.subscribe(user => {
this.controls.directory.setValue(
getUserPreference(
user.preferences,
USER_PREFERENCE_IMPORT_ROOT_DIRECTORY,
''
IMPORT_ROOT_DIRECTORY_PREFERENCE,
DEFAULT_IMPORT_ROOT_DIRECTORY
)
);
this.controls.maximum.setValue(
parseInt(
getUserPreference(
user.preferences,
USER_PREFERENCE_IMPORT_MAXIMUM,
'0'
IMPORT_MAXIMUM_RESULTS_PREFERENCE,
`${DEFAULT_IMPORT_MAXIMUM_RESULTS}`
),
10
)
);
});
}

ngOnInit(): void {}
ngOnInit(): void {
}

get controls(): { [p: string]: AbstractControl } {
return this.loadFilesForm.controls;
Expand All @@ -112,7 +115,7 @@ export class ImportToolbarComponent implements OnInit {
this.store.dispatch(
loadComicFiles({
directory: this.directory,
maximum: this.maximum,
maximum: this.maximum
})
);
}
Expand Down
42 changes: 35 additions & 7 deletions comixed-web/src/app/library/effects/comic-import.effects.spec.ts
Expand Up @@ -43,6 +43,13 @@ import {
import { hot } from 'jasmine-marbles';
import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { saveUserPreference } from '@app/user/actions/user.actions';
import {
DELETE_BLOCKED_PAGES_PREFERENCE,
IGNORE_METADATA_PREFERENCE,
IMPORT_MAXIMUM_RESULTS_PREFERENCE,
IMPORT_ROOT_DIRECTORY_PREFERENCE
} from '@app/library/library.constants';

describe('ComicImportEffects', () => {
const FILES = [COMIC_FILE_1, COMIC_FILE_2, COMIC_FILE_3, COMIC_FILE_4];
Expand Down Expand Up @@ -90,18 +97,28 @@ describe('ComicImportEffects', () => {
});

describe('loading comic files', () => {
const MAXIMUM_RESULT = 100;

it('fires an action on success', () => {
const serviceResponse = { files: FILES } as LoadComicFilesResponse;
const action = loadComicFiles({
directory: ROOT_DIRECTORY,
maximum: 100
maximum: MAXIMUM_RESULT
});
const outcome1 = comicFilesLoaded({ files: FILES });
const outcome2 = saveUserPreference({
name: IMPORT_ROOT_DIRECTORY_PREFERENCE,
value: ROOT_DIRECTORY
});
const outcome3 = saveUserPreference({
name: IMPORT_MAXIMUM_RESULTS_PREFERENCE,
value: `${MAXIMUM_RESULT}`
});
const outcome = comicFilesLoaded({ files: FILES });

actions$ = hot('-a', { a: action });
comicImportService.loadComicFiles.and.returnValue(of(serviceResponse));

const expected = hot('-b', { b: outcome });
const expected = hot('-(bcd)', { b: outcome1, c: outcome2, d: outcome3 });
expect(effects.loadComicFiles$).toBeObservable(expected);
expect(alertService.info).toHaveBeenCalledWith(jasmine.any(String));
});
Expand Down Expand Up @@ -141,19 +158,30 @@ describe('ComicImportEffects', () => {
});

describe('sending comic files', () => {
const IGNORE_METADATA = Math.random() > 0.5;
const DELETE_BLOCKED_PAGES = Math.random() > 0.5;

it('fires an action on success', () => {
const serviceResponse = new HttpResponse({ status: 200 });
const action = sendComicFiles({
files: FILES,
ignoreMetadata: false,
deleteBlockedPages: true
ignoreMetadata: IGNORE_METADATA,
deleteBlockedPages: DELETE_BLOCKED_PAGES
});
const outcome1 = comicFilesSent();
const outcome2 = saveUserPreference({
name: IGNORE_METADATA_PREFERENCE,
value: `${IGNORE_METADATA}`
});
const outcome3 = saveUserPreference({
name: DELETE_BLOCKED_PAGES_PREFERENCE,
value: `${DELETE_BLOCKED_PAGES}`
});
const outcome = comicFilesSent();

actions$ = hot('-a', { a: action });
comicImportService.sendComicFiles.and.returnValue(of(serviceResponse));

const expected = hot('-b', { b: outcome });
const expected = hot('-(bcd)', { b: outcome1, c: outcome2, d: outcome3 });
expect(effects.sendComicFiles$).toBeObservable(expected);
expect(alertService.info).toHaveBeenCalledWith(jasmine.any(String));
});
Expand Down
35 changes: 30 additions & 5 deletions comixed-web/src/app/library/effects/comic-import.effects.ts
Expand Up @@ -30,9 +30,16 @@ import {
sendComicFiles,
sendComicFilesFailed
} from '@app/library/actions/comic-import.actions';
import { catchError, map, switchMap, tap } from 'rxjs/operators';
import { catchError, mergeMap, switchMap, tap } from 'rxjs/operators';
import { LoadComicFilesResponse } from '@app/library/models/net/load-comic-files-response';
import { of } from 'rxjs';
import { saveUserPreference } from '@app/user/actions/user.actions';
import {
DELETE_BLOCKED_PAGES_PREFERENCE,
IGNORE_METADATA_PREFERENCE,
IMPORT_MAXIMUM_RESULTS_PREFERENCE,
IMPORT_ROOT_DIRECTORY_PREFERENCE
} from '@app/library/library.constants';

@Injectable()
export class ComicImportEffects {
Expand Down Expand Up @@ -64,9 +71,17 @@ export class ComicImportEffects {
)
)
),
map((response: LoadComicFilesResponse) =>
comicFilesLoaded({ files: response.files })
),
mergeMap((response: LoadComicFilesResponse) => [
comicFilesLoaded({ files: response.files }),
saveUserPreference({
name: IMPORT_ROOT_DIRECTORY_PREFERENCE,
value: action.directory
}),
saveUserPreference({
name: IMPORT_MAXIMUM_RESULTS_PREFERENCE,
value: `${action.maximum}`
})
]),
catchError(error => {
this.logger.error('Service failure:', error);
this.alertService.error(
Expand Down Expand Up @@ -109,7 +124,17 @@ export class ComicImportEffects {
)
)
),
map(() => comicFilesSent()),
mergeMap(() => [
comicFilesSent(),
saveUserPreference({
name: IGNORE_METADATA_PREFERENCE,
value: `${action.ignoreMetadata}`
}),
saveUserPreference({
name: DELETE_BLOCKED_PAGES_PREFERENCE,
value: `${action.deleteBlockedPages}`
})
]),
catchError(error => {
this.logger.error('Service failure:', error);
this.alertService.error(
Expand Down
13 changes: 13 additions & 0 deletions comixed-web/src/app/library/library.constants.ts
Expand Up @@ -31,6 +31,19 @@ export const SEND_COMIC_FILES_URL = `${API_ROOT_URL}/files/import`;

export const LOAD_COMIC_URL = `${API_ROOT_URL}/comics/\${id}`;

// import options
export const IMPORT_ROOT_DIRECTORY_PREFERENCE =
'preference.import.root-directory';
export const DEFAULT_IMPORT_ROOT_DIRECTORY = '';
export const IMPORT_MAXIMUM_RESULTS_PREFERENCE =
'preference.import.maximum-results';
export const DEFAULT_IMPORT_MAXIMUM_RESULTS = 0;
export const IGNORE_METADATA_PREFERENCE = 'preference.import.ignore-metadata';
export const IGNORE_METADATA_DEFAULT = `${false}`;
export const DELETE_BLOCKED_PAGES_PREFERENCE =
'preference.import.delete-blocked-pages';
export const DELETE_BLOCKED_PAGES_DEFAULT = `${false}`;

// display options
export const PAGE_SIZE_PREFERENCE = 'preference.page-size';
export const DEFAULT_PAGE_SIZE = 400;

0 comments on commit 539a9d5

Please sign in to comment.