Skip to content

Commit

Permalink
fixed ta offline , local and online loading . fixed rerender
Browse files Browse the repository at this point in the history
  • Loading branch information
sijumoncy committed Nov 24, 2023
1 parent e66b4e4 commit de51316
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 52 deletions.
113 changes: 63 additions & 50 deletions renderer/src/components/EditorPage/Reference/TA/TaNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ export default function TaNavigation({ languageId, referenceResources }) {
},
} = useContext(ReferenceContext);

useEffect(() => {
// offline figureout the options folders in backend
if (referenceResources?.offlineResource?.offline) {
const { offlineResource } = referenceResources;
if (offlineResource?.data?.value?.meta?.ingredients?.length > 0) {
const folderOptions = offlineResource?.data?.value?.meta?.ingredients.map((item) => item.identifier);
setoptions(folderOptions);
setselectedOption(folderOptions[0]);
}
} else {
// online
// fetch(`${BaseUrl}${owner}/${languageId}_ta/contents/translate/`)
fetch(`${BaseUrl}${owner}/${languageId}_ta/`)
.then((response) => response.json())
.then((actualData) => {
// get avaialble books/ingredients
const optionsList = actualData?.ingredients?.map((item) => item.identifier);
if (optionsList?.length > 0) {
setoptions(optionsList);
setselectedOption(optionsList[0]);
}
})
.catch((err) => {
logger.debug('In Fetch TA Content.js', `Error in Fetch : ${err.message}`);
});
}
}, [referenceResources, BaseUrl, languageId, owner]);

useEffect(() => {
if (referenceResources && referenceResources?.offlineResource?.offline) {
// offline
Expand All @@ -42,10 +70,7 @@ export default function TaNavigation({ languageId, referenceResources }) {
const currentUser = user?.username;
const folder = path.join(newpath, packageInfo.name, 'users', `${currentUser}`, 'resources');
const projectName = `${offlineResource?.data?.value?.meta?.name}_${offlineResource?.data?.value?.meta?.owner}_${offlineResource?.data?.value?.meta?.release?.tag_name}`;
// multiple books or options
if (offlineResource?.data?.value?.meta?.books.length > 0) {
setoptions(offlineResource.data.value.meta.books);
}

// if (fs.existsSync(path.join(folder, projectName, 'translate'))) {
if (selectedOption && fs.existsSync(path.join(folder, projectName, selectedOption))) {
fs.readdir(path.join(folder, projectName, selectedOption), async (err, folderNames) => {
Expand Down Expand Up @@ -74,53 +99,41 @@ export default function TaNavigation({ languageId, referenceResources }) {
} else {
// online
const taArray = [];
// fetch(`${BaseUrl}${owner}/${languageId}_ta/contents/translate/`)
fetch(`${BaseUrl}${owner}/${languageId}_ta/`)
.then((response) => response.json())
.then((actualData) => {
// get avaialble books/ingredients
const optionsList = actualData?.ingredients?.map((item) => item.identifier);
if (optionsList?.length > 0 && !options.length > 0) {
setoptions(optionsList);
}
// get data function
const fetchData = async () => {
await fetch(`${BaseUrl}${owner}/${languageId}_ta/contents/${selectedOption}/`)
.then((response) => response.json())
.then((folderData) => {
// console.log({ folderData });
!folderData?.message && folderData?.forEach((element) => {
const pattern = /^.*\.(yml|yaml)/gm;
if (!pattern.test(element.name.toLowerCase())) {
const tempObj = {};
tempObj.folder = element.name;
fetch(`${BaseUrl}${owner}/${languageId}_ta/raw/${selectedOption}/${element.name}/title.md`)
.then((response) => response.text())
.then((data) => {
tempObj.title = data;
});
fetch(`${BaseUrl}${owner}/${languageId}_ta/raw/${selectedOption}/${element.name}/sub-title.md`)
.then((response) => response.text())
.then((data) => {
tempObj.subTitle = data;
});
taArray.push(tempObj);
}
});
});
};

const getData = async () => {
await fetchData();
setTaList(taArray);
setSelected(taArray[0]);
};
// console.log({ selectedOption });
selectedOption && getData();
})
.catch((err) => {
logger.debug('In Fetch TA Content.js', `Error in Fetch : ${err.message}`);
});
// get data function
const fetchData = async () => {
await fetch(`${BaseUrl}${owner}/${languageId}_ta/contents/${selectedOption}/`)
.then((response) => response.json())
.then((folderData) => {
// console.log({ folderData });
!folderData?.message && folderData?.forEach((element) => {
const pattern = /^.*\.(yml|yaml)/gm;
if (!pattern.test(element.name.toLowerCase())) {
const tempObj = {};
tempObj.folder = element.name;
fetch(`${BaseUrl}${owner}/${languageId}_ta/raw/${selectedOption}/${element.name}/title.md`)
.then((response) => response.text())
.then((data) => {
tempObj.title = data;
});
fetch(`${BaseUrl}${owner}/${languageId}_ta/raw/${selectedOption}/${element.name}/sub-title.md`)
.then((response) => response.text())
.then((data) => {
tempObj.subTitle = data;
});
taArray.push(tempObj);
}
});
});
};

const getData = async () => {
await fetchData();
setTaList(taArray);
setSelected(taArray[0]);
};
// console.log({ selectedOption });
selectedOption && getData();
}
}, [BaseUrl, languageId, options, owner, referenceResources, selectedOption]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ console.log(headerArr);
break;

case 'ta':
// console.log('filepath : ', { projectId, filePath });
setOfflineMarkdown('');
setOfflineMarkdown('');
if (filePath && projectId && fs.existsSync(path.join(folder, projectName, projectId, filePath))) {
const filecontent = fs.readFileSync(path.join(folder, projectName, projectId, filePath), 'utf8');
// console.log('filecontent : ', { filecontent });
Expand Down

0 comments on commit de51316

Please sign in to comment.