From 39c58c3adc9e6f7ac518e72bdc45e101993ba2fb Mon Sep 17 00:00:00 2001 From: sijumoncy <72241997+sijumoncy@users.noreply.github.com> Date: Wed, 24 Apr 2024 11:10:40 +0530 Subject: [PATCH] refactor fucntions; checkpoint for config store on initial load; fix for load back from inprogress on initial load --- .../TranslationMergeUI.jsx | 61 ++++++++++++------- .../mergeTextTranslationProject.js | 4 +- .../layouts/projects/ImportProjectPopUp.js | 17 +++++- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/renderer/src/components/TextTranslationMerge/TranslationMergeUI.jsx b/renderer/src/components/TextTranslationMerge/TranslationMergeUI.jsx index 670aa7cd..4518d4fe 100644 --- a/renderer/src/components/TextTranslationMerge/TranslationMergeUI.jsx +++ b/renderer/src/components/TextTranslationMerge/TranslationMergeUI.jsx @@ -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); @@ -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); @@ -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) @@ -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); }; /** @@ -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); @@ -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) { @@ -309,6 +327,7 @@ function TranslationMergeUI({ conflictData, closeMergeWindow, triggerSnackBar }) setLoading(false); } else { await checkForConflictInSelectedBook(selectedBook); + await writeBackConflictConfigData(usfmJsons.conflictMeta.projectFullName, usfmJsons); } })(); } else { diff --git a/renderer/src/components/TextTranslationMerge/mergeTextTranslationProject.js b/renderer/src/components/TextTranslationMerge/mergeTextTranslationProject.js index 83f661c4..74fd6824 100644 --- a/renderer/src/components/TextTranslationMerge/mergeTextTranslationProject.js +++ b/renderer/src/components/TextTranslationMerge/mergeTextTranslationProject.js @@ -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); conflictedIngFilePaths.push(key); } } else { @@ -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); diff --git a/renderer/src/layouts/projects/ImportProjectPopUp.js b/renderer/src/layouts/projects/ImportProjectPopUp.js index f7858ccf..65f17f0c 100644 --- a/renderer/src/layouts/projects/ImportProjectPopUp.js +++ b/renderer/src/layouts/projects/ImportProjectPopUp.js @@ -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() @@ -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({});