deposit-upload.state.ts 2.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {
  Inject,
  Injectable,
} from "@angular/core";
import {ApiEnum} from "@app/shared/enums/api.enum";
import {DepositDataFile} from "@deposit/models/deposit-data-file.model";
import {DlcmFileUploadWrapper} from "@deposit/models/dlcm-file-upload-wrapper.model";
import {DepositDataFileAction} from "@deposit/stores/data-file/deposit-data-file.action";
import {
  DepositUploadAction,
  depositUploadActionNameSpace,
} from "@deposit/stores/upload/deposit-upload.action";
import {DlcmEnvironment} from "@environments/environment.defaults.model";
import {
  Actions,
  State,
  Store,
} from "@ngxs/store";
import {StateEnum} from "@shared/enums/state.enum";
import {
  ApiService,
  defaultUploadStateInitValue,
  ENVIRONMENT,
  isNullOrUndefined,
  LABEL_TRANSLATE,
  LabelTranslateInterface,
  NotificationService,
  Override,
  StringUtil,
  UploadState,
  UploadStateModel,
} from "solidify-frontend";

export interface DepositUploadStateModel extends UploadStateModel<DepositDataFile> {
}

@Injectable()
@State<DepositUploadStateModel>({
  name: StateEnum.deposit_upload,
  defaults: {
    ...defaultUploadStateInitValue(),
  },
  children: [],
})
export class DepositUploadState extends UploadState<DepositUploadStateModel, DepositDataFile> {
  private readonly _FILE_KEY: string = "file";
  private readonly _CATEGORY_KEY: string = "category";
  private readonly _TYPE_KEY: string = "type";
  private readonly _FOLDER_KEY: string = "folder";

  constructor(protected apiService: ApiService,
              protected store: Store,
              protected notificationService: NotificationService,
              protected actions$: Actions,
              @Inject(ENVIRONMENT) protected readonly _environment: DlcmEnvironment,
              @Inject(LABEL_TRANSLATE) protected readonly _labelTranslateInterface: LabelTranslateInterface,
  ) {
    super(apiService, store, notificationService, actions$, {
      nameSpace: depositUploadActionNameSpace,
      callbackAfterAllUploadFinished: action => store.dispatch(new DepositDataFileAction.Refresh(action.parentId)),
    }, _environment, _labelTranslateInterface);
  }

  protected get _urlResource(): string {
    return ApiEnum.preIngestDeposits;
  }

  @Override()
  protected generateUploadFormData(action: DepositUploadAction.UploadFile): FormData {
    const formData = new FormData();
    const fileUploadWrapper = action.fileUploadWrapper as DlcmFileUploadWrapper;
    if (!action.isArchive) {
      formData.append(this._FOLDER_KEY, isNullOrUndefined(fileUploadWrapper.subDirectory) ? StringUtil.stringEmpty : fileUploadWrapper.subDirectory);
    }
    formData.append(this._FILE_KEY, fileUploadWrapper.file, fileUploadWrapper.file.name);
    formData.append(this._CATEGORY_KEY, fileUploadWrapper.dataCategory);
    formData.append(this._TYPE_KEY, fileUploadWrapper.dataType);
    return formData;
  }
}