Skip to content

Commit

Permalink
change csv and tsv to subtypes when importing ezbookkeeping exported …
Browse files Browse the repository at this point in the history
…files in import transaction dialog
  • Loading branch information
mayswind committed Oct 26, 2024
1 parent fb5484f commit 47ab410
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.
27 changes: 15 additions & 12 deletions src/consts/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ const supportedImageExtensions = '.jpg,.jpeg,.png,.gif,.webp';

const supportedImportFileTypes = [
{
type: 'ezbookkeeping_csv',
name: 'ezbookkeeping Data Export File (CSV)',
extensions: '.csv',
document: {
supportMultiLanguages: true,
anchor: 'export-transactions'
}
},
{
type: 'ezbookkeeping_tsv',
name: 'ezbookkeeping Data Export File (TSV)',
extensions: '.tsv',
type: 'ezbookkeeping',
name: 'ezbookkeeping Data Export File',
extensions: '.csv,.tsv',
subTypes: [
{
type: 'ezbookkeeping_csv',
name: 'CSV (Comma-separated values) File',
extensions: '.csv',
},
{
type: 'ezbookkeeping_tsv',
name: 'TSV (Tab-separated values) File',
extensions: '.tsv',
}
],
document: {
supportMultiLanguages: true,
anchor: 'export-transactions'
Expand Down
29 changes: 29 additions & 0 deletions src/lib/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { isString } from './common.js';

export function getFileExtension(filename) {
if (!filename || !isString(filename)) {
return '';
}

const parts = filename.split('.');
return parts[parts.length - 1];
}

export function isFileExtensionSupported(filename, supportedExtensions) {
if (!supportedExtensions) {
return false;
}

const supportedExtensionsArray = supportedExtensions.split(',');
const fileExtension = getFileExtension(filename).toLowerCase();

for (let i = 0; i < supportedExtensionsArray.length; i++) {
const supportedExtension = getFileExtension(supportedExtensionsArray[i]).toLowerCase();

if (supportedExtension === fileExtension) {
return true;
}
}

return false;
}
1 change: 1 addition & 0 deletions src/lib/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,7 @@ function getAllSupportedImportFileTypes(i18nGlobal, translateFn) {
subTypes.push({
type: subType.type,
displayName: translateFn(subType.name),
extensions: subType.extensions
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,7 @@
"Time": "Time",
"Color": "Color",
"Type": "Type",
"Format": "Format",
"All Types": "All Types",
"More": "More",
"All": "All",
Expand Down Expand Up @@ -1523,8 +1524,7 @@
"Data Import Completed": "Data Import Completed",
"File Type": "File Type",
"How to export this file?": "How to export this file?",
"ezbookkeeping Data Export File (CSV)": "ezbookkeeping Data Export File (CSV)",
"ezbookkeeping Data Export File (TSV)": "ezbookkeeping Data Export File (TSV)",
"ezbookkeeping Data Export File": "ezbookkeeping Data Export File",
"Quicken Interchange Format (QIF) File": "Quicken Interchange Format (QIF) File",
"Year-month-day format": "Year-month-day format",
"Month-day-year format": "Month-day-year format",
Expand Down
4 changes: 2 additions & 2 deletions src/locales/zh_Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,7 @@
"Time": "时间",
"Color": "颜色",
"Type": "类型",
"Format": "格式",
"All Types": "全部类型",
"More": "更多",
"All": "全部",
Expand Down Expand Up @@ -1523,8 +1524,7 @@
"Data Import Completed": "数据导入完成",
"File Type": "文件类型",
"How to export this file?": "如何导出该文件?",
"ezbookkeeping Data Export File (CSV)": "ezbookkeeping 数据导出文件 (CSV)",
"ezbookkeeping Data Export File (TSV)": "ezbookkeeping 数据导出文件 (TSV)",
"ezbookkeeping Data Export File": "ezbookkeeping 数据导出文件",
"Quicken Interchange Format (QIF) File": "Quicken Interchange Format (QIF) 文件",
"Year-month-day format": "年-月-日 格式",
"Month-day-year format": "月-日-年 格式",
Expand Down
31 changes: 26 additions & 5 deletions src/views/desktop/transactions/list/dialogs/ImportDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
item-title="displayName"
item-value="type"
:disabled="submitting"
:label="$t('Type')"
:placeholder="$t('Type')"
:label="$t('Format')"
:placeholder="$t('Format')"
:items="allFileSubTypes"
v-model="fileSubType"
/>
Expand Down Expand Up @@ -490,6 +490,7 @@ import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import categoryConstants from '@/consts/category.js';
import transactionConstants from '@/consts/transaction.js';
import { getNameByKeyValue } from '@/lib/common.js';
import { isFileExtensionSupported } from '@/lib/file.js';
import { generateRandomUUID } from '@/lib/misc.js';
import logger from '@/lib/logger.js';
import {
Expand Down Expand Up @@ -534,8 +535,8 @@ export default {
showState: false,
clientSessionId: '',
currentStep: 'uploadFile',
fileType: 'ezbookkeeping_csv',
fileSubType: '',
fileType: 'ezbookkeeping',
fileSubType: 'ezbookkeeping_csv',
importFile: null,
importTransactions: null,
editingTransaction: null,
Expand Down Expand Up @@ -656,6 +657,14 @@ export default {
return getTimezoneOffsetMinutes(this.settingsStore.appSettings.timeZone);
},
supportedImportFileExtensions() {
if (this.allFileSubTypes && this.allFileSubTypes.length) {
const subTypeExtensions = getNameByKeyValue(this.allFileSubTypes, this.fileSubType, 'type', 'extensions');
if (subTypeExtensions) {
return subTypeExtensions;
}
}
return getNameByKeyValue(this.allSupportedImportFileTypes, this.fileType, 'type', 'extensions');
},
exportFileGuideDocumentUrl() {
Expand Down Expand Up @@ -852,12 +861,24 @@ export default {
this.editingTags = [];
this.currentPage = 1;
this.countPerPage = 10;
},
fileSubType: function (newValue) {
let supportedExtensions = getNameByKeyValue(this.allFileSubTypes, newValue, 'type', 'extensions');
if (!supportedExtensions) {
supportedExtensions = getNameByKeyValue(this.allSupportedImportFileTypes, this.fileType, 'type', 'extensions');
}
if (this.importFile && this.importFile.name && !isFileExtensionSupported(this.importFile.name, supportedExtensions)) {
this.importFile = null;
}
}
},
methods: {
open() {
const self = this;
self.fileType = 'ezbookkeeping_csv';
self.fileType = 'ezbookkeeping';
self.fileSubType = 'ezbookkeeping_csv';
self.currentStep = 'uploadFile';
self.importFile = null;
self.importTransactions = null;
Expand Down

0 comments on commit 47ab410

Please sign in to comment.