Skip to content

Commit

Permalink
refactor fucntions; checkpoint for config store on initial load; fix …
Browse files Browse the repository at this point in the history
…for load back from inprogress on initial load
  • Loading branch information
sijumoncy committed Apr 24, 2024
1 parent 50f33e9 commit 39c58c3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
61 changes: 40 additions & 21 deletions renderer/src/components/TextTranslationMerge/TranslationMergeUI.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function TranslationMergeUI({ conflictData, closeMergeWindow, triggerSnackBar })
const checkForConflictInSelectedBook = async (selectedBook) => {
// parse imported
const fs = window.require('fs');
console.log('..............................>>>>>>>>>>>>>>>>>>> ', usfmJsons.conflictMeta, selectedBook);
const IncomingUsfm = fs.readFileSync(path.join(usfmJsons.conflictMeta.incomingPath, selectedBook), 'utf8');
if (IncomingUsfm) {
const importedJson = await parseUsfm(IncomingUsfm);
Expand Down Expand Up @@ -198,7 +199,21 @@ function TranslationMergeUI({ conflictData, closeMergeWindow, triggerSnackBar })
closeMergeWindow();
};

const resolveAndMarkDoneChapter = () => {
// Function to write back the current usfmJSON data as config in the .merge
const writeBackConflictConfigData = async (projectFullName, configData) => {
const fs = window.require('fs');
const newpath = localStorage.getItem('userPath');
const path = require('path');
localforage.getItem('userProfile').then((user) => {
const USFMMergeDirPath = path.join(newpath, packageInfo.name, 'users', user?.username, '.merge-usfm');
if (!fs.existsSync(path.join(USFMMergeDirPath, projectFullName))) {
fs.mkdirSync(path.join(USFMMergeDirPath, projectFullName), { recursive: true });
}
fs.writeFileSync(path.join(USFMMergeDirPath, projectFullName, 'usfmJsons.json'), JSON.stringify(configData));
});
};

const resolveAndMarkDoneChapter = async () => {
setChapterResolveDone(false);
// remove current chapter from conflicted list
const restOfTheChapters = conflictedChapters[selectedBook]?.filter((chNo) => chNo !== selectedChapter);
Expand All @@ -213,10 +228,9 @@ function TranslationMergeUI({ conflictData, closeMergeWindow, triggerSnackBar })
setSelectedChapter(restOfTheChapters[0]);
}
// store the jsons to the backend (/.merge/projectName/BookID.json)
const fs = window.require('fs');

const { projectFullName } = usfmJsons.conflictMeta;
const newpath = localStorage.getItem('userPath');
const path = require('path');

// resolved chapters of each book and resolved books in the conflictMeta and store in the BE
const currentUSFMJsonsData = JSON.parse(JSON.stringify(usfmJsons));
// initial time (if resolved chapters exist for the project)
Expand All @@ -226,13 +240,7 @@ function TranslationMergeUI({ conflictData, closeMergeWindow, triggerSnackBar })
currentUSFMJsonsData.conflictMeta.resolvedStatus = { [selectedBook]: { conflictedChapters: restOfTheChapters, isBookResolved } };
}
setUsfmJsons(currentUSFMJsonsData);
localforage.getItem('userProfile').then((user) => {
const USFMMergeDirPath = path.join(newpath, packageInfo.name, 'users', user?.username, '.merge-usfm');
if (!fs.existsSync(path.join(USFMMergeDirPath, projectFullName))) {
fs.mkdirSync(path.join(USFMMergeDirPath, projectFullName), { recursive: true });
}
fs.writeFileSync(path.join(USFMMergeDirPath, projectFullName, 'usfmJsons.json'), JSON.stringify(currentUSFMJsonsData));
});
await writeBackConflictConfigData(projectFullName, currentUSFMJsonsData);
};

/**
Expand All @@ -250,19 +258,27 @@ function TranslationMergeUI({ conflictData, closeMergeWindow, triggerSnackBar })
let bookToSelect;
const resolvedBooksArr = [];
const conflictedChsOfBooks = {};
// eslint-disable-next-line no-restricted-syntax
for (const book in usfmJsonsContent.conflictMeta.resolvedStatus) {
if (book in usfmJsonsContent.conflictMeta.resolvedStatus) {
const currentBook = usfmJsonsContent.conflictMeta.resolvedStatus[book];
if (!currentBook.isBookResolved && !bookToSelect) {
bookToSelect = book;
}
if (currentBook.isBookResolved) {
resolvedBooksArr.push(book);

// loading config - config have data of atleast 1 chapter of any book resolved ( checkpoint )
if (usfmJsonsContent.conflictMeta.resolvedStatus) {
// eslint-disable-next-line no-restricted-syntax
for (const book in usfmJsonsContent.conflictMeta.resolvedStatus) {
if (book in usfmJsonsContent.conflictMeta.resolvedStatus) {
const currentBook = usfmJsonsContent.conflictMeta.resolvedStatus[book];
if (!currentBook.isBookResolved && !bookToSelect) {
bookToSelect = book;
}
if (currentBook.isBookResolved) {
resolvedBooksArr.push(book);
}
conflictedChsOfBooks[book] = currentBook.conflictedChapters;
}
conflictedChsOfBooks[book] = currentBook.conflictedChapters;
}
} else {
// checkpoint - stored on initially and not worked on any chapter
bookToSelect = usfmJsonsContent.conflictMeta.files[0];
}

setSelectedBook(bookToSelect);
setResolvedBooks(resolvedBooksArr);
setConflictedChapters(conflictedChsOfBooks);
Expand All @@ -274,6 +290,8 @@ function TranslationMergeUI({ conflictData, closeMergeWindow, triggerSnackBar })
}
};

console.log({ conflictData, conflictedChapters, resolvedBooks });

// useEffect to trigger comleted all conflict Resolution
useEffect(() => {
if (resolvedBooks.length >= Object.keys(conflictedChapters).length) {
Expand Down Expand Up @@ -309,6 +327,7 @@ function TranslationMergeUI({ conflictData, closeMergeWindow, triggerSnackBar })
setLoading(false);
} else {
await checkForConflictInSelectedBook(selectedBook);
await writeBackConflictConfigData(usfmJsons.conflictMeta.projectFullName, usfmJsons);
}
})();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const mergeTextTranslationProject = async (incomingPath, currentUser, set
const incomingMd5 = val.checksum.md5;
if (currentMd5 && incomingMd5) {
if (currentMd5 !== incomingMd5) {
console.log('MD5s xxxxxxxxxxxxxx : ', { bookId, currentMd5, incomingMd5 }, currentMd5 === incomingIngredients);

Check warning on line 29 in renderer/src/components/TextTranslationMerge/mergeTextTranslationProject.js

View workflow job for this annotation

GitHub Actions / Lint Run

Unexpected console statement
conflictedIngFilePaths.push(key);
}
} else {
Expand Down Expand Up @@ -86,8 +87,7 @@ export const mergeTextTranslationProject = async (incomingPath, currentUser, set
},
});
} else {
// TODO : ADD A MESSGAGE HERE -nothing to merge
return;
return 'noConflict';
}

setProcessMerge(false);
Expand Down
17 changes: 15 additions & 2 deletions renderer/src/layouts/projects/ImportProjectPopUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ export default function ImportProjectPopUp(props) {
completedSteps: 0,
});

const triggerSnackBar = (status, message) => {
setNotify(status);
setSnackText(message);
setOpenSnackBar(true);
};

async function close(triggeredFrom) {
logger.debug('ImportProjectPopUp.js', `Closing the Dialog box : Triggered from : ${triggeredFrom}`);
removeExtractedZipDir()
Expand Down Expand Up @@ -193,8 +199,15 @@ export default function ImportProjectPopUp(props) {
await mergeProject(folderPath, currentUser, setConflictPopup, setModel, setProcessMerge);
}else if (sbData?.burritoType === 'scripture / textTranslation') {
console.log("Started Indentify Merge conflicts ------");
await mergeTextTranslationProject(folderPath, currentUser, setConflictPopup, setProcessMerge, sbData)
console.log("completed merge idenitfy process ------");
try {
const response = await mergeTextTranslationProject(folderPath, currentUser, setConflictPopup, setProcessMerge, sbData)
if(response === "noConflict") {
triggerSnackBar('success', "No Conflict Found",)
}
console.log("completed merge idenitfy process ------");
} catch(err) {

}
}
setMerge(false)
setSbData({});
Expand Down

0 comments on commit 39c58c3

Please sign in to comment.