-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChoice.jsx
More file actions
181 lines (166 loc) · 6.05 KB
/
Copy pathChoice.jsx
File metadata and controls
181 lines (166 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { useState, useRef } from 'react'
import { useFiles } from '../providers/Files/Hook'
import Preview from './Preview'
import { BsFileEarmarkZip } from 'react-icons/bs'
import { Mailling } from '../utils/Mailling'
const Choice = () => {
const { files } = useFiles()
const { data, keys } = files.xlsx
const [fileName, setFileName] = useState()
const [selectedKey, setSelectedKey] = useState(null)
const [isLoading, setIsLoading] = useState(false)
const [progress, setProgress] = useState({ current: 0, total: 0 })
const [statusMessage, setStatusMessage] = useState('')
const intervalRef = useRef(null)
const handleClick = (key) => {
setSelectedKey(key)
setFileName(`${key}_${data[0][key]}`)
setProgress({ current: 0, total: 0 })
setStatusMessage('')
}
const handleDownload = async () => {
setIsLoading(true)
setProgress({ current: 0, total: data.length })
setStatusMessage('Processando arquivos...')
// Função para animar os pontos após "Compactando arquivos"
const startCompactingAnimation = () => {
let dots = 0
setStatusMessage('Compactando arquivos')
intervalRef.current = setInterval(() => {
dots = (dots + 1) % 4 // 0,1,2,3
setStatusMessage('Compactando arquivos' + '.'.repeat(dots))
}, 2000)
}
try {
const zip = await Mailling(
data,
files.docx.file,
selectedKey,
(prog) => {
setProgress(prog)
},
() => {
startCompactingAnimation()
setProgress({ current: data.length, total: data.length })
}
)
// ZIP gerado, limpa animação
if (intervalRef.current) {
clearInterval(intervalRef.current)
intervalRef.current = null
}
if (zip.status === 'ok') {
setStatusMessage('Download pronto!')
// trigger download...
const url = URL.createObjectURL(zip.zip)
const a = document.createElement('a')
a.href = url
a.download = 'mala-direta.zip'
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
} else {
alert(zip.message)
setStatusMessage('')
}
} catch (error) {
alert('Erro ao gerar os arquivos.')
console.error(error)
setStatusMessage('')
} finally {
setIsLoading(false)
setProgress({ current: 0, total: 0 })
if (intervalRef.current) {
clearInterval(intervalRef.current)
intervalRef.current = null
}
}
}
const progressPercent =
progress.total > 0
? Math.round((progress.current / progress.total) * 100)
: 0
return (
<div
className="flex flex-col justify-start gap-6 w-full items-center px-4 text-wt-1"
role="region"
aria-label="Gerador de Arquivo"
>
<h2 className="text-or-1 text-2xl font-bold text-center">
Gerar arquivos DOCX
</h2>
<div className="w-full max-w-3xl flex flex-col gap-8">
{/* Botões de escolha de chave */}
<div className="overflow-auto max-h-[300px] custom-scrollbar">
<h3 className="text-center text-gr-2 mb-6">
Escolha a coluna para nomear os arquivos:
</h3>
<div className="grid sm:grid-cols-2 md:grid-cols-3 gap-4">
{keys.map((key) => {
const isSelected = selectedKey === key
return (
<button
key={key}
onClick={() => handleClick(key)}
className={`w-full px-4 py-2 rounded font-semibold transition-all focus:outline-none focus-visible:ring-2
${
isSelected
? 'bg-or-2 ring-or-1 text-wt-1 scale-105 shadow-md'
: 'bg-or-3 hover:bg-or-3 opacity-40 text-wt-1 hover:scale-105 hover:shadow-sm'
}`}
aria-label={`Usar chave ${key} para nome do arquivo`}
>
{key}
</button>
)
})}
</div>
</div>
{/* Nome do arquivo + ações */}
{fileName && (
<div className="flex flex-col gap-6 border-t border-gr-2 pt-6">
{/* Actions */}
<div className="flex flex-col md:flex-row w-full gap-4 justify-center items-stretch sm:items-center">
{/* Botão Preview */}
<div className="flex-1 flex justify-center">
<Preview title="preview" />
</div>
{/* Botão de download */}
<div className="flex-1 flex justify-center">
<button
className="w-full sm:w-auto bg-or-3 text-wt-1 font-bold px-6 py-2 rounded hover:bg-or-2 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-or-1 truncate flex items-center justify-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed"
aria-label="Baixar mala direta em arquivo zip"
onClick={handleDownload}
disabled={isLoading}
>
<BsFileEarmarkZip size={18} />
<span className="truncate">
{isLoading ? 'Gerando arquivos...' : 'gerar arquivos'}
</span>
</button>
</div>
</div>
{/* Divisória */}
<div className="border-t border-gr-2" />
{/* Progress bar + legenda */}
{isLoading && progress.total > 0 && (
<>
<div className="w-full bg-gray-300 rounded h-3 overflow-hidden mt-2">
<div
className="bg-or-2 h-3 transition-all duration-300"
style={{ width: `${progressPercent}%` }}
/>
</div>
<div className="text-center mt-2 text-gr-2 font-medium">
{statusMessage} ({progress.current} de {progress.total})
</div>
</>
)}
</div>
)}
</div>
</div>
)
}
export default Choice