1
1
<template >
2
- <div @click =" importCsv" class =" cursor-pointer flex gap-2 items-center" >
2
+ <div @click =" handleImportClick"
3
+ :class =" [
4
+ 'cursor-pointer flex gap-2 items-center',
5
+ checkProgress ? 'opacity-50 pointer-events-none' : ''
6
+ ]" >
3
7
{{$t('Import from CSV')}}
4
-
5
- <svg v-if =" inProgress"
6
- 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>
8
+ </div >
9
+ <div v-if =" checkProgress" class =" fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-20" >
10
+ <div class =" flex flex-col items-center bg-white dark:bg-gray-800 p-6 rounded-lg shadow-lg" >
11
+ <p class =" text-gray-700 dark:text-gray-300 text-sm" >{{ $t('Checking data...') }}</p >
12
+ <svg 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>
13
+ </div >
7
14
</div >
8
15
<Dialog ref =" confirmDialog" :header =" t('Import Confirmation')" :buttons =" computedButtons" >
9
16
<div v-if =" importStats" >
29
36
<p >{{ $t('Would you like to proceed?') }}</p >
30
37
</template >
31
38
</div >
39
+ <div v-if =" importProgress" class =" fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-20" >
40
+ <div class =" flex flex-col items-center bg-white dark:bg-gray-800 p-6 rounded-lg shadow-lg" >
41
+ <p class =" text-gray-700 dark:text-gray-300 text-sm" >{{ $t('Importing...') }}</p >
42
+ <svg 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>
43
+ </div >
44
+ </div >
32
45
</Dialog >
33
46
</template >
34
47
35
48
<script setup lang="ts">
36
- import { ref , Ref , computed } from ' vue' ;
49
+ import { ref , Ref , computed , watch } from ' vue' ;
37
50
import { callAdminForthApi } from ' @/utils' ;
38
51
import adminforth from ' @/adminforth' ;
39
52
import Papa from ' papaparse' ;
@@ -42,7 +55,8 @@ import { useI18n } from 'vue-i18n';
42
55
43
56
const { t } = useI18n ();
44
57
45
- const inProgress: Ref <boolean > = ref (false );
58
+ const importProgress: Ref <boolean > = ref (false );
59
+ const checkProgress: Ref <boolean > = ref (false );
46
60
const confirmDialog = ref (null );
47
61
const importStats = ref (null );
48
62
const pendingData = ref (null );
@@ -64,33 +78,35 @@ const computedButtons = computed(() => {
64
78
return buttons .filter (button => button .visible !== false );
65
79
});
66
80
async function confirmImport(dialog ) {
67
- dialog .hide ();
68
81
await postData (pendingData .value );
82
+ dialog .hide ();
69
83
}
70
84
71
85
async function checkRecords(data : Record <string , string []>) {
86
+ checkProgress .value = true ;
72
87
const resp = await callAdminForthApi ({
73
88
path: ` /plugin/${props .meta .pluginInstanceId }/check-records ` ,
74
89
method: ' POST' ,
75
90
body: { data }
76
91
});
77
-
92
+ checkProgress . value = false ;
78
93
return resp ;
79
94
}
80
95
81
96
async function confirmImportNewOnly(dialog ) {
82
- dialog .hide ();
83
97
await postDataNewOnly (pendingData .value );
98
+ dialog .hide ();
84
99
}
85
100
86
101
async function postData(data : Record <string , string []>, skipDuplicates : boolean = false ) {
102
+ importProgress .value = true ;
87
103
const resp = await callAdminForthApi ({
88
104
path: ` /plugin/${props .meta .pluginInstanceId }/import-csv ` ,
89
105
method: ' POST' ,
90
106
body: { data }
91
107
});
92
108
93
- inProgress .value = false ;
109
+ importProgress .value = false ;
94
110
95
111
if (resp .importedCount > 0 || resp .updatedCount > 0 ) {
96
112
adminforth .list .refresh ();
@@ -106,13 +122,14 @@ async function postData(data: Record<string, string[]>, skipDuplicates: boolean
106
122
}
107
123
108
124
async function postDataNewOnly(data : Record <string , string []>) {
125
+ importProgress .value = true ;
109
126
const resp = await callAdminForthApi ({
110
127
path: ` /plugin/${props .meta .pluginInstanceId }/import-csv-new-only ` ,
111
128
method: ' POST' ,
112
129
body: { data }
113
130
});
114
131
115
- inProgress .value = false ;
132
+ importProgress .value = false ;
116
133
if (resp .importedCount > 0 ) {
117
134
adminforth .list .refresh ();
118
135
}
@@ -125,18 +142,15 @@ async function postDataNewOnly(data: Record<string, string[]>) {
125
142
}
126
143
127
144
async function importCsv() {
128
- inProgress .value = false ;
129
145
const fileInput = document .createElement (' input' );
130
146
131
147
fileInput .type = ' file' ;
132
148
fileInput .accept = ' .csv' ;
133
149
fileInput .click ();
134
150
fileInput .onchange = async (e ) => {
135
- inProgress .value = true ;
136
151
137
152
const file = (e .target as HTMLInputElement ).files ?.[0 ];
138
153
if (! file ) {
139
- inProgress .value = false ;
140
154
return ;
141
155
}
142
156
const reader = new FileReader ();
@@ -177,7 +191,6 @@ async function importCsv() {
177
191
const stats = await checkRecords (data );
178
192
importStats .value = stats ;
179
193
confirmDialog .value ?.open ();
180
- inProgress .value = false ;
181
194
},
182
195
error : (error ) => {
183
196
adminforth .alert ({
@@ -188,7 +201,6 @@ async function importCsv() {
188
201
}
189
202
});
190
203
} catch (error ) {
191
- inProgress .value = false ;
192
204
adminforth .alert ({
193
205
message: ` Error processing CSV: ${error .message } ` ,
194
206
variant: ' danger'
@@ -198,4 +210,10 @@ async function importCsv() {
198
210
reader .readAsText (file );
199
211
};
200
212
}
213
+ function handleImportClick() {
214
+ console .log (' handleImportClick' , checkProgress .value );
215
+ if (! checkProgress .value ) {
216
+ importCsv ();
217
+ }
218
+ }
201
219
</script >
0 commit comments