Skip to content

Commit 0f1cd34

Browse files
committed
fix download button
1 parent bae12b2 commit 0f1cd34

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/pages/Map.tsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Control from 'react-leaflet-custom-control'
1414
import { INITIAL_BOUNDS, INITIAL_ZOOM } from '@/constants'
1515
import { useLocation } from 'react-router'
1616
import { useDB } from '../context/DBContext'
17+
import { getBytes, ref as storageRef } from 'firebase/storage'
1718

1819
function SetInitialBounds() {
1920
const location = useLocation()
@@ -33,7 +34,7 @@ function SetInitialBounds() {
3334
}
3435

3536
const Map: React.FC = () => {
36-
const { addIncident, deleteIncident, editIncident, isAdmin, db } = useDB()
37+
const { addIncident, deleteIncident, editIncident, isAdmin, db, storage } = useDB()
3738
const apiKey = import.meta.env.VITE_STADIA_KEY
3839
const maxBounds: LatLngBoundsLiteral = [
3940
// Southwest coordinate
@@ -57,6 +58,7 @@ const Map: React.FC = () => {
5758
const [markerDisplayType, setMarkerDisplayType] = useState<'single' | 'group' | 'groupPie'>('groupPie')
5859
const [location, setLocation] = useState<Incident['location'] | null>(null)
5960
const [editID, setEditID] = useState<string | null>(null)
61+
const [isDownloadingCheckpoint, setIsDownloadingCheckpoint] = useState(false)
6062

6163
async function submitIncident(
6264
dateString: Incident['dateString'],
@@ -112,6 +114,33 @@ const Map: React.FC = () => {
112114
setLocation(null)
113115
}
114116

117+
async function downloadAdminCheckpoint() {
118+
if (isDownloadingCheckpoint) return
119+
120+
try {
121+
setIsDownloadingCheckpoint(true)
122+
123+
const fileRef = storageRef(storage, 'adminCheckpointState.json')
124+
const bytes = await getBytes(fileRef)
125+
const blob = new Blob([bytes], { type: 'application/json' })
126+
const url = URL.createObjectURL(blob)
127+
128+
const a = document.createElement('a')
129+
a.href = url
130+
a.download = 'adminCheckpointState.json'
131+
document.body.appendChild(a)
132+
a.click()
133+
a.remove()
134+
135+
URL.revokeObjectURL(url)
136+
} catch (error) {
137+
console.error('No se pudo descargar adminCheckpointState.json:', error)
138+
alert('No se pudo descargar la copia de datos')
139+
} finally {
140+
setIsDownloadingCheckpoint(false)
141+
}
142+
}
143+
115144
return (
116145
<div className="relative h-full">
117146
<MapContainer
@@ -183,12 +212,13 @@ const Map: React.FC = () => {
183212
>
184213
Crear incidente
185214
</button>
186-
<a
187-
href="https://firebasestorage.googleapis.com/v0/b/redcoralmap.appspot.com/o/adminCheckpointState.json?alt=media"
188-
className="block rounded-md bg-blue-500 p-2 text-white hover:bg-blue-600"
215+
<button
216+
onClick={downloadAdminCheckpoint}
217+
disabled={isDownloadingCheckpoint}
218+
className="block rounded-md bg-blue-500 p-2 text-white hover:bg-blue-600 disabled:cursor-not-allowed disabled:opacity-60"
189219
>
190-
Guardar copia de datos
191-
</a>
220+
{isDownloadingCheckpoint ? 'Descargando...' : 'Guardar copia de datos'}
221+
</button>
192222
</div>
193223
)}
194224
<div>

0 commit comments

Comments
 (0)