Skip to content

Commit

Permalink
fix: Make rating start at 0 for surveys (#18011)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar authored Oct 17, 2023
1 parent e129687 commit fd230b1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cypress/e2e/surveys.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('Surveys', () => {

cy.get('[id="scenes.surveys.surveyLogic.new.survey.questions.0.scale"]')
.invoke('html')
.should('include', '1 - 10')
.should('include', '0 - 10')

cy.get('[id="scenes.surveys.surveyLogic.new.survey.questions.0.upperBoundLabel"]').should(
'have.value',
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/scenes/surveys/EditSurveyNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ export default function EditSurveyNew(): JSX.Element {
'number'
? [
{
label: '1 - 10',
label: '0 - 10',
value: 10,
},
]
Expand Down
32 changes: 18 additions & 14 deletions frontend/src/scenes/surveys/SurveyAppearance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,27 +365,31 @@ const NumberRating = ({
preview?: boolean
}): JSX.Element => {
const [activeNumber, setActiveNumber] = useState<number | undefined>()

const totalNumbers = ratingSurveyQuestion.scale === 10 ? 11 : ratingSurveyQuestion.scale
return (
<div
style={{
border: `1.5px solid ${appearance.borderColor || defaultSurveyAppearance.borderColor}`,
gridTemplateColumns: `repeat(${ratingSurveyQuestion.scale}, minmax(0, 1fr))`,
gridTemplateColumns: `repeat(${totalNumbers}, minmax(0, 1fr))`,
}}
className={`rating-options-buttons ${ratingSurveyQuestion.scale === 5 ? '' : 'max-numbers'}`}
>
{(ratingSurveyQuestion.scale === 5 ? [1, 2, 3, 4, 5] : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).map((num, idx) => {
const active = activeNumber === num
return (
<RatingButton
preview={preview}
key={idx}
active={active}
appearance={appearance}
num={num}
setActiveNumber={setActiveNumber}
/>
)
})}
{(ratingSurveyQuestion.scale === 10 ? [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] : [1, 2, 3, 4, 5]).map(
(num, idx) => {
const active = activeNumber === num
return (
<RatingButton
preview={preview}
key={idx}
active={active}
appearance={appearance}
num={num}
setActiveNumber={setActiveNumber}
/>
)
}
)}
</div>
)
}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/scenes/surveys/surveyLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ export const surveyLogic = kea<surveyLogicType>([
const { results } = responseJSON

let total = 0
const data = new Array(question.scale).fill(0)
const dataSize = question.scale === 10 ? 11 : question.scale
const data = new Array(dataSize).fill(0)
results?.forEach(([value, count]) => {
total += count
data[value - 1] = count
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/scenes/surveys/surveyViewViz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ export function RatingQuestionBarChart({
<></>
) : (
<div className="mb-8">
<div className="font-semibold text-muted-alt">{`1-${question.scale} rating`}</div>
<div className="font-semibold text-muted-alt">{`${
question.scale === 10 ? '0 - 10' : '1 - 5'
} rating`}</div>
<div className="text-xl font-bold mb-2">{question.question}</div>
<div className=" h-50 border rounded pt-6 pb-2 px-2">
<div className="relative h-full w-full">
Expand Down Expand Up @@ -216,9 +218,11 @@ export function RatingQuestionBarChart({
hoverBackgroundColor: barColor,
},
]}
labels={Array.from({ length: question.scale }, (_, i) => (i + 1).toString()).map(
(n) => n
)}
labels={
question.scale === 10
? ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
: ['1', '2', '3', '4', '5']
}
/>
</BindLogic>
</div>
Expand Down

0 comments on commit fd230b1

Please sign in to comment.