toasts component testing conditional import of angular setup-jest for vscode-jest support Update jest.config.js Create open-documents.service.spec.ts Add unit tests for all REST services settings service test Remove component from settings service test Create permissions.service.spec.ts upload documents service tests Update package.json Create toast.service.spec.ts Tasks service test Statistics widget component tests Update permissions.service.ts Create app.component.spec.ts settings component testing tasks component unit testing Management list component generic tests Some management component tests document notes component unit tests Create document-list.component.spec.ts Create save-view-config-dialog.component.spec.ts Create filter-editor.component.spec.ts small and large document cards unit testing Create bulk-editor.component.spec.ts document detail unit tests saving work on documentdetail component spec Create document-asn.component.spec.ts dashboard & widgets unit testing Fix ResizeObserver mock common component unit tests fix some merge errors Update app-frame.component.spec.ts Create page-header.component.spec.ts input component unit tests FilterableDropdownComponent unit testing and found minor errors update taskservice unit tests Edit dialogs unit tests Create date-dropdown.component.spec.ts Remove selectors from guard tests confirm dialog component tests app frame component test Miscellaneous component tests Update document-list-view.service.spec.ts directives unit tests Remove unused resizeobserver mock guard unit tests Update query-params.spec.ts try to fix flaky playwright filter rules utils & testing Interceptor unit tests Pipes unit testing Utils unit tests Update upload-documents.service.spec.ts consumer status service tests Update setup-jest.ts Create document-list-view.service.spec.ts Update app-routing.module.ts
157 lines
4.1 KiB
TypeScript
157 lines
4.1 KiB
TypeScript
import { Directive, EventEmitter, Input, OnInit, Output } from '@angular/core'
|
|
import { FormGroup } from '@angular/forms'
|
|
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
|
|
import { Observable } from 'rxjs'
|
|
import {
|
|
MATCHING_ALGORITHMS,
|
|
MATCH_AUTO,
|
|
MATCH_NONE,
|
|
} from 'src/app/data/matching-model'
|
|
import { ObjectWithId } from 'src/app/data/object-with-id'
|
|
import { ObjectWithPermissions } from 'src/app/data/object-with-permissions'
|
|
import { PaperlessUser } from 'src/app/data/paperless-user'
|
|
import { AbstractPaperlessService } from 'src/app/services/rest/abstract-paperless-service'
|
|
import { UserService } from 'src/app/services/rest/user.service'
|
|
import { PermissionsFormObject } from '../input/permissions/permissions-form/permissions-form.component'
|
|
import { SettingsService } from 'src/app/services/settings.service'
|
|
|
|
export enum EditDialogMode {
|
|
CREATE = 0,
|
|
EDIT = 1,
|
|
}
|
|
|
|
@Directive()
|
|
export abstract class EditDialogComponent<
|
|
T extends ObjectWithPermissions | ObjectWithId
|
|
> implements OnInit
|
|
{
|
|
constructor(
|
|
protected service: AbstractPaperlessService<T>,
|
|
private activeModal: NgbActiveModal,
|
|
private userService: UserService,
|
|
private settingsService: SettingsService
|
|
) {}
|
|
|
|
users: PaperlessUser[]
|
|
|
|
@Input()
|
|
dialogMode: EditDialogMode = EditDialogMode.CREATE
|
|
|
|
@Input()
|
|
object: T
|
|
|
|
@Output()
|
|
succeeded = new EventEmitter()
|
|
|
|
@Output()
|
|
failed = new EventEmitter()
|
|
|
|
networkActive = false
|
|
|
|
closeEnabled = false
|
|
|
|
error = null
|
|
|
|
abstract getForm(): FormGroup
|
|
|
|
objectForm: FormGroup = this.getForm()
|
|
|
|
ngOnInit(): void {
|
|
if (this.object != null) {
|
|
if (this.object['permissions']) {
|
|
this.object['set_permissions'] = this.object['permissions']
|
|
}
|
|
|
|
this.object['permissions_form'] = {
|
|
owner: (this.object as ObjectWithPermissions).owner,
|
|
set_permissions: (this.object as ObjectWithPermissions).permissions,
|
|
}
|
|
this.objectForm.patchValue(this.object)
|
|
}
|
|
|
|
// wait to enable close button so it doesnt steal focus from input since its the first clickable element in the DOM
|
|
setTimeout(() => {
|
|
this.closeEnabled = true
|
|
})
|
|
|
|
this.userService.listAll().subscribe((r) => {
|
|
this.users = r.results
|
|
if (this.dialogMode === EditDialogMode.CREATE) {
|
|
this.objectForm.get('permissions_form')?.setValue({
|
|
owner: this.settingsService.currentUser.id,
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
getCreateTitle() {
|
|
return $localize`Create new item`
|
|
}
|
|
|
|
getEditTitle() {
|
|
return $localize`Edit item`
|
|
}
|
|
|
|
getTitle() {
|
|
switch (this.dialogMode) {
|
|
case EditDialogMode.CREATE:
|
|
return this.getCreateTitle()
|
|
case EditDialogMode.EDIT:
|
|
return this.getEditTitle()
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
getMatchingAlgorithms() {
|
|
return MATCHING_ALGORITHMS
|
|
}
|
|
|
|
get patternRequired(): boolean {
|
|
return (
|
|
this.objectForm?.value.matching_algorithm !== MATCH_AUTO &&
|
|
this.objectForm?.value.matching_algorithm !== MATCH_NONE
|
|
)
|
|
}
|
|
|
|
save() {
|
|
this.error = null
|
|
const formValues = Object.assign({}, this.objectForm.value)
|
|
const permissionsObject: PermissionsFormObject =
|
|
this.objectForm.get('permissions_form')?.value
|
|
if (permissionsObject) {
|
|
formValues.owner = permissionsObject.owner
|
|
formValues.set_permissions = permissionsObject.set_permissions
|
|
delete formValues.permissions_form
|
|
}
|
|
|
|
var newObject = Object.assign(Object.assign({}, this.object), formValues)
|
|
var serverResponse: Observable<T>
|
|
switch (this.dialogMode) {
|
|
case EditDialogMode.CREATE:
|
|
serverResponse = this.service.create(newObject)
|
|
break
|
|
case EditDialogMode.EDIT:
|
|
serverResponse = this.service.update(newObject)
|
|
default:
|
|
break
|
|
}
|
|
this.networkActive = true
|
|
serverResponse.subscribe({
|
|
next: (result) => {
|
|
this.activeModal.close()
|
|
this.succeeded.emit(result)
|
|
},
|
|
error: (error) => {
|
|
this.error = error.error
|
|
this.networkActive = false
|
|
this.failed.next(error)
|
|
},
|
|
})
|
|
}
|
|
|
|
cancel() {
|
|
this.activeModal.close()
|
|
}
|
|
}
|