Skip to content

Commit

Permalink
Try to get upload working
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjgeiger committed Jun 3, 2024
1 parent 9653666 commit 2384879
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/models/file-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class FileProviderModel extends BaseFileItem {
@belongsTo('base-file-item', { polymorphic: true })
rootFolder!: AsyncBelongsTo<FileModel> & FileModel;

@hasMany('file', { polymorphic: true })
@hasMany('file', { inverse:'parentFolder', polymorphic: true })
files!: AsyncHasMany<FileModel>;

@belongsTo('abstract-node', { inverse: 'files', polymorphic: true })
Expand Down
37 changes: 28 additions & 9 deletions app/preprints/-components/submit/file/upload-file/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import Toast from 'ember-toastr/services/toast';
import PreprintModel from 'ember-osf-web/models/preprint';
import PreprintStateMachine from 'ember-osf-web/preprints/-components/submit/preprint-state-machine/component';
import FileModel from 'ember-osf-web/models/file';
import { task } from 'ember-concurrency';
import { waitFor } from '@ember/test-waiters';
import { taskFor } from 'ember-concurrency-ts';

interface PreprintUploadArgs {
manager: PreprintStateMachine;
Expand All @@ -22,6 +25,14 @@ interface PreprintUploadArgs {
export default class PreprintUpload extends Component<PreprintUploadArgs> {
@service intl!: Intl;
@service toast!: Toast;
url?: URL;
rootFolder?: FileModel;

constructor(owner: unknown, args: any) {
super(owner, args);

taskFor(this.prepUrl).perform();
}

get clickableElementSelectors() {
if (this.args.clickableElementId) {
Expand All @@ -46,21 +57,29 @@ export default class PreprintUpload extends Component<PreprintUploadArgs> {
};
}

@task
@waitFor
async prepUrl() {
const theFiles = await this.args.preprint.files;
const rootFolder = await theFiles.firstObject!.rootFolder;
const urlString = await theFiles.firstObject!.links.upload as string;
const url = new URL( urlString );
this.url = url;
this.rootFolder = rootFolder;
}

@action
buildUrl(files: any[]): string {
const { name } = files[0];
const url = new URL(this.args.preprint.files.firstObject!.links.upload as string);
url.searchParams.append('kind', 'file');
url.searchParams.append('name', name);
return url.toString();
this.url!.searchParams.append('kind', 'file');
this.url!.searchParams.append('name', name);
return this.url!.toString();
}

@action
@task
@waitFor
async success(_: any, __:any, file: FileModel): Promise<void> {
const preprint = await this.args.manager.preprint;
const files = await preprint.files;
const osfStorage = await files!.firstObject;
const primaryFile = await osfStorage!.files;
const primaryFile = await this.rootFolder!.files;
this.args.manager.preprint.set('primaryFile', primaryFile.firstObject);
await this.args.manager.preprint.save();
this.args.validate(file);
Expand Down
46 changes: 25 additions & 21 deletions app/preprints/-components/submit/file/upload-file/template.hbs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
<div ...attributes>
{{#let (unique-id 'upload-files-dropzone') as |id|}}
<DropzoneWidget
local-class='upload-file-widget'
@buildUrl={{this.buildUrl}}
@options={{this.dropzoneOptions}}
@dropzone={{false}}
@dragenter={{@dragEnter}}
@dragleave={{@dragLeave}}
@dragover={{@dragOver}}
@drop={{@drop}}
@enable={{@enable}}
@id={{id}}
@clickable={{this.clickableElementSelectors}}
@preUpload={{this.preUpload}}
@success={{this.success}}
>
{{yield}}
</DropzoneWidget>
{{/let}}
</div>
{{#if this.prepUrl.isRunning}}
<LoadingIndicator data-test-loading-indicator @dark={{true}} />
{{else}}
<div ...attributes>
{{#let (unique-id 'upload-files-dropzone') as |id|}}
<DropzoneWidget
local-class='upload-file-widget'
@buildUrl={{this.buildUrl}}
@options={{this.dropzoneOptions}}
@dropzone={{false}}
@dragenter={{@dragEnter}}
@dragleave={{@dragLeave}}
@dragover={{@dragOver}}
@drop={{@drop}}
@enable={{@enable}}
@id={{id}}
@clickable={{this.clickableElementSelectors}}
@preUpload={{this.preUpload}}
@success={{perform this.success}}
>
{{yield}}
</DropzoneWidget>
{{/let}}
</div>
{{/if}}

0 comments on commit 2384879

Please sign in to comment.