-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstructured.vue
More file actions
269 lines (242 loc) · 8 KB
/
Copy pathstructured.vue
File metadata and controls
269 lines (242 loc) · 8 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<template>
<div>
<Breadcrumb>
<BreadcrumbItem to="/">
{{ $t('Accueil') }}
</BreadcrumbItem>
<BreadcrumbItem to="/datasets">
{{ $t('Jeux de données') }}
</BreadcrumbItem>
<BreadcrumbItem>
{{ $t('Publication structurée') }}
</BreadcrumbItem>
</Breadcrumb>
<Stepper
:steps
:current-step="currentStepNumber"
/>
<Step1AssociateSchema
v-if="currentStep === 1"
v-model="associateSchemaForm"
v-model:publication-mode="publicationMode"
@next="moveToStep(2)"
/>
<Step2LoadData
v-if="currentStep === 2"
v-model:resources="resources"
v-model:use-spreadsheet="useSpreadsheet"
:schema="associateSchemaForm.selectedSchema"
:publication-mode="publicationMode"
@previous="moveToStep(1)"
@next="dataNext"
@open-sheet="openSheet"
/>
<Step2Sheet
v-if="currentStep === '2-sheet'"
v-model="file"
v-model:resources="resources"
:schema="associateSchemaForm.selectedSchema"
:publication-mode="publicationMode"
@previous="moveToStep(2)"
@next="dataNext"
/>
<Step3DescribeDataset
v-if="currentStep === 3 && associateSchemaForm.selectedSchema && associateSchemaForm.owned?.organization"
v-model="datasetForm"
:resources="resources"
@previous="goBackFromStep3"
@next="describeNext"
/>
<Step4CompletePublication
v-if="currentStep === 4 && newDataset"
:dataset="newDataset"
@next="updateDataset"
/>
<div class="h-64" />
</div>
</template>
<script setup lang="ts">
import { defaultAccessTypeForm, type Dataset, type Frequency, type Owned, type Resource, type SchemaPublicationMode, type SpatialZone } from '@datagouv/components-next'
import Step1AssociateSchema from '~/components/Datasets/Structured/Step1AssociateSchema.vue'
import Step2LoadData from '~/components/Datasets/Structured/Step2LoadData.vue'
import Step2Sheet from '~/components/Datasets/Structured/Step2Sheet.vue'
import Step3DescribeDataset from '~/components/Datasets/Structured/Step3DescribeDataset.vue'
import Step4CompletePublication from '~/components/Datasets/New/Step4CompletePublication.vue'
import Stepper from '~/components/Stepper/Stepper.vue'
import type { DatasetForm, EnrichedLicense, ResourceForm, SpatialGranularity, Tag } from '~/types/types'
import Breadcrumb from '~/components/Breadcrumb/Breadcrumb.vue'
import BreadcrumbItem from '~/components/Breadcrumbs/BreadcrumbItem.vue'
import type { AssociateSchemaForm } from '~/types/schema'
const { t } = useTranslation()
const config = useRuntimeConfig()
const route = useRoute()
const { $api } = useNuxtApp()
const { start, set, progress, finish } = useLoadingIndicator()
const ASSOCIATE_SCHEMA_FORM_STATE = 'structured-associate-schema-form'
const DATASET_FILES_STATE = 'structured-dataset-files'
const DATASET_FORM_STATE = 'structured-dataset-form'
const USE_SPREADSHEET_STATE = 'structured-use-spreadsheet'
const NEW_DATASET_STATE = 'structured-new-dataset'
const PUBLICATION_MODE_STATE = 'structured-publication-mode'
const UPLOADED_FILE_STATE = 'strutured-uploaded-file'
const steps = computed(() => ([
t('Associez un schéma'),
t('Chargez vos données'),
t('Décrivez votre jeu de données'),
t('Finalisez la publication'),
]))
const useSpreadsheet = useState(USE_SPREADSHEET_STATE, () => false)
const datasetForm = useState(DATASET_FORM_STATE, () => ({
title: '',
acronym: '',
description: '',
featured: false,
owned: null as Owned | null,
tags: [] as Array<Tag>,
license: null as EnrichedLicense | null,
temporal_coverage: { start: null, end: null },
frequency: null as Frequency | null,
spatial_zones: [] as Array<SpatialZone>,
spatial_granularity: null as SpatialGranularity | null,
contact_points: [],
private: true,
...defaultAccessTypeForm(),
} as DatasetForm))
const associateSchemaForm = useState(ASSOCIATE_SCHEMA_FORM_STATE, () => ({
owned: null,
existingDataset: null,
selectedSchema: null,
} as AssociateSchemaForm))
const file = useState<File | null>(UPLOADED_FILE_STATE, () => null)
const resources = useState<Array<ResourceForm>>(DATASET_FILES_STATE, () => [])
const newDataset = useState<Dataset | null>(NEW_DATASET_STATE, () => null)
const publicationMode = useState<SchemaPublicationMode>(PUBLICATION_MODE_STATE, () => 'new')
const currentStep = computed(() => {
const step = route.query.step as string
if (step === '2-sheet') return '2-sheet'
return parseInt(step) || 1
})
const currentStepNumber = computed(() => {
const step = currentStep.value
if (step === '2-sheet') return 2
return step
})
const isCurrentStepValid = computed(() => {
const step = currentStepNumber.value
if (step < 1) return false
if (step > steps.value.length) return false
if (step === 4 && !newDataset.value) return false
return true
})
function moveToStep(step: '2-sheet' | 1 | 2 | 3 | 4) {
if (step !== '2-sheet') {
file.value = null
}
return navigateTo({ path: route.path, query: { ...route.query, step } })
}
function dataNext() {
if (publicationMode.value === 'existing') {
save()
}
else {
datasetForm.value.title = `${associateSchemaForm.value.selectedSchema?.title} (${associateSchemaForm.value.owned?.organization.name})`
datasetForm.value.description = `Ce jeu de données répond aux spécifications du schéma ${associateSchemaForm.value.selectedSchema?.title}`
datasetForm.value.owned = associateSchemaForm.value.owned
moveToStep(3)
}
}
function goBackFromStep3() {
// Si on a utilisé le tableur, retourner à l'écran tableur
if (useSpreadsheet.value) {
moveToStep('2-sheet')
}
else {
moveToStep(2)
}
}
function describeNext() {
save()
}
function openSheet(fileToOpen?: File) {
file.value = fileToOpen ?? null
moveToStep('2-sheet')
}
async function save() {
try {
start()
if (publicationMode.value === 'existing' && associateSchemaForm.value.existingDataset) {
newDataset.value = associateSchemaForm.value.existingDataset
}
else {
newDataset.value = newDataset.value || await $api<Dataset>('/api/1/datasets/', {
method: 'POST',
body: JSON.stringify(datasetToApi(datasetForm.value, { private: true, extras: {
publish_source: `${config.public.title}:structured${useSpreadsheet.value ? '-table' : ''}`,
} })),
})
}
const dataset = newDataset.value
let results: Array<PromiseSettledResult<Resource>> = []
try {
start()
for (const chunk of chunkArray(resources.value, config.public.maxNumberOfResourcesToUploadInParallel)) {
results = [...results, ...await Promise.allSettled(chunk.map(async (resource) => {
const result = await saveResourceForm(dataset, resource)
set(progress.value + 100 / resources.value.length)
return result
}))]
}
finish()
}
catch {
finish({ error: true })
}
if (results.every(f => f.status !== 'rejected')) {
if (publicationMode.value === 'existing') {
await navigateTo(`/datasets/${dataset.slug}`)
clearNuxtState(NEW_DATASET_STATE)
}
else {
await moveToStep(4)
}
associateSchemaForm.value = {
owned: null,
existingDataset: null,
selectedSchema: null,
}
clearNuxtState(USE_SPREADSHEET_STATE)
clearNuxtState(DATASET_FORM_STATE)
clearNuxtState(DATASET_FILES_STATE)
clearNuxtState(PUBLICATION_MODE_STATE)
clearNuxtState(UPLOADED_FILE_STATE)
}
}
finally {
finish()
}
}
async function updateDataset(asPrivate: boolean) {
if (!newDataset.value) {
return moveToStep(3)
}
if (!asPrivate) {
start()
try {
newDataset.value.private = false
await $api<Dataset>(`/api/1/datasets/${newDataset.value.id}`, {
method: 'PUT',
body: JSON.stringify(newDataset.value),
})
}
finally {
finish()
}
}
await navigateTo(`/datasets/${newDataset.value.slug}`)
}
watchEffect(() => {
if (!isCurrentStepValid.value) {
moveToStep(1)
}
})
</script>