Skip to content

Commit bb9732c

Browse files
authored
Merge pull request #5 from devforth/import-csv
Add validation for primary key column
2 parents f447857 + d894d62 commit bb9732c

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

custom/ImportCsv.vue

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
aria-hidden="true" class="w-4 h-4 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg>
77
</div>
88
<Dialog ref="confirmDialog"
9-
header="Import Confirmation"
9+
:header="t('Import Confirmation')"
1010
:buttons="[
11-
{ label: 'Import & Replace', onclick: (dialog) => { confirmImport(dialog) } },
12-
{ label: 'Import New Only', onclick: (dialog) => { confirmImportNewOnly(dialog) } },
13-
{ label: 'Cancel', onclick: (dialog) => dialog.hide() }
11+
{ label: t('Import & Replace'), onclick: (dialog) => { confirmImport(dialog) } },
12+
{ label: t('Import New Only'), onclick: (dialog) => { confirmImportNewOnly(dialog) } },
13+
{ label: t('Cancel'), onclick: (dialog) => dialog.hide() }
1414
]"
1515
>
1616
<div v-if="importStats">
17-
<p>Are you sure you want to continue?</p>
18-
<p class="mt-2">You are importing {{ importStats.total }} records:</p>
17+
<p>{{ $t('Are you sure you want to continue?') }}</p>
18+
<p class="mt-2">{{ $t('You are importing {count} records:', { count: importStats.total }) }}</p>
1919
<ul class="list-disc ml-6 mt-2">
20-
<li>{{ importStats.existingCount }} existing records will be replaced</li>
21-
<li>{{ importStats.newCount }} new records will be added</li>
20+
<li>{{ $t('{count} existing records will be replaced (old records will be lost)', { count: importStats.existingCount }) }}</li>
21+
<li>{{ $t('{count} new records will be added', { count: importStats.newCount }) }}</li>
2222
</ul>
2323
</div>
2424
</Dialog>
@@ -30,6 +30,9 @@ import { callAdminForthApi } from '@/utils';
3030
import adminforth from '@/adminforth';
3131
import Papa from 'papaparse';
3232
import { Dialog } from '@/afcl';
33+
import { useI18n } from 'vue-i18n';
34+
35+
const { t } = useI18n();
3336
3437
const inProgress: Ref<boolean> = ref(false);
3538
const confirmDialog = ref(null);

index.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ export default class ImportExport extends AdminForthPlugin {
131131
return { ok: false, errors };
132132
}
133133

134+
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
135+
134136
const columnValues: any[] = Object.values(data);
135137
for (let i = 0; i < columnValues[0].length; i++) {
136138
const row = {};
@@ -142,7 +144,6 @@ export default class ImportExport extends AdminForthPlugin {
142144

143145
let importedCount = 0;
144146
let updatedCount = 0;
145-
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
146147

147148
await Promise.all(rows.map(async (row) => {
148149
try {
@@ -191,6 +192,7 @@ export default class ImportExport extends AdminForthPlugin {
191192
return { ok: false, errors };
192193
}
193194

195+
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
194196
const columnValues: any[] = Object.values(data);
195197
for (let i = 0; i < columnValues[0].length; i++) {
196198
const row = {};
@@ -201,7 +203,6 @@ export default class ImportExport extends AdminForthPlugin {
201203
}
202204

203205
let importedCount = 0;
204-
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
205206

206207
await Promise.all(rows.map(async (row) => {
207208
try {
@@ -232,19 +233,11 @@ export default class ImportExport extends AdminForthPlugin {
232233
const { data } = body;
233234

234235
const primaryKeyColumn = this.resourceConfig.columns.find(col => col.primaryKey);
235-
if (!primaryKeyColumn) {
236-
return {
237-
ok: true,
238-
total: Object.values(data)[0].length,
239-
existingCount: 0,
240-
newCount: Object.values(data)[0].length
241-
};
242-
}
243236

244237
const rows = [];
245238
const columns = Object.keys(data);
246239
const columnValues = Object.values(data);
247-
for (let i = 0; i < columnValues[0].length; i++) {
240+
for (let i = 0; i < (columnValues[0] as any[]).length; i++) {
248241
const row = {};
249242
for (let j = 0; j < columns.length; j++) {
250243
row[columns[j]] = columnValues[j][i];

0 commit comments

Comments
 (0)