UNPKG

labo-components

Version:
132 lines (113 loc) 5.54 kB
import ReactDOM from 'react-dom'; import AnnotationAPI from '../api/AnnotationAPI'; import ProjectAPI from '../api/ProjectAPI'; import ProjectMigrationForm from '../components/workspace/projects/ProjectMigrationForm'; import FlexModalN from '../components/FlexModalN'; export default class ProjectUtil { static DataTypes = Object.freeze({ QUERIES: Symbol("Queries"), BOOKMARKS: Symbol("Bookmarks"), ANNOTATIONS: Symbol("Annotations"), SESSIONS: Symbol("Sessions") }); // ---------------------------- static importOldUserData = async (userId, callback) => { console.debug('first fetch the old user id for user ' + userId) const respU = await ProjectAPI.fetchOldUserId(userId); console.debug(respU) const oldUserId = respU && respU.id ? respU.id : null; if(oldUserId === null) return null; console.debug('then import the projects from old user ' + oldUserId) const respP = await ProjectAPI.importOldUserProjects(userId, oldUserId); console.debug(respP) console.debug('then import the annotations from old user ' + oldUserId) const respA = await AnnotationAPI.importOldUserAnnotations(userId, oldUserId); console.debug(respA) //TODO add call to project API to change the user for old personal collections return { userImport : respU === null ? false : respU, projectImport : respP === null ? false : respP, annotationImport : respA === null ? false : respA } } static deleteProjects = (projects, userId, onCompletion=null) => { if(!projects || !userId)return null; const msg = projects.length > 1 ? 'Are you sure you want to delete ' + projects.length + ' projects?' : 'Are you sure you want to delete project ' + projects[0].name if (window.confirm(msg)) { let calls = projects.length; projects.forEach((project, index) => { ProjectAPI.delete(userId, project.id, status => { calls--; if (calls === 0 && typeof(onCompletion) === 'function') { onCompletion(); } }); }); } }; //TODO add param for the elementId, so it is flexible static moveProject = (user, project, userProjects, onCompletion=null) => { if(!project) return false; const closeModal = () => ReactDOM.unmountComponentAtNode(document.getElementById('project-migration-modal')); const projectMigrationForm = ( <FlexModalN size="large" onClose={closeModal}> <ProjectMigrationForm project={project} projectDataTypes={Object.values(ProjectUtil.DataTypes).map(v => v.description)} userProjects={userProjects} user={user} onMigrate={ (action, user, sourceProject, targetProject, selectedDataTypes) => { closeModal(); ProjectUtil.onMigrate(action, user, sourceProject, targetProject, selectedDataTypes, onCompletion) } } /> </FlexModalN> ) const ref = ReactDOM.render(projectMigrationForm, document.getElementById('project-migration-modal')); }; static onMigrate = (action, user, sourceProject, targetProject, selectedDataTypes, onCompletion=null) => { console.debug( 'going to ' + action + ' project ' + sourceProject.id + ' for user ' + user.id + ' to ' + targetProject.id, selectedDataTypes ) //TODO assemble params for a call to the ProjectAPI (workspace API) if(selectedDataTypes.includes(ProjectUtil.DataTypes.QUERIES.description)) { console.debug('move not supported yet') } if(selectedDataTypes.includes(ProjectUtil.DataTypes.SESSIONS.description)) { console.debug('move not supported yet') } //for the annotation API, determine which types to move/copy const typesToInclude = []; if(selectedDataTypes.includes(ProjectUtil.DataTypes.BOOKMARKS.description)) { typesToInclude.push(ProjectUtil.DataTypes.BOOKMARKS.description); } if(selectedDataTypes.includes(ProjectUtil.DataTypes.ANNOTATIONS.description)) { typesToInclude.push(ProjectUtil.DataTypes.ANNOTATIONS.description); } console.debug('GOING TO ' + action + ' THESE TYPES', typesToInclude); //determine whether to send a call to the Annotation API or not if(typesToInclude.length > 0 && user.id && sourceProject.id && targetProject.id) { if(action === 'move') { console.debug('MOVING ANNOTATIONS!') AnnotationAPI.moveProjectAnnotations(user.id, sourceProject.id, targetProject.id, typesToInclude, status => { if(status && !status.error && typeof(onCompletion) === 'function') { console.debug('COMPLETED THE ANNOTATION MOVE!'); onCompletion(); } }); } else if(action === 'copy') { console.debug('COPYING ANNOTATIONS!') AnnotationAPI.copyProjectAnnotations(user.id, sourceProject.id, targetProject.id, typesToInclude, status => { if(status && !status.error && typeof(onCompletion) === 'function') { onCompletion(); } }); } } } };