Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor category handling #470

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 15 additions & 38 deletions frontend/src/components/ResultPage.vue
Original file line number Diff line number Diff line change
@@ -1,74 +1,51 @@
<script lang="ts" setup>
import { ref, computed, watchEffect } from 'vue'
import { ref, computed } from 'vue'
import axios from 'axios'
import SnackbarAlert from '@/components/SnackbarAlert.vue'
import { TYPOLOGIES, MEASURED_GUNS_TYPOLOGIES } from '@/utils/firearms-utils/index'
import { isUserUsingCrosscall } from '@/utils/isUserUsingCrosscall'
import { useSnackbarStore } from '@/stores/snackbar'
import { useStepsStore } from '@/stores/steps'
import { useResultStore } from '@/stores/result'
import { useRouter, useRoute } from 'vue-router'
import { getMentionsFromCategories } from '@/utils/mentions'

const { setMessage } = useSnackbarStore()
const stepsStore = useStepsStore()
const resultStore = useResultStore()
const router = useRouter()
const route = useRoute()

function getCategoryFromTypologyAndMeasures (typology: string, gunLength: number, gunBarrelLength: number) {
if (gunLength !== null && gunBarrelLength !== null) {
switch (typology) {
case 'epaule_a_pompe':
if (gunLength > 75 && gunBarrelLength > 55) { return 'C' } else { return 'B' }
case 'epaule_a_un_coup_par_canon':
if (gunLength > 75 && gunBarrelLength > 40) { return 'C' } else { return 'B' }
case 'epaule_a_levier_sous_garde': case 'epaule_a_verrou': case 'epaule_semi_auto_style_chasse':
if (gunLength < 75 || gunBarrelLength < 40) { return 'B' }
if (gunLength > 75 && gunBarrelLength > 55) { return 'C' }
}
}
return TYPOLOGIES[typology]?.category
}

watchEffect(() => {
if (!resultStore.img) router.push({ name: 'StartPage' })
})

const confidence = computed(() => resultStore.confidence)
const confidenceLevel = computed(() => resultStore.confidenceLevel)
const img = computed(() => resultStore.img)
const imgUrl = computed(() => resultStore.imgUrl)

const typology = computed(() => resultStore.typology)
const typology = TYPOLOGIES[resultStore.typology]

const isDummy = computed(() => stepsStore.isDummy)
const isDummyTypology = computed(() => TYPOLOGIES[typology.value]?.isDummyTypology === true)
const isDummyTypology = computed(() => typology?.isDummyTypology === true)
const isCardDetected = computed(() => resultStore.gunLength !== null && resultStore.gunBarrelLength !== null)

const isUp = ref(false)
const isDown = ref(false)
const isFeedbackDone = ref(false)

const label = computed(() => TYPOLOGIES[typology.value]?.displayLabel)
const label = computed(() => typology?.displayLabel)

const category = computed(() => {
if (stepsStore.selectedAlarmGun && stepsStore.selectedAlarmGun !== '') {
return 'D'
} else if (isDummy.value) {
return 'Non Classée'
} else if (typology.value === 'revolver') {
return TYPOLOGIES[typology.value]?.categoryWithoutSecuring
} else if (typology.displayLabel === 'Revolver') {
return typology?.categoryWithoutSecuring
} else {
return getCategoryFromTypologyAndMeasures(typology.value, resultStore.gunLength, resultStore.gunBarrelLength)
return typology?.getCategory(resultStore.gunLength, resultStore.gunBarrelLength)
}
})

const disclaimer = computed(() => TYPOLOGIES[typology.value] && Object.hasOwn(TYPOLOGIES[typology.value], 'getDisclaimer') ? TYPOLOGIES[typology.value].getDisclaimer(category.value, isCardDetected.value) : null)
const disclaimer = computed(() => typology && Object.hasOwn(typology, 'getDisclaimer') ? typology.getDisclaimer(category.value, isCardDetected.value) : null)

function sendFeedback (isCorrect: boolean) {
const json = {
image_url: imgUrl.value,
image_url: resultStore.imgUrl,
feedback: isCorrect,
confidence: confidence.value,
label: typology.value,
Expand Down Expand Up @@ -98,7 +75,7 @@ function sendFeedback (isCorrect: boolean) {
<div class="result-frame -mx-8 py-5 px-8">
<div class="result">
<h2
v-if="route.name === 'IdentificationTypologyResult' && isDummyTypology === true"
v-if="$route.name === 'IdentificationTypologyResult' && isDummyTypology === true"
class="typology-title bg-white py-4"
>
Typologie de l'arme
Expand Down Expand Up @@ -167,7 +144,7 @@ function sendFeedback (isCorrect: boolean) {
>
{{ label }}
</h3>
<template v-if="confidenceLevel !== 'low' && (route.name !== 'IdentificationTypologyResult' || isDummyTypology !== true)">
<template v-if="confidenceLevel !== 'low' && ($route.name !== 'IdentificationTypologyResult' || isDummyTypology !== true)">
<h3
class="fr-alert__title"
data-testid="arm-category"
Expand All @@ -180,14 +157,14 @@ function sendFeedback (isCorrect: boolean) {
</template>
</div>
<div
v-if="disclaimer && confidenceLevel !== 'low' && (route.name !== 'IdentificationTypologyResult' || isDummyTypology !== true)"
v-if="disclaimer && confidenceLevel !== 'low' && ($route.name !== 'IdentificationTypologyResult' || isDummyTypology !== true)"
class="fr-alert fr-alert--warning"
>
<p v-html="disclaimer" />
</div>
<MissingCardAlert v-if="MEASURED_GUNS_TYPOLOGIES.includes(typology) && isCardDetected === false && isDummy == false" />
<div
v-if="confidenceLevel !== 'low' && (route.name !== 'IdentificationTypologyResult' || isDummyTypology !== true)"
v-if="confidenceLevel !== 'low' && ($route.name !== 'IdentificationTypologyResult' || isDummyTypology !== true)"
class="fr-callout mt-3"
>
<p class="fr-callout__text">
Expand All @@ -201,7 +178,7 @@ function sendFeedback (isCorrect: boolean) {
</p>
</div>
<div
v-if="confidenceLevel !== 'low' && route.name === 'IdentificationTypologyResult' && isDummyTypology"
v-if="confidenceLevel !== 'low' && $route.name === 'IdentificationTypologyResult' && isDummyTypology"
class="fr-alert fr-alert--warning"
>
<p>
Expand All @@ -222,7 +199,7 @@ function sendFeedback (isCorrect: boolean) {
<SnackbarAlert class="text-center" />
</div>
<div
v-if="confidenceLevel !== 'low' && route.name !== 'IdentificationFinalResult'"
v-if="confidenceLevel !== 'low' && $route.name !== 'IdentificationFinalResult'"
:aria-disabled="isFeedbackDone"
class="feedback"
>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/firearms-utils/arme-alarme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import pistoletAlarmeImg13Zoom from '@/assets/guide-identification/photos/arme_a

export const arme_alarme = {
displayLabel: 'Arme d\'alarme',
category: 'D',
getCategory: () => 'D',
options: [
{
label: 'ZORAKI R2',
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/firearms-utils/autre-pistolet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Autre pistolet
export const autre_pistolet = {
typologie: 'autre_pistolet',
displayLabel: 'Pistolet divers',
category: 'A, B ou D',
getCategory: () => 'A, B ou D',
isDummyTypology: false,
options_text: '',
options: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import levierSousGardeMagasinAvantImg from '@/assets/guide-mise-en-securite/phot
import levierSousGardePasMagasinImg from '@/assets/guide-mise-en-securite/photos/epaule_a_levier_sous_garde/epaule_levier_ss_garde_magasin_fixe.jpg'
import levierSousGardeMagasinAvantVideo from '@/assets/guide-mise-en-securite/videos/epaule_a_levier_sous_garde/epaule_levier_ss_garde_magasin_avant.mp4'
import levierSousGardePasMagasinVideo from '@/assets/guide-mise-en-securite/videos/epaule_a_levier_sous_garde/epaule_levier_ss_garde_magasin_fixe.mp4'
import { getEpaulLevierVerrouDisclaimer } from '@/utils/firearms-utils/index'
import { getEpaulLevierVerrouDisclaimer, getCommonCategory } from '@/utils/firearms-utils/index'
/*
Armes d’épaule à levier de sous-garde
Magasin avant
Magasin fixe
*/
export const epaule_a_levier_sous_garde = {
displayLabel: "Arme d'épaule à levier de sous-garde",
category: 'B ou C',
getCategory: getCommonCategory,
isDummyTypology: false,
options_text: 'En manipulant l’arme avec précaution dans une <span class="font-bold">direction sans risque</span>, inspectez l’<span class="font-bold">extrémité du tube sous le canon</span>. Sélectionnez ce que vous voyez :',
options: {
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/utils/firearms-utils/epaule-a-pompe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import epaulePompeSimpleVideo from '@/assets/guide-mise-en-securite/videos/epaul
// import epaulePompeCompliqueVideo from '@/assets/guide-mise-en-securite/videos/epaule_a_pompe/epaule_pompe_complique.mp4'
/*
3 - Armes d’épaule à pompe

*/
export const epaule_a_pompe = {
displayLabel: "Arme d'épaule à pompe",
category: 'B ou C',
getCategory: (gunLength: number, gunBarrelLength: number) => {
if (gunLength && gunBarrelLength) { return gunLength > 75 && gunBarrelLength > 55 ? 'C' : 'B' } else { return 'B ou C' }
},
isDummyTypology: false,
options_text: '',
text_steps: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Armes d’épaule à un coup par canon
*/
export const epaule_a_un_coup_par_canon = {
displayLabel: "Arme d'épaule à un coup par canon",
category: 'B ou C',
getCategory: (gunLength: number, gunBarrelLength: number) => {
if (gunLength && gunBarrelLength) { return (gunLength > 75 && gunBarrelLength > 40) ? 'C' : 'B' } else { return 'B ou C' }
},
isDummyTypology: false,
options_text: 'Sélectionnez ce que vous voyez sur la <span class="font-bold">zone centrale de l’arme</span>',
options: {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/utils/firearms-utils/epaule-a-verrou.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import epauleAVerrouCartridges from '@/assets/guide-identification/photos/epaule_a_verrou/epaule_a_verrou_chargeur_cartouche.jpg'
import epauleAVerrouBalls from '@/assets/guide-identification/photos/epaule_a_verrou/epaule_a_verrou_chargeur_bille.jpg'
import epauleAVerrouVideo from '@/assets/guide-mise-en-securite/videos/epaule_a_verrou/epaule_verrou_chargeur-video.mp4'
import { getEpaulLevierVerrouDisclaimer } from '@/utils/firearms-utils/index'
import { getEpaulLevierVerrouDisclaimer, getCommonCategory } from '@/utils/firearms-utils/index'

/*
Armes d’épaule à verrou
*/
export const epaule_a_verrou = {
displayLabel: 'Arme d’épaule à verrou',
category: 'B ou C',
getCategory: getCommonCategory,
isDummyTypology: true,
text_steps: {
1: 'Observer l’arme en l’orientant dans une <span class="font-bold">direction sans risque</span>, en manipulant avec précaution',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Armes d’épaule semi-automatiques ou automatiques type militaire milieu 20e
*/
export const epaule_semi_auto_style_militaire_milieu_20e = {
displayLabel: 'Arme d’épaule semi-automatique ou automatique',
category: 'A ou B',
getCategory: () => 'A ou B',
isDummyTypology: false,
getDisclaimer: () => "<strong>Catégorie A</strong> si à l'origine l’arme était à <strong>répétition automatique</strong> puis a été <strong>transformée</strong> en arme <strong>semi automatique</strong>, ou si l’arme possède <strong>une crosse rétractable / pliable</strong> et qu’en configuration la plus courte elle <strong>mesure moins de 60 cm</strong>.",
} as const
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getCommonCategory } from '@/utils/firearms-utils/index'

const DISCLAIMERS = {
short: '<strong>Catégorie B</strong> si la <strong>capacité > à 3 munitions</strong>, ou si le <strong>chargeur est amovible</strong>.',
long: '<ul><li><strong>Catégorie B</strong> si la <strong>capacité > à 3 munitions</strong>, ou si le <strong>canon est lisse</strong>, ou si le <strong>chargeur est amovible</strong>.</li><li><strong>Catégorie C</strong> si le <strong>canon est rayé</strong>.</li></ul>',
Expand All @@ -8,7 +10,7 @@ Armes d’épaule semi-automatiques ou automatiques type chasse
*/
export const epaule_semi_auto_style_chasse = {
displayLabel: 'Arme d’épaule semi-automatique',
category: 'B ou C',
getCategory: getCommonCategory,
isDummyTypology: false,
getDisclaimer: (category: string, isCardDetected: boolean) => {
if (['B ou C', 'C'].includes(category)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Armes d’épaule à mécanisme ancien
*/
export const epaule_mecanisme_ancien = {
displayLabel: "Arme d'épaule à mécanisme ancien",
category: 'D',
getCategory: () => 'D',
isDummyTypology: false,
} as const
8 changes: 8 additions & 0 deletions frontend/src/utils/firearms-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ export function getEpaulLevierVerrouDisclaimer (category: string, isCardDetected
return '<ul><li><strong>Catégorie B</strong> : si la capacité > 11 munitions ou si le <strong>canon est lisse</strong></li><li><strong>Catégorie C</strong> : si la capacité < 11 munitions et le <strong>canon est rayé</strong></li></ul>'
}
}

export function getCommonCategory (gunLength: number, gunBarrelLength: number) {
if (gunLength && gunBarrelLength) {
if (gunLength < 75 || gunBarrelLength < 40) { return 'B' }
if (gunLength > 75 && gunBarrelLength > 55) { return 'C' }
}
return 'B ou C'
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Pistolet à mécanisme ancien
*/
export const pistolet_mecanisme_ancien = {
displayLabel: 'Pistolet à mécanisme ancien',
category: 'D',
getCategory: () => 'D',
isDummyTypology: false,
} as const
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pistolets semi-auto modernes
*/
export const pistolet_semi_auto_moderne = {
displayLabel: 'Pistolet semi-automatique',
category: 'B',
getCategory: () => 'B',
isDummyTypology: true,
pistolet_semi_auto_moderne_text_option: 'Sélectionner ce que vous voyez sur votre arme : <span class="font-bold">bouton à proximité du pontet du côté gauche de la poignée</span>, OU <span class="font-bold">bouton sur le talon</span> de la crosse.',
textOptions: '',
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/utils/firearms-utils/revolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Revolvers
*/
export const revolver = {
displayLabel: 'Revolver',
category: 'B',
getCategory: () => 'B',
categoryWithoutSecuring: 'B ou D',
isDummyTypology: true,
options_step_1_text: 'En maintenant l’arme dans une <span class="font-bold">direction sécurisée</span> , sélectionnez ce que vous voyez.',
Expand Down Expand Up @@ -170,6 +170,6 @@ export const revolver = {

export const revolver_black_powder = {
displayLabel: 'Revolver à poudre noire',
category: 'D',
getCategory: () => 'D',
isDummyTypology: false,
} as const
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Armes d’épaule semi-automatiques ou automatiques type militaire moderne
*/
export const semi_auto_style_militaire_autre = {
displayLabel: 'Arme semi-automatique ou automatique',
category: 'A ou B',
getCategory: () => 'A ou B',
isDummyTypology: true,
options_text: 'Observez la position du chargeur et sélectionnez ce que vous voyez :',
options: {
Expand Down
Loading