Skip to content

Commit

Permalink
Merge pull request #755 from friendsofagape/fix/Project-A-B-resource-…
Browse files Browse the repository at this point in the history
…issue

Fix Issue #728
  • Loading branch information
vipinpaul authored Jan 19, 2023
2 parents e3b5baa + e545ce5 commit 78cf9ad
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 4 deletions.
28 changes: 28 additions & 0 deletions renderer/src/components/hooks/useAddNotification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { AutographaContext } from '@/components/context/AutographaContext';
import moment from 'moment';
import { useContext } from 'react';
import localforage from 'localforage';

export default function useAddNotification() {
const {
action: {
setNotifications,
},
} = useContext(AutographaContext);

const addNotification = async (title, text, type) => {
localforage.getItem('notification').then((value) => {
const temp = [...value];
temp.push({
title,
text,
type,
time: moment().format(),
hidden: true,
});
setNotifications(temp);
});
};

return { addNotification };
}
26 changes: 26 additions & 0 deletions renderer/src/core/projects/existProjectInBackEnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import localforage from 'localforage';

function isBackendProjectExist(ProjectDir) {
const newpath = localStorage.getItem('userPath');
const fs = window.require('fs');
const path = require('path');
// Step1 : check the project Dir and Meta exist
// step2 : exist return True
return new Promise((resolve) => {
// let checkStatus = false;
localforage.getItem('userProfile').then((value) => {
if (value?.username) {
const resourcePath = path.join(newpath, 'autographa', 'users', value.username, 'resources', ProjectDir);
// check for path exist or not and resolve true or false will work for pane 1 now add for other panes
if (fs.existsSync(resourcePath) && fs.existsSync(path.join(resourcePath, 'metadata.json'))) {
resolve(true);
} else {
resolve(false);
}
} else {
resolve(false);
}
});
});
}
export default isBackendProjectExist;
79 changes: 76 additions & 3 deletions renderer/src/layouts/editor/SectionPlaceholder1.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import ReferenceObs from '@/components/EditorPage/ObsEditor/ReferenceObs';
import { isElectron } from '@/core/handleElectron';
import core from '@/components/EditorPage/ObsEditor/core';
import ReferenceAudio from '@/components/EditorPage/Reference/Audio/ReferenceAudio';
import isBackendProjectExist from '@/core/projects/existProjectInBackEnd';
import { SnackBar } from '@/components/SnackBar';
import useAddNotification from '@/components/hooks/useAddNotification';

const TranslationHelps = dynamic(
() => import('@/components/EditorPage/Reference/TranslationHelps'),
Expand All @@ -22,6 +25,10 @@ const TranslationHelps = dynamic(

const SectionPlaceholder1 = ({ editor }) => {
// const supportedBooks = null;
const [snackBar, setOpenSnackBar] = useState(false);
const [snackText, setSnackText] = useState('');
const [notify, setNotify] = useState();
const { addNotification } = useAddNotification();
const [referenceColumnOneData1, setReferenceColumnOneData1] = useState({
languageId: '',
selectedResource: '',
Expand Down Expand Up @@ -148,6 +155,14 @@ const SectionPlaceholder1 = ({ editor }) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [resetResourceOnDeleteOffline?.referenceColumnOneData1Reset, resetResourceOnDeleteOffline?.referenceColumnOneData2Reset]);

const checkResourceExist = async (ProjectDir) => {
if (ProjectDir) {
const existResource = await isBackendProjectExist(ProjectDir);
return existResource;
}
};

// call useEffect on Load resource
useEffect(() => {
const refsHistory = [];
const rows = [];
Expand All @@ -173,8 +188,58 @@ const SectionPlaceholder1 = ({ editor }) => {
Object.entries(_value).forEach(
([_rownum, _value]) => {
rows.push(_rownum);
// if (openResource1 === false
// || openResource2 === false) {
// check existing the dir of resource in backend
// helps resurce : offline TRUE , others resourceId == obs/bible/audio _value.resouceId
if (_value.offline.offline || ['obs', 'bible', 'audio'].includes(_value.resouceId.toLowerCase())) {
let projectDirName;
if (_value.offline.offline) {
projectDirName = _value.offline.data.projectDir;
} else {
projectDirName = _value.name;
}
// offline resource exist check fucntion not awaiting always comes false in resouceExistcheck
checkResourceExist(projectDirName)
.then(async (resourceStatus) => {
if (!resourceStatus) {
// setRemovingSection(row);1 2 3 4
if (_columnnum === '0' && _rownum === '1') {
setRemovingSection('1');
// setOpenResource1(true);
setReferenceColumnOneData1((prev) => ({
...prev,
languageId: '',
selectedResource: '',
refName: '',
header: '',
owner: '',
offlineResource: { offline: false },
}
));
} else if (_columnnum === '0' && _rownum === '2') {
setRemovingSection('2');
// setOpenResource2(true);
setReferenceColumnOneData2((prev) => ({
...prev,
languageId: '',
selectedResource: '',
refName: '',
header: '',
owner: '',
offlineResource: { offline: false },
}
));
}
setNotify('failure');
setSnackText(`${projectDirName} is no longer available. Please download again.`);
setOpenSnackBar(true);
await addNotification(
'Reference Resource',
`${projectDirName} is no longer available \n. Please download again.`,
'failure',
);
}
});
}
if (_rownum === '1') {
setReferenceColumnOneData1({
...referenceColumnOneData1,
Expand All @@ -197,7 +262,6 @@ const SectionPlaceholder1 = ({ editor }) => {
offlineResource: _value?.offline,
});
}
// }
},
);
}
Expand Down Expand Up @@ -234,6 +298,7 @@ const SectionPlaceholder1 = ({ editor }) => {
}
}, [sectionNum]);

// call useEffect on Save reference (call on new resource / new pane)
useEffect(() => {
const refsHistory = [];
localforage.getItem('currentProject').then((projectName) => {
Expand Down Expand Up @@ -341,6 +406,7 @@ const SectionPlaceholder1 = ({ editor }) => {
/>
);
useEffect(() => {
// Set OBS stories
if (isElectron()) {
localforage.getItem('userProfile').then((user) => {
const fs = window.require('fs');
Expand Down Expand Up @@ -496,6 +562,13 @@ const SectionPlaceholder1 = ({ editor }) => {
)}
</>
)}
<SnackBar
openSnackBar={snackBar}
snackText={snackText}
setOpenSnackBar={setOpenSnackBar}
setSnackText={setSnackText}
error={notify}
/>
</>
);
};
Expand Down
74 changes: 73 additions & 1 deletion renderer/src/layouts/editor/SectionPlaceholder2.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ import ReferenceObs from '@/components/EditorPage/ObsEditor/ReferenceObs';
import { isElectron } from '@/core/handleElectron';
import core from '@/components/EditorPage/ObsEditor/core';
import ReferenceAudio from '@/components/EditorPage/Reference/Audio/ReferenceAudio';
import isBackendProjectExist from '@/core/projects/existProjectInBackEnd';
import { SnackBar } from '@/components/SnackBar';
import useAddNotification from '@/components/hooks/useAddNotification';

const TranslationHelps = dynamic(
() => import('@/components/EditorPage/Reference/TranslationHelps'),
{ ssr: false },
);

const SectionPlaceholder2 = ({ editor }) => {
const [snackBar, setOpenSnackBar] = useState(false);
const [snackText, setSnackText] = useState('');
const [notify, setNotify] = useState();
const { addNotification } = useAddNotification();
const supportedBooks = null;
const [referenceColumnTwoData1, setReferenceColumnTwoData1] = useState({
languageId: '',
Expand Down Expand Up @@ -142,6 +149,14 @@ const SectionPlaceholder2 = ({ editor }) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [resetResourceOnDeleteOffline?.referenceColumnTwoData1Reset, resetResourceOnDeleteOffline?.referenceColumnTwoData2Reset]);

const checkResourceExist = async (ProjectDir) => {
if (ProjectDir) {
const existResource = await isBackendProjectExist(ProjectDir);
return existResource;
}
};

// call useEffect on Load resource
useEffect(() => {
const refsHistory = [];
const rows = [];
Expand All @@ -167,7 +182,57 @@ const SectionPlaceholder2 = ({ editor }) => {
Object.entries(_value).forEach(
([_rownum, _value]) => {
rows.push(_rownum);
// if (openResource3 === false || openResource4 === false) {
// check existing the dir of resource in backend
// helps resurce : offline TRUE , others resourceId == obs/bible/audio _value.resouceId
if (_value.offline.offline || ['obs', 'bible', 'audio'].includes(_value.resouceId.toLowerCase())) {
let projectDirName;
if (_value.offline.offline) {
projectDirName = _value.offline.data.projectDir;
} else {
projectDirName = _value.name;
}
// console.log({ projectDirName });
// offline resource exist check fucntion not awaiting always comes false in resouceExistcheck
checkResourceExist(projectDirName)
.then(async (resourceStatus) => {
if (!resourceStatus) {
if (_columnnum === '1' && _rownum === '1') {
setRemovingSection('3');
setReferenceColumnTwoData1((prev) => ({
...prev,
languageId: '',
selectedResource: '',
refName: '',
header: '',
owner: '',
offlineResource: { offline: false },
}
));
} else if (_columnnum === '1' && _rownum === '2') {
setRemovingSection('4');
// setOpenResource2(true);
setReferenceColumnTwoData2((prev) => ({
...prev,
languageId: '',
selectedResource: '',
refName: '',
header: '',
owner: '',
offlineResource: { offline: false },
}
));
}
setNotify('failure');
setSnackText(`${projectDirName} is no longer available. Please download again.`);
setOpenSnackBar(true);
await addNotification(
'Reference Resource',
`${projectDirName} is no longer available \n. Please download again.`,
'failure',
);
}
});
}
if (_rownum === '1') {
setReferenceColumnTwoData1({
...referenceColumnTwoData1,
Expand Down Expand Up @@ -494,6 +559,13 @@ const SectionPlaceholder2 = ({ editor }) => {
)}
</>
)}
<SnackBar
openSnackBar={snackBar}
snackText={snackText}
setOpenSnackBar={setOpenSnackBar}
setSnackText={setSnackText}
error={notify}
/>
</>
);
};
Expand Down

0 comments on commit 78cf9ad

Please sign in to comment.