1
1
<script setup lang="ts">
2
2
import api from ' ../../api' ;
3
3
import utils from ' ../../utils' ;
4
+ import { json2csv } from ' json-2-csv' ;
4
5
5
6
const props = defineProps <{
6
7
date: string ,
@@ -13,11 +14,13 @@ const headers = ['Numéro', 'Département', 'Niveau de gravité maximum', 'Resso
13
14
const rows = ref ([]);
14
15
const loading = ref (false );
15
16
const componentKey = ref (0 );
17
+ const ars = ref ([]);
16
18
17
19
async function loadData() {
18
20
rows .value = [];
19
21
loading .value = true ;
20
22
const { data, error } = await api .getArretesRestrictions (props .date , props .area );
23
+ ars .value = data .value ;
21
24
data .value ?.forEach ((d : any ) => {
22
25
rows .value .push ([d .numero , d .departement ?.nom , utils .getShortSituationLabel (utils .getRestrictionRank (d .niveauGraviteMax )),
23
26
d .types .map (t => utils .getTypeLabel (t )).join (' , ' ), d .dateDebut , d .dateFin ? d .dateFin : ' ' , d .fichier ? {
@@ -31,6 +34,43 @@ async function loadData() {
31
34
loading .value = false ;
32
35
}
33
36
37
+ async function downloadCsv() {
38
+ const formatArretes = ars .value
39
+ .map ((arrete : any ) => {
40
+ return {
41
+ id: arrete .id ,
42
+ numero: arrete .numero ,
43
+ date_debut: arrete .dateDebut ,
44
+ date_signature: arrete .dateSignature ,
45
+ date_fin: arrete .dateFin ,
46
+ statut: arrete .statut ,
47
+ departement: arrete .departement .code ,
48
+ chemin_fichier: arrete .fichier ? arrete .fichier ?.url : ' ' ,
49
+ arrete_cadre: arrete .arretesCadre ?.map ((arreteCadre : any ) => {
50
+ return {
51
+ id: arreteCadre .id ,
52
+ numero: arreteCadre .numero ,
53
+ date_debut: arrete .dateDebut ,
54
+ date_signature: arrete .dateSignature ,
55
+ chemin_fichier: arreteCadre .fichier ? arreteCadre .fichier ?.url : ' ' ,
56
+ };
57
+ }),
58
+ };
59
+ });
60
+ const csv = await json2csv (formatArretes , {
61
+ expandArrayObjects: true ,
62
+ });
63
+
64
+ // Create a CSV file and allow the user to download it
65
+ let blob = new Blob ([csv ], { type: ' text/csv' });
66
+ let url = window .URL .createObjectURL (blob );
67
+
68
+ let a = document .createElement (' a' );
69
+ a .href = url ;
70
+ a .download = ` arretes_restrictions_${props .date }.csv ` ;
71
+ a .click ();
72
+ }
73
+
34
74
const tableTitle = computed (() => {
35
75
return ` Arrêtés de restrictions ${props .filterText ? ' (' + props .filterText + ' )' : ' ' } ` ;
36
76
});
@@ -53,6 +93,12 @@ watch(() => props, () => {
53
93
:pagination =" true"
54
94
:key =" componentKey"
55
95
class =" fr-table--layout-fixed" />
96
+
97
+ <div class =" text-align-right fr-mt-1w" >
98
+ <DsfrButton @click =" downloadCsv()" >
99
+ Télécharger les données (CSV)
100
+ </DsfrButton >
101
+ </div >
56
102
</template >
57
103
<template v-else >
58
104
<div class =" fr-grid-row fr-grid-row--center fr-my-2w" >
0 commit comments