Skip to content

Conversation

@mocca102
Copy link
Contributor

@mocca102 mocca102 commented May 5, 2025

Pull Request Checklist

Before you submit a pull request, please make sure you have to following:

  • I have added or updated TypeScript types for my changes, ensuring they are compatible with the existing codebase.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added any necessary documentation (if appropriate).

PR Type

What kind of change does this PR introduce?

  • Feature

What are the changes?

  • Support updating the rendered quiz result data (image, price, ids) based on the selected variation swatch.
  • Send the selected variation_id with tracking events

How?

hooks/useResult.tsx

  • Responsible for returning the correct item data based on the currently selected variation
  • Look up the docs page for useResult for detailed documentation

Usage scenarios

Component

  • Supported OOB with ResultCardSwatches component

RenderProps

  • getQuizResultSwatchProps
    • Spread on the swatch component, and it works OOB
    • Updated the CioQuiz usage example with getQuizResultSwatchProps

Hooks

  • The user will need to utilize the useResult hook
  • Look up the docs page for useResult for detailed documentation

Before and After

  • Screenshot 2025-05-06 at 7 47 17 PM
  • Screenshot 2025-05-06 at 7 46 05 PM

@mocca102 mocca102 requested a review from a team as a code owner May 5, 2025 22:38
@mocca102 mocca102 removed the request for review from a team May 5, 2025 22:40
@mocca102 mocca102 force-pushed the ci-4249-os-ui-quizzes-fix-incorrect-variation_id-being-sent-with branch from 9fb0bb6 to 577ef88 Compare May 5, 2025 22:59
@mocca102 mocca102 force-pushed the ci-4249-os-ui-quizzes-fix-incorrect-variation_id-being-sent-with branch from 534feb7 to c0cfabc Compare May 6, 2025 13:25
@mocca102 mocca102 requested a review from a team May 6, 2025 21:09
@mocca102 mocca102 removed the request for review from a team May 7, 2025 12:29
@mocca102 mocca102 requested a review from a team May 7, 2025 13:21
Copy link
Contributor

@Mudaafi Mudaafi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First review, I didn't take a look at the tests in-depth yet, but at a glance they look good. Implementation looks good as well, I just had a couple of questions/comments first

import { useCallback, useState } from 'react';
import { GetQuizResultSwatchProps, QuizResultDataPartial } from '../types';

const useResult = (result: QuizResultDataPartial, swatchImageKey?: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we name this useResultCard instead to match the component it's tied to? I got confused with the Results component a couple of times but I might just be slow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getNestedValueUsingDotNotation(result?.data, salePriceKey)
);
const { faceOutResult, getQuizResultSwatchProps } = useResult(result, swatchImageKey);
const { data, value } = faceOutResult || {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the defensive programming. Just curious, is there ever a case where faceOutResult would be null? From the types, it doesn't seem to be something we'd expect to happen.

Comment on lines 23 to 30
const style = {
background: `url(${
swatchImageKey ? variation?.data?.[swatchImageKey] : variation?.data?.image_url
})`,
backgroundSize: 'fit-object',
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than this being a style we inject into the ResultCardsSwatches later, would it be better to just return a custom class like cio-result-card-swatch .cio-result-card-swatch-selected?

I understand background can't be a static css and needs to be computed, so we can keep it as a style, but I'd also want to return the swatchImage as a separate prop itself so that custom renderFunctions can make use of it w/o needing to rely on background or navigate into the result object to retrieve it. wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we can keep it as a style

Alternatively, the component can do the setting of the background style if we already return the image as a prop

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import { QuizResultDataPartial } from '../../types';
import { QuizResultDataPartial, RenderResultCard } from '../../types';
import { getNestedValueUsingDotNotation, validateNumberOrString } from '../../utils';
import ResultCardSwatches from '../ResultCardSwatches/ResultCardSwatches';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change required, but it would be nice if we had an index.ts export file structure. We use this pattern throughout the repo so let's keep it as is until we have the time to refactor

import { Meta, Markdown } from '@storybook/blocks';
import UseResultDocs from './markdown/UseResultDocs.md';

<Meta title="Quiz/useResult" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts? Felt a little weird having it just dangling there

Suggested change
<Meta title="Quiz/useResult" />
<Meta title="Quiz/Other Hooks/useResult" />

Copy link
Contributor Author

@mocca102 mocca102 Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@esezen esezen requested a review from Copilot June 20, 2025 14:12
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces support for variation-based quiz results, allowing updated content (image, price, IDs) based on the selected variation swatch and enabling tracking with the selected variation_id.

  • Added new hook (useResult) and TypeScript types to manage variation selection.
  • Updated components (ResultCard, ResultCardSwatches, Results, etc.), documentation, and tests to support the new swatch behavior.

Reviewed Changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/types.ts Added new types and updated options to include swatch-related props.
src/styles.css Introduced CSS classes for quiz swatches and selected states.
src/stories/Quiz/** Updated documentation and usage examples for useResult and render props.
src/services/index.ts Adjusted tracking event to correctly read variation_id.
src/hooks/useResult.tsx Created new hook managing variations and swatch props with minor styling.
src/components/** Updated ResultCard and related components to use new hook and props.
spec/** Extended tests to cover the new variation swatch behaviors and hook usage.
.github/workflows/spell-check.yml Updated Node.js version for spell-checking.

mocca102 and others added 7 commits September 30, 2025 13:52
Co-authored-by: Ahmad Mudaafi' <ahmad.mudaafi@constructor.io>
Co-authored-by: Ahmad Mudaafi' <ahmad.mudaafi@constructor.io>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Ahmad Mudaafi' <ahmad.mudaafi@constructor.io>
Co-authored-by: Ahmad Mudaafi' <ahmad.mudaafi@constructor.io>
@mocca102 mocca102 requested a review from Mudaafi September 30, 2025 11:23
Copy link
Contributor

@Mudaafi Mudaafi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants