Skip to content

Commit b254fbd

Browse files
authored
Download geojson (#215)
* Download geojson * Add translations
1 parent f0e81f8 commit b254fbd

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

src/app/[locale]/admin/routes/_components/route-form.tsx

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Button } from '@nextui-org/button'
44
import { Checkbox } from '@nextui-org/checkbox'
55
import { Input, Textarea } from '@nextui-org/input'
6-
import { IconExternalLink } from '@tabler/icons-react'
6+
import { IconDownload, IconExternalLink } from '@tabler/icons-react'
77
import { useTranslations } from 'next-intl'
88
import { FC, useState } from 'react'
99
import { Controller } from 'react-hook-form'
@@ -16,6 +16,7 @@ import {
1616
useSafeForm,
1717
} from '~/components/generic/safe-form'
1818
import { cn } from '~/helpers/cn'
19+
import { download } from '~/helpers/download-file'
1920
import { multiLineToGeoJsonString } from '~/helpers/spatial-data/multi-line'
2021
import { Link, useRouter } from '~/navigation'
2122
import { createRouteSchema } from '~/schemas/routes'
@@ -214,18 +215,7 @@ export const RouteForm: FC<{
214215
)}
215216
/>
216217

217-
<div className="relative flex-1 basis-64 ">
218-
<Button
219-
as={Link}
220-
href="https://geojson.io/#map=13.26/41.95443/3.21328"
221-
endContent={<IconExternalLink />}
222-
className="mb-4"
223-
color="primary"
224-
target="_blank"
225-
>
226-
Open GeoJSON.io
227-
</Button>
228-
218+
<div className="relative flex-1 basis-64 space-y-4">
229219
<Controller
230220
name="path"
231221
control={form.control}
@@ -240,9 +230,37 @@ export const RouteForm: FC<{
240230
onChange={onChange}
241231
value={value}
242232
label={t('columns.path')}
233+
labelPlacement="outside"
243234
/>
244235
)}
245236
/>
237+
238+
<div className="flex gap-4">
239+
<Button
240+
as={Link}
241+
href="https://geojson.io/#map=13.26/41.95443/3.21328"
242+
startContent={<IconExternalLink />}
243+
color="primary"
244+
target="_blank"
245+
>
246+
{t('open-editor')}
247+
</Button>
248+
249+
<Button
250+
startContent={<IconDownload />}
251+
color="primary"
252+
onPress={() => {
253+
download(
254+
form.getValues('path'),
255+
`route-${form.getValues('name')}-${new Date().toISOString()}.json`,
256+
'application/json'
257+
)
258+
}}
259+
disabled={!form.getValues('path')}
260+
>
261+
{t('download-geojson')}
262+
</Button>
263+
</div>
246264
</div>
247265
</div>
248266

src/helpers/download-file.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function download(
2+
content: string,
3+
filename: string,
4+
contentType: string = 'text/plain'
5+
) {
6+
const blob = new Blob([content], { type: contentType })
7+
const url = URL.createObjectURL(blob)
8+
const link = document.createElement('a')
9+
link.download = filename
10+
link.href = url
11+
link.click()
12+
}

src/messages/ca.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@
292292
"edit": "Editar",
293293
"delete": "Eliminar"
294294
},
295-
"delete-unsupported": "Eliminar no està suportat, elimina directament de la base de dades"
295+
"delete-unsupported": "Eliminar no està suportat, elimina directament de la base de dades",
296+
"open-editor": "Obrir editor",
297+
"download-geojson": "Descarregar GeoJSON"
296298
},
297299
"admin-login": {
298300
"heading": "Accés denegat",

src/messages/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@
293293
"edit": "Edit",
294294
"delete": "Delete"
295295
},
296-
"delete-unsupported": "Delete is not supported, delete from database directly"
296+
"delete-unsupported": "Delete is not supported, delete from database directly",
297+
"open-editor": "Open editor",
298+
"download-geojson": "Download GeoJSON"
297299
},
298300
"admin-login": {
299301
"heading": "Access denied",

0 commit comments

Comments
 (0)