Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Upload BeforeSave Callback #359

Merged
merged 11 commits into from
Sep 14, 2023
3 changes: 1 addition & 2 deletions src/renderer/src/stories/DandiResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ export class DandiResults extends LitElement {

const otherElIds = ["embargo_status"];

const liveId = this.id; // '000552' // From Huszar
const dandiset = await get(liveId, isStaging(this.id) ? "staging" : undefined);
const dandiset = await get(this.id, isStaging(this.id) ? "staging" : undefined);

otherElIds.forEach((str) => handleId(str, dandiset));
elIds.forEach((str) => handleId(str, dandiset.draft_version));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class GuidedUploadPage extends Page {

beforeSave = () => {
const globalState = this.info.globalState;
const isNewDandiset = globalState.upload.dandiset_id !== this.localState.dandiset_id;
const isNewDandiset = globalState.upload?.dandiset_id !== this.localState.dandiset_id;
merge({ upload: this.localState }, globalState); // Merge the local and global states
if (isNewDandiset) delete globalState.upload.results; // Clear the preview results entirely if a new dandiset
};
Expand Down Expand Up @@ -47,21 +47,17 @@ export class GuidedUploadPage extends Page {
if (!result || !result.isConfirmed) return this.to(1);
}

const results = await uploadToDandi.call(this, {
globalUploadInfo.results = await uploadToDandi.call(this, {
...globalUploadInfo.info,
project: globalState.project.name,
});

globalUploadInfo.results = results; // Save the preview results

this.unsavedUpdates = true; // Ensure that this saves automatically

this.to(1);
},
};

render() {
const state = (this.localState = merge(this.info.globalState.upload ?? { info: {}, results: null }, {}));
const state = (this.localState = merge(this.info.globalState.upload ?? { info: {} }, {}));

this.form = new JSONSchemaForm({
schema: dandiUploadSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export class GuidedResultsPage extends Page {
}

render() {
const { conversion } = this.info.globalState.conversion;
const { conversion } = this.info.globalState;

if (!conversion)
return html`<div style="text-align: center;"><p>Your conversion failed. Please try again.</p></div>`;

const { dandiset_id } = this.info.globalState.upload?.info ?? {};

return DandiResults({ id: dandiset_id, files: conversion });
return new DandiResults({ id: dandiset_id, files: conversion });
}
}

Expand Down
28 changes: 16 additions & 12 deletions src/renderer/src/stories/pages/uploads/UploadsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { DandiResults } from "../../DandiResults.js";

export const isStaging = (id) => parseInt(id) >= 100000;

export async function uploadToDandi(info) {
export async function uploadToDandi(info, type = "project" in info ? "" : "folder") {
const api_key = global.data.DANDI?.api_key;
if (!api_key) {
await Swal.fire({
Expand All @@ -31,18 +31,29 @@ export async function uploadToDandi(info) {
return this.to("settings");
}

return await run(
"upload/folder",
const result = await run(
type ? `upload/${type}` : "upload",
{
...info,
staging: isStaging(info.dandiset_id), // Automatically detect staging IDs
api_key,
},
{ title: "Uploading to DANDI" }
).catch((e) => {
this.notify(e.message, "error");
notyf.open({
type: "error",
message: e.message,
});
throw e;
});

if (result)
notyf.open({
type: "success",
message: `${info.project ?? info.nwb_folder_path} successfully uploaded to Dandiset ${info.dandiset_id}`,
});

return result;
}

export class UploadsPage extends Page {
Expand All @@ -58,14 +69,7 @@ export class UploadsPage extends Page {
label: defaultButtonMessage,
onClick: async () => {
await this.form.validate(); // Will throw an error in the callback
const results = await uploadToDandi.call(this, { ...globalState });

if (results)
notyf.open({
type: "success",
message: `${globalState.nwb_folder_path} successfully uploaded to Dandiset ${globalState.dandiset_id}`,
});

await uploadToDandi.call(this, { ...global.data.uploads });
global.data.uploads = {};
global.save();

Expand Down
Loading