From 12b81f436a07b4d6574132a0c6dee5a9630ec23c Mon Sep 17 00:00:00 2001 From: vipinpaul Date: Thu, 26 Oct 2023 14:04:14 +0530 Subject: [PATCH] Enable replace alert in Edit project page --- .../src/components/Projects/ImportPopUp.js | 36 +- renderer/src/modules/projects/ImportPopUp.js | 343 ------------------ 2 files changed, 21 insertions(+), 358 deletions(-) delete mode 100644 renderer/src/modules/projects/ImportPopUp.js diff --git a/renderer/src/components/Projects/ImportPopUp.js b/renderer/src/components/Projects/ImportPopUp.js index b547eb43f..de9b7cf93 100644 --- a/renderer/src/components/Projects/ImportPopUp.js +++ b/renderer/src/components/Projects/ImportPopUp.js @@ -1,5 +1,5 @@ import React, { - useRef, Fragment, useContext, useEffect, + useRef, Fragment, useContext, useEffect, useState, } from 'react'; import PropTypes from 'prop-types'; import { useTranslation } from 'react-i18next'; @@ -18,19 +18,20 @@ export default function ImportPopUp(props) { open, closePopUp, projectType, + replaceConformation, } = props; const cancelButtonRef = useRef(null); - const [books, setBooks] = React.useState([]); - const [folderPath, setFolderPath] = React.useState([]); - const [valid, setValid] = React.useState(false); - const [snackBar, setOpenSnackBar] = React.useState(false); - const [snackText, setSnackText] = React.useState(''); - const [notify, setNotify] = React.useState(); - const [show, setShow] = React.useState(false); - const [fileFilter, setfileFilter] = React.useState([{ name: 'usfm files', extensions: ['usfm', 'sfm', 'USFM', 'SFM'] }]); + const [books, setBooks] = useState([]); + const [folderPath, setFolderPath] = useState([]); + const [valid, setValid] = useState(false); + const [snackBar, setOpenSnackBar] = useState(false); + const [snackText, setSnackText] = useState(''); + const [notify, setNotify] = useState(); + const [show, setShow] = useState(false); + const [fileFilter, setfileFilter] = useState([{ name: 'usfm files', extensions: ['usfm', 'sfm', 'USFM', 'SFM'] }]); const { t } = useTranslation(); - const [labelImportFiles, setLabelImportFiles] = React.useState('Choose USFM files'); + const [labelImportFiles, setLabelImportFiles] = useState(t('label-choose-usfm-files')); const { actions: { setImportedFiles, @@ -104,10 +105,12 @@ export default function ImportPopUp(props) { switch (projectType) { case 'Translation': { const usfm = fs.readFileSync(filePath, 'utf8'); - const myUsfmParser = new grammar.USFMParser(usfm); + const myUsfmParser = new grammar.USFMParser(usfm, grammar.LEVEL.RELAXED); const isJsonValid = myUsfmParser.validate(); if (isJsonValid) { - logger.debug('ImportPopUp.js', 'Valid USFM file.'); + // If importing a USFM file then ask user for replace of USFM with the new content or not + replaceConformation(true); + logger.debug('ImportPopUp. js', 'Valid USFM file.'); const jsonOutput = myUsfmParser.toJSON(); files.push({ id: jsonOutput.book.bookCode, content: usfm }); } else { @@ -121,9 +124,11 @@ export default function ImportPopUp(props) { case 'Audio': { const usfm = fs.readFileSync(filePath, 'utf8'); - const myUsfmParser = new grammar.USFMParser(usfm); + const myUsfmParser = new grammar.USFMParser(usfm, grammar.LEVEL.RELAXED); const isJsonValid = myUsfmParser.validate(); if (isJsonValid) { + // If importing a USFM file then ask user for replace of USFM with the new content or not + replaceConformation(true); logger.debug('ImportPopUp.js', 'Valid USFM file.'); const jsonOutput = myUsfmParser.toJSON(); files.push({ id: jsonOutput.book.bookCode, content: usfm }); @@ -187,12 +192,12 @@ export default function ImportPopUp(props) { switch (projectType) { case 'Translation': setfileFilter([{ name: 'usfm files', extensions: ['usfm', 'sfm', 'USFM', 'SFM'] }]); - setLabelImportFiles('Choose USFM files'); + setLabelImportFiles(t('label-choose-usfm-files')); break; case 'OBS': setfileFilter([{ name: 'markdown files', extensions: ['md', 'markdown', 'MD', 'MARKDOWN'] }]); - setLabelImportFiles('Choose Markdown files'); + setLabelImportFiles(t('label-choose-md-files')); break; default: @@ -334,4 +339,5 @@ ImportPopUp.propTypes = { open: PropTypes.bool, closePopUp: PropTypes.func, projectType: PropTypes.string, + replaceConformation: PropTypes.bool, }; diff --git a/renderer/src/modules/projects/ImportPopUp.js b/renderer/src/modules/projects/ImportPopUp.js deleted file mode 100644 index 38739ce97..000000000 --- a/renderer/src/modules/projects/ImportPopUp.js +++ /dev/null @@ -1,343 +0,0 @@ -import React, { - useRef, Fragment, useContext, useEffect, -} from 'react'; -import PropTypes from 'prop-types'; -import { useTranslation } from 'react-i18next'; -import { Dialog, Transition } from '@headlessui/react'; -import { DocumentTextIcon, FolderOpenIcon } from '@heroicons/react/24/outline'; -import { SnackBar } from '@/components/SnackBar'; -import { ProjectContext } from '@/components/context/ProjectContext'; -import styles from './ImportPopUp.module.css'; -import * as logger from '../../logger'; -import CloseIcon from '@/illustrations/close-button-black.svg'; - -const grammar = require('usfm-grammar'); - -export default function ImportPopUp(props) { - const { - open, - closePopUp, - projectType, - replaceConformation, - } = props; - - const { t } = useTranslation(); - const cancelButtonRef = useRef(null); - const [books, setBooks] = React.useState([]); - const [folderPath, setFolderPath] = React.useState([]); - const [valid, setValid] = React.useState(false); - const [snackBar, setOpenSnackBar] = React.useState(false); - const [snackText, setSnackText] = React.useState(''); - const [notify, setNotify] = React.useState(); - const [show, setShow] = React.useState(false); - const [fileFilter, setfileFilter] = React.useState([{ name: 'usfm files', extensions: ['usfm', 'sfm', 'USFM', 'SFM'] }]); - const [labelImportFiles, setLabelImportFiles] = React.useState(t('label-choose-usfm-files')); - const { - actions: { - setImportedFiles, - }, - } = useContext(ProjectContext); - - function close() { - logger.debug('ImportPopUp.js', 'Closing the Import UI'); - setValid(false); - setShow(false); - closePopUp(); - } - function clear() { - logger.debug('ImportPopUp.js', 'Clearing the data from the path box'); - setFolderPath([]); - setBooks([]); - } - const getBooks = (filePaths) => { - logger.debug('ImportPopUp.js', 'In getBooks for displaying books name using the paths'); - const book = []; - // regex to split path to two groups '(.*[\\\/])' for path and '(.*)' for file name - const regexPath = /^(.*[\\//])(.*)$/; - // execute the match on the string filePath - filePaths.forEach((filePath) => { - const match = regexPath.exec(filePath); - if (match !== null) { - // we ignore the match[0] because it's the match for the hole path string - const fileName = match[2]; - book.push(fileName); - } - }); - setBooks(book); - }; - const openFileDialogSettingData = async () => { - logger.debug('ImportPopUp.js', 'Inside openFileDialogSettingData'); - const options = { - properties: ['openFile', 'multiSelections'], - filters: fileFilter, - }; - const { dialog } = window.require('@electron/remote'); - const chosenFolder = await dialog.showOpenDialog(options); - if ((chosenFolder.filePaths).length > 0) { - logger.debug('ImportPopUp.js', 'Selected the files'); - setShow(true); - } else { - logger.debug('ImportPopUp.js', 'Didn\'t select any file'); - close(); - } - await getBooks(chosenFolder.filePaths); - setFolderPath(chosenFolder.filePaths); - }; - - const OBSValidate = (filename) => { - let match = false; - logger.debug('ImportPopUp.js', 'Inside OBS validate, allow file name with 01-50 only'); - logger.debug('ImportPopUp.js', filename); - if (filename === 'front.md' || filename === 'back.md') { - match = true; - } else { - const regexExp = /^(5[0]|[1-4][0-9]|[0][1-9]).md$/; - match = regexExp.exec(filename); - } - return match; - }; - - const importFiles = (folderPath) => { - logger.debug('ImportPopUp.js', 'Inside importFiles'); - const fs = window.require('fs'); - const files = []; - folderPath.forEach((filePath) => { - switch (projectType) { - case 'Translation': { - const usfm = fs.readFileSync(filePath, 'utf8'); - const myUsfmParser = new grammar.USFMParser(usfm, grammar.LEVEL.RELAXED); - const isJsonValid = myUsfmParser.validate(); - if (isJsonValid) { - // If importing a USFM file then ask user for replace of USFM with the new content or not - replaceConformation(true); - logger.debug('ImportPopUp.js', 'Valid USFM file.'); - const jsonOutput = myUsfmParser.toJSON(); - files.push({ id: jsonOutput.book.bookCode, content: usfm }); - } else { - logger.warn('ImportPopUp.js', 'Invalid USFM file.'); - setNotify('failure'); - setSnackText(t('dynamic-msg-invalid-usfm-file')); - setOpenSnackBar(true); - } - break; - } - - case 'Audio': { - const usfm = fs.readFileSync(filePath, 'utf8'); - const myUsfmParser = new grammar.USFMParser(usfm, grammar.LEVEL.RELAXED); - const isJsonValid = myUsfmParser.validate(); - if (isJsonValid) { - // If importing a USFM file then ask user for replace of USFM with the new content or not - replaceConformation(true); - logger.debug('ImportPopUp.js', 'Valid USFM file.'); - const jsonOutput = myUsfmParser.toJSON(); - files.push({ id: jsonOutput.book.bookCode, content: usfm }); - } else { - logger.warn('ImportPopUp.js', 'Invalid USFM file.'); - setNotify('failure'); - setSnackText(t('dynamic-msg-invalid-usfm-file')); - setOpenSnackBar(true); - } - break; - } - - case 'OBS': { - const mdfile = fs.readFileSync(filePath, 'utf8'); - let filename = filePath.split(/[(\\)?(/)?]/gm).pop(); - const regexExp = /^([1-9]).md$/; - - const matchSingleDigit = regexExp.exec(filename); - if (matchSingleDigit) { - let fileNum = filename.split('.')[0]; - fileNum = fileNum.toString().padStart(2, 0); - filename = `${fileNum}.md`; - } - const isMdValid = OBSValidate(filename); - if (isMdValid) { - logger.debug('ImportPopUp.js', 'Valid Md file.'); - files.push({ id: filename, content: mdfile }); - } else { - logger.warn('ImportPopUp.js', 'Invalid Md file.'); - setNotify('failure'); - setSnackText(t('dynamic-msg-invalid-md-file')); - setOpenSnackBar(true); - } - break; - } - - default: - break; - } - }); - setImportedFiles(files); - close(); - }; - const importProject = async () => { - logger.debug('ImportPopUp.js', 'Inside importProject'); - if (folderPath.length > 0) { - logger.debug('ImportPopUp.js', 'Importing the data'); - setValid(false); - closePopUp(false); - await importFiles(folderPath); - } else { - logger.debug('ImportPopUp.js', 'Invalid Path'); - setValid(true); - setNotify('failure'); - setSnackText(t('dynamic-msg-invalid-path')); - setOpenSnackBar(true); - } - }; - useEffect(() => { - logger.debug('ImportPopUp.js', 'Inside useEffect to set filter types of Import'); - switch (projectType) { - case 'Translation': - setfileFilter([{ name: 'usfm files', extensions: ['usfm', 'sfm', 'USFM', 'SFM'] }]); - setLabelImportFiles(t('label-choose-usfm-files')); - break; - - case 'OBS': - setfileFilter([{ name: 'markdown files', extensions: ['md', 'markdown', 'MD', 'MARKDOWN'] }]); - setLabelImportFiles(t('label-choose-md-files')); - break; - - default: - break; - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [projectType]); - - useEffect(() => { - if (open) { - openFileDialogSettingData(); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [open]); - return ( - <> - - close} - > - -
-
- -
- -
-
- {t('label-import-book')} -
- -
- -
-
-
-

{labelImportFiles}

-
- setFolderPath(e.target.value)} - className="bg-white w-52 lg:w-80 block rounded shadow-sm sm:text-sm focus:border-primary border-gray-300" - /> - -
-
-
-

{valid === true ? t('label-enter-location') : ''}

-
-
- { - books.map((book) => ( -
- - {book} -
- )) - } -
- -
-
- -
-
- - - -
-
-
- -
- -
-
-
- - - ); -} -ImportPopUp.propTypes = { - open: PropTypes.bool, - closePopUp: PropTypes.func, - projectType: PropTypes.string, - replaceConformation: PropTypes.bool, -};