diff --git a/scripts/guided-mode/guided-curate-dataset.js b/scripts/guided-mode/guided-curate-dataset.js index ea1ee912e..e9ee03f15 100644 --- a/scripts/guided-mode/guided-curate-dataset.js +++ b/scripts/guided-mode/guided-curate-dataset.js @@ -9001,7 +9001,7 @@ const handleAddContributorHeaderUI = () => { `; }; -const openGuidedAddContributorSwal = async () => { +const window.openGuidedAddContributorSwal = async () => { let affiliationTagify; let contributorRolesTagify; diff --git a/sections/guided_mode/guided_curate_dataset.html b/sections/guided_mode/guided_curate_dataset.html index 51492b21b..d253ca61d 100644 --- a/sections/guided_mode/guided_curate_dataset.html +++ b/sections/guided_mode/guided_curate_dataset.html @@ -2698,7 +2698,7 @@ <h2 class="text-sub-step-title mb-0"> <button class="ui primary basic button mt-sm" id="guided-button-add-contributor" - onclick="openGuidedAddContributorSwal(false)" + onclick="window.openGuidedAddContributorSwal(false)" > <i class="fas fa-plus" style="margin-right: 7px"></i>Add a new contributor </button> diff --git a/src/renderer/src/main.js b/src/renderer/src/main.js index 94d6635a0..701b23c10 100644 --- a/src/renderer/src/main.js +++ b/src/renderer/src/main.js @@ -11,6 +11,7 @@ import './scripts/organize-dataset/organizeDS' import './scripts/organize-dataset/curate-functions' import './scripts/metadata-files/manifest' import './scripts/spreadSheetTools/spreadSheetTools' +import './scripts/others/contributor-storage' // Application Lotties import './assets/lotties/activate-lotties' diff --git a/src/renderer/src/scripts/guided-mode/guided-curate-dataset.js b/src/renderer/src/scripts/guided-mode/guided-curate-dataset.js index 2faf8f176..e6d9f91e0 100644 --- a/src/renderer/src/scripts/guided-mode/guided-curate-dataset.js +++ b/src/renderer/src/scripts/guided-mode/guided-curate-dataset.js @@ -8939,7 +8939,7 @@ const openGuidedEditContributorSwal = async (contibuttorOrcidToEdit) => { const handleAddContributorHeaderUI = () => { const existingContributorORCiDs = getExistingContributorORCiDs(); - const locallyStoredContributorArray = loadStoredContributors().filter((contributor) => { + const locallyStoredContributorArray = window.loadStoredContributors().filter((contributor) => { return !existingContributorORCiDs.includes(contributor.ORCiD); }); @@ -9000,7 +9000,7 @@ const handleAddContributorHeaderUI = () => { `; }; -const openGuidedAddContributorSwal = async () => { +window.openGuidedAddContributorSwal = async () => { let affiliationTagify; let contributorRolesTagify; diff --git a/src/renderer/src/scripts/others/contributor-storage.js b/src/renderer/src/scripts/others/contributor-storage.js new file mode 100644 index 000000000..0e51e7a3b --- /dev/null +++ b/src/renderer/src/scripts/others/contributor-storage.js @@ -0,0 +1,90 @@ +while (!window.htmlPagesAdded) { + await new Promise((resolve) => setTimeout(resolve, 100)) +} + +const storedContributorsPath = window.path.join(window.homeDirectory, "SODA", "stored-contributors.json"); + +// Save the contributors array to the JSON file +const saveStoredContributors = (contributors) => { + try { + window.fs.writeFileSync(storedContributorsPath, JSON.stringify(contributors)); + } catch (err) { + window.log.info("Error saving stored contributors file: " + err); + } +}; + +// Load the stored contributors array from the JSON file +// If the file doesn't exist, return an empty array +window.loadStoredContributors = () => { + try { + const contributorFileData = window.fs.readFileSync(storedContributorsPath); + return JSON.parse(contributorFileData); + } catch (err) { + return []; + } +}; + +// Add a new contributor to the JSON file +// If a contributor with the same ORCiD already exists, update the existing contributor +const addOrUpdateStoredContributor = ( + firstName, + lastName, + ORCiD, + affiliationsArray, + rolesArray +) => { + if (typeof firstName !== "string" || !firstName.length > 0) { + window.log.info("Attempted to add contributor with invalid first name"); + return; + } + if (typeof lastName !== "string" || !lastName.length > 0) { + window.log.info("Attempted to add contributor with invalid last name"); + return; + } + if (typeof ORCiD !== "string" || !ORCiD.length > 0) { + window.log.info("Attempted to add contributor with invalid ORCiD"); + return; + } + if (!Array.isArray(affiliationsArray) || affiliationsArray.length === 0) { + window.log.info("Invalid affiliations array"); + return; + } + if (!Array.isArray(rolesArray) || rolesArray.length === 0) { + window.log.info("Invalid roles array"); + return; + } + + // If the stored contributors file doesn't exist, create it and write an empty array to it + if (!window.fs.existsSync(storedContributorsPath)) { + try { + window.fs.writeFileSync(storedContributorsPath, "[]"); + } catch (err) { + window.log.info("Error creating stored contributors file: " + err); + return; + } + } + + const contributorObj = { + firstName: firstName, + lastName: lastName, + ORCiD: ORCiD, + affiliations: affiliationsArray, + roles: rolesArray, + }; + + const storedContributorsArray = window.loadStoredContributors(); + + const existingStoredContributorWithSameORCiDIndex = storedContributorsArray.findIndex( + (contributorObj) => contributorObj.ORCiD === ORCiD + ); + + // If a contributor with the same ORCiD already exists, update the existing contributor + if (existingStoredContributorWithSameORCiDIndex >= 0) { + storedContributorsArray[existingStoredContributorWithSameORCiDIndex] = contributorObj; + } else { + // If a contributor with the same ORCiD doesn't exist, add the new contributor + storedContributorsArray.push(contributorObj); + } + // Write the updated array to the JSON file + saveStoredContributors(storedContributorsArray); +}; diff --git a/src/renderer/src/scripts/others/renderer.js b/src/renderer/src/scripts/others/renderer.js index c8d9ddeb6..ab78fd107 100644 --- a/src/renderer/src/scripts/others/renderer.js +++ b/src/renderer/src/scripts/others/renderer.js @@ -95,7 +95,7 @@ let introStatus = { // // Log file settings // window.log.setupRendererLogOptions() -const homeDirectory = await window.electron.ipcRenderer.invoke('get-app-path', 'home') +window.homeDirectory = await window.electron.ipcRenderer.invoke('get-app-path', 'home') @@ -1371,16 +1371,16 @@ dragselect_area.subscribe("dragstart", ({ items, event, isDragging }) => { // ///// Global variables for this section // /////// Save and load award and milestone info -// let metadataPath = window.path.join(homeDirectory, "SODA", "METADATA"); +// let metadataPath = window.path.join(window.homeDirectory, "SODA", "METADATA"); // let awardFileName = "awards.json"; // let affiliationFileName = "affiliations.json"; // let milestoneFileName = "milestones.json"; // let protocolConfigFileName = "protocol-config.json"; // let affiliationConfigPath = window.path.join(metadataPath, affiliationFileName); // let milestonePath = window.path.join(metadataPath, milestoneFileName); -window.progressFilePath = window.path.join(homeDirectory, "SODA", "Progress"); -// let guidedProgressFilePath = window.path.join(homeDirectory, "SODA", "Guided-Progress"); -// const guidedManifestFilePath = window.path.join(homeDirectory, "SODA", "guided_manifest_files"); +window.progressFilePath = window.path.join(window.homeDirectory, "SODA", "Progress"); +// let guidedProgressFilePath = window.path.join(window.homeDirectory, "SODA", "Guided-Progress"); +// const guidedManifestFilePath = window.path.join(window.homeDirectory, "SODA", "guided_manifest_files"); // let protocolConfigPath = window.path.join(metadataPath, protocolConfigFileName); // let allCollectionTags = {}; // let currentTags = {}; @@ -1388,11 +1388,11 @@ window.progressFilePath = window.path.join(homeDirectory, "SODA", "Progress"); // if (process.platform === "linux") { // //check if data exists inside of the Soda folder, and if it does, move it into the capitalized SODA folder -// if (fs.existsSync(window.path.join(homeDirectory, "Soda"))) { +// if (fs.existsSync(window.path.join(window.homeDirectory, "Soda"))) { // //copy the folder contents of home/Soda to home/SODA -// fs.copySync(window.path.join(homeDirectory, "Soda"), window.path.join(homeDirectory, "SODA")); +// fs.copySync(window.path.join(window.homeDirectory, "Soda"), window.path.join(window.homeDirectory, "SODA")); // //delete the old folder -// fs.removeSync(window.path.join(homeDirectory, "Soda")); +// fs.removeSync(window.path.join(window.homeDirectory, "Soda")); // } // } @@ -5295,7 +5295,7 @@ const addDataArrayToDatasetStructureAtPath = async (importedData) => { // let original_image_path = path[0]; // let image_path = original_image_path; // let destination_image_path = require("path").join( -// homeDirectory, +// window.homeDirectory, // "SODA", // "banner-image-conversion" // ); diff --git a/src/renderer/src/sections/guided_mode/guided_curate_dataset.html b/src/renderer/src/sections/guided_mode/guided_curate_dataset.html index 1d631aba8..5e191820f 100644 --- a/src/renderer/src/sections/guided_mode/guided_curate_dataset.html +++ b/src/renderer/src/sections/guided_mode/guided_curate_dataset.html @@ -2693,7 +2693,7 @@ <h2 class="text-sub-step-title mb-0"> <button class="ui primary basic button mt-sm" id="guided-button-add-contributor" - onclick="openGuidedAddContributorSwal(false)" + onclick="window.openGuidedAddContributorSwal(false)" > <i class="fas fa-plus" style="margin-right: 7px"></i>Add a new contributor </button>