Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Laurent committed Nov 21, 2023
1 parent 7cdfe74 commit f8ff7ed
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"import": "Import {{ type }}",
"importCleanFailed": "The cleaning of the imported file on server failed: {{ exportPath }}",
"importWithCurrentLocaleHeading": "Different locale detected",
"importWithCurrentLocaleDescription": "This file was exported from the \"{{ foundLocal }}\" locale. Are you sure you want to import it into the \"{{ currentLocale }}\" locale?",
"importWithCurrentLocaleDescription": "This file was exported from the \"{{ foundLocale }}\" locale. Are you sure you want to import it into the \"{{ currentLocale }}\" locale?",
"importDuplicateContinue": "Continue Import",
"importDuplicateDetected": "Duplicates Detected.",
"importDuplicateMessage": "Check the items you'd like to overwrite during import.",
Expand Down
41 changes: 28 additions & 13 deletions lib/methods/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,55 @@ const { EJSON } = require('bson');
module.exports = self => {
return {
async import(req, moduleName) {
const overrideLocale = self.apos.launder.boolean(req.body.overrideLocale);
console.log('🚀 ~ file: import.js:10 ~ import ~ overrideLocale:', overrideLocale);

const {
docs, attachmentsInfo, exportPath, filePath
} = await self.readExportFile(req);
if (!req.user) {
throw self.apos.error('forbidden');
}

const overrideLocale = self.apos.launder.boolean(req.body.overrideLocale);
// let jobId = self.apos.launder.string(req.body.jobId);
// let notificationId = self.apos.launder.string(req.body.notificationId);
let exportPath = self.apos.launder.string(req.body.exportPath);
let filePath = self.apos.launder.string(req.body.filePath);

let docs;
let attachmentsInfo;

if (exportPath) {
({
docs, attachmentsInfo
} = await self.getFilesData(exportPath));
} else {
({
docs, attachmentsInfo, exportPath, filePath
} = await self.readExportFile(req));
}

const total = docs.length + attachmentsInfo.length;
const {
reporting, jobId, notificationId
} = await self.instantiateJob(req, total);

const siteHasMultipleLocale = Object.keys(self.apos.i18n.locales).length > 1;
console.log('🚀 ~ file: import.js:25 ~ import ~ siteHasMultipleLocale:', siteHasMultipleLocale);
const differentLocaleFound = self.getFirstDifferentLocale(req, docs);
console.log('🚀 ~ file: import.js:25 ~ import ~ differentLocaleFound:', differentLocaleFound);
const siteHasMultipleLocales = Object.keys(self.apos.i18n.locales).length > 1;
const differentLocaleFoundInDocs = self.getFirstDifferentLocale(req, docs);

if (siteHasMultipleLocale && differentLocaleFound) {
if (siteHasMultipleLocales && differentLocaleFoundInDocs) {
if (!overrideLocale) {
await reporting.end();
// Display confirmation modal to ask the user if he wants to
// continue the import with the current locale overriding the docs one:
await self.apos.notify(req, ' ', {
type: 'warning',
event: {
name: 'import-locale-differs',
data: {
moduleName,
exportPath,
filePath,
jobId,
notificationId,
content: {
heading: req.t('aposImportExport:importWithCurrentLocaleHeading'),
description: req.t('aposImportExport:importWithCurrentLocaleDescription', {
foundLocal: differentLocaleFound,
foundLocale: differentLocaleFoundInDocs,
currentLocale: req.locale
}),
negativeLabel: 'apostrophe:no',
Expand All @@ -54,6 +68,7 @@ module.exports = self => {
return;
}

// The user decided to continue the import (`overrideLocale` param sent)
self.rewriteDocsWithCurrentLocale(req, docs);
}

Expand Down
15 changes: 8 additions & 7 deletions ui/apos/apps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ export default () => {
}

async function handleDifferentLocale(event) {
console.log('handleDifferentLocale');
const result = await apos.modal.execute('AposModalConfirm', event);
console.log('🚀 ~ file: index.js:31 ~ handleDifferentLocale ~ result:', result);
const continueImport = await apos.modal.execute('AposModalConfirm', event);

if (result) {
if (continueImport) {
try {
await apos.http.post('/api/v1/@apostrophecms/import-export/import', {
const moduleAction = apos.modules[event.moduleName].action;

await apos.http.post(`${moduleAction}/import`, {
body: {
exportPath: event.exportPath,
jobId: event.jobId,
notificationId: event.notificationId
filePath: event.filePath,
overrideLocale: true
}
});
} catch (error) {
Expand All @@ -50,6 +50,7 @@ export default () => {
return;
}

// If not, we still need to clean the uploaded archive
try {
await apos.http.post('/api/v1/@apostrophecms/import-export/clean-export', {
body: {
Expand Down

0 comments on commit f8ff7ed

Please sign in to comment.