Skip to content

Commit

Permalink
include validation for rating
Browse files Browse the repository at this point in the history
  • Loading branch information
leiyre committed Nov 7, 2024
1 parent f657b31 commit fc8e541
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/>
<DatasetConfigurationRating
v-else-if="question.settings.type.isRatingType"
v-model="question.settings.options"
:question="question"
@is-focused="$emit('is-focused', $event)"
/>
<DatasetConfigurationRanking
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,80 @@
<template>
<div class="dataset-config-rating__input-container">
<input
type="number"
min="0"
max="10"
step="1"
:value="value.length ? value.length - 1 : 0"
@input="onInput($event.target.value)"
@focus="$emit('is-focused', true)"
@blur="$emit('is-focused', false)"
:placeholder="placeholder"
class="dataset-config-rating__input"
/>
<div>
<div
:class="{ '--error': errors.length }"
class="dataset-config-rating__input-container"
>
<input
type="number"
min="0"
max="10"
step="1"
:value="
question.settings.options.length
? question.settings.options.length - 1
: 0
"
@input="onInput($event.target.value)"
@focus.stop="onFocus"
@blur="onBlur"
:placeholder="placeholder"
class="dataset-config-rating__input"
/>
</div>
<Validation v-if="errors.length" :validations="translatedValidations" />
</div>
</template>

<script>
export default {
data() {
return {
errors: [],
isDirty: false,
};
},
props: {
value: {
type: Array,
question: {
type: Object,
required: true,
},
placeholder: {
type: String,
default: "",
},
},
model: {
prop: "value",
event: "on-value-change",
computed: {
translatedValidations() {
return this.errors.map((validation) => {
return this.$t(validation);
});
},
},
methods: {
validateOptions() {
this.errors = this.question.validate();
},
onFocus() {
this.$emit("is-focused", true);
},
onBlur() {
this.isDirty = true;
this.validateOptions();
this.$emit("is-focused", false);
},
onInput(inputValue) {
let value = parseInt(inputValue);
value = Math.max(0, Math.min(value, 10));
value = Math.max(1, Math.min(value, 10));
const valuesArray = Array.from({ length: value + 1 }, (_, i) => ({
value: i,
}));
this.$emit("on-value-change", valuesArray);
this.question.settings.options = valuesArray;
if (this.isDirty) {
this.validateOptions();
}
},
},
};
Expand Down
3 changes: 3 additions & 0 deletions argilla-frontend/translation/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ export default {
atLeastTwoOptions: "At least two options are required",
optionsWithoutLabel: "Empty options are not allowed",
},
rating: {
atLeastTwoOptions: "At least two options are required",
},
},
atLeastOneQuestion: "At least one question is required.",
atLeastOneRequired: "At least one required question is needed.",
Expand Down
3 changes: 3 additions & 0 deletions argilla-frontend/translation/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ export default {
atLeastTwoOptions: "Se requieren al menos dos opciones",
optionsWithoutLabel: "No se permiten opciones vacías",
},
rating: {
atLeastTwoOptions: "Se requieren al menos dos opciones",
},
},
atLeastOneQuestion: "Se requiere al menos una pregunta.",
atLeastOneRequired: "Se requiere al menos una pregunta obligatoria.",
Expand Down
5 changes: 5 additions & 0 deletions argilla-frontend/v1/domain/entities/hub/QuestionCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ export class QuestionCreation {
);
}
}
if (this.isRatingType) {
if (this.options.length < 2) {
errors.push("datasetCreation.questions.rating.atLeastTwoOptions");
}
}

return errors;
}
Expand Down

0 comments on commit fc8e541

Please sign in to comment.