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

Fixing Duration Style for Desktop View #36

Merged
merged 2 commits into from
Feb 18, 2025
Merged

Conversation

ahmadtaimoor-deriv
Copy link
Contributor

@ahmadtaimoor-deriv ahmadtaimoor-deriv commented Feb 18, 2025

Summary by Sourcery

This pull request refactors the Duration field component to use the new DesktopTradeFieldCard component, and improves the UI of the tab list component.

Enhancements:

  • Improves the Duration field component by integrating it with the new DesktopTradeFieldCard component for a more consistent look and feel.
  • Enhances the tab list component to improve the UI, including changes to colors, fonts, and spacing.

Copy link

Deploying champion-trader with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0f15d65
Status:🚫  Build failed.

View logs

Copy link

sourcery-ai bot commented Feb 18, 2025

Reviewer's Guide by Sourcery

This pull request focuses on improving the styling and layout of the duration selection component, especially for desktop view. It introduces the use of DesktopTradeFieldCard to provide a consistent look and feel across the trade form. Additionally, it updates the styles of the TabList component and DesktopTradeFieldCard, and adds a background color to the MarketInfo component.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Refactor DurationField to use DesktopTradeFieldCard for desktop view and adjust DurationController layout.
  • Wrapped DurationField with DesktopTradeFieldCard component for desktop view.
  • Adjusted the position of the Popover component in DurationField.
  • Modified DurationController to remove DesktopTradeFieldCard wrapper and adjust width for desktop layout.
  • Removed unnecessary div in TradeFormController and adjusted the gap between fields.
src/screens/TradePage/components/TradeFormController.tsx
src/components/Duration/DurationField.tsx
src/components/Duration/DurationController.tsx
Update TabList component styles for vertical variant.
  • Modified the styles of the VerticalTabList component to improve appearance and alignment.
  • Added background color and adjusted padding and font styles.
src/components/ui/tab-list.tsx
Update DesktopTradeFieldCard styles and add tests.
  • Updated the styles of the DesktopTradeFieldCard component to use a background color and border.
  • Added tests for the DesktopTradeFieldCard component to ensure correct rendering and style application.
src/components/ui/__tests__/desktop-trade-field-card.test.tsx
src/components/ui/desktop-trade-field-card.tsx
Add background color to MarketInfo component.
  • Added a background color to the MarketInfo component for visual enhancement.
src/components/MarketInfo/MarketInfo.tsx

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Manifest Files

@ahmadtaimoor-deriv ahmadtaimoor-deriv changed the title F duration style Fixing Duration Style for Desktop View Feb 18, 2025
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ahmadtaimoor-deriv - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a visual cue to indicate when the DurationField is selected on desktop, similar to the StakeField.
  • The change to VerticalTabList introduces a hardcoded color; consider using a theme variable or Tailwind class for better maintainability.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 2 issues found
  • 🟡 Complexity: 2 issues found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -20,7 +20,7 @@ describe('DesktopTradeFieldCard', () => {
);

const card = container.firstChild as HTMLElement;
expect(card).toHaveClass('bg-white', 'rounded-lg', 'p-2');
expect(card).toHaveClass('rounded-lg', 'p-2','bg-[rgba(246,247,248,1)]','border','border-transparent');
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Test the isSelected style

Add a test case to verify the styles when the isSelected prop is true. This ensures the visual feedback for the selected state is correctly applied.

Suggested implementation:

  it('merges custom className with default styles', () => {
    // existing test code...
  });

  // New test to verify styles when isSelected is true
  it('applies isSelected style when isSelected prop is true', () => {
    // Render the component with the isSelected prop set to true.
    const { container } = render(<DesktopTradeFieldCard isSelected={true} />);
    const card = container.firstChild as HTMLElement;
    // Expect the card to have default styles plus the selected border style.
    expect(card).toHaveClass('rounded-lg', 'p-2', 'bg-[rgba(246,247,248,1)]', 'border', 'border-blue-500');
  });

Make sure that the component DesktopTradeFieldCard is imported in this test file if it isn't already. For example:

<<<<<<< SEARCH
// (No import of DesktopTradeFieldCard)

import DesktopTradeFieldCard from '../DesktopTradeFieldCard';

REPLACE

Also ensure that test utilities like render are imported from the React Testing Library.

@@ -31,6 +31,6 @@ describe('DesktopTradeFieldCard', () => {
);

const card = container.firstChild as HTMLElement;
expect(card).toHaveClass('bg-white p-2 rounded-lg');
expect(card).toHaveClass('bg-[rgba(246,247,248,1)] rounded-lg p-2 border border-transparent custom-class');
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Test the error style

It's important to also test the error styles. Add a test case where the error prop is true to ensure the visual feedback for errors is correctly displayed.

Suggested implementation:

  it('applies error styles when error prop is true', () => {
    const { container } = render(<DesktopTradeFieldCard error />);
    const card = container.firstChild as HTMLElement;
    expect(card).toHaveClass('bg-[rgba(246,247,248,1)]', 'rounded-lg', 'p-2', 'border', 'border-red-500');
  });
});

Make sure that the DesktopTradeFieldCard component applies "border-red-500" (or the correct error class for your design) when the error prop is true. Adjust expected class names in the test if your design uses different classes.


interface TradeFormControllerProps {
isLandscape: boolean;
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider extracting the repeated reduce logic and SSE event handlers into helper functions.

Consider extracting the repeated reduce logic and SSE event handlers into helper functions to reduce inline nesting. For example, you could create a helper to compute the payout mapping:

// helpers.ts
export const computePayoutValues = (
  buttonStates: Record<string, ButtonState>,
  targetAction: string,
  newPayout: number
): Record<string, number> =>
  Object.keys(buttonStates).reduce((acc, key) => {
    acc[key] = key === targetAction ? newPayout : buttonStates[key]?.payout || 0;
    return acc;
  }, {} as Record<string, number>);

Then in your component:

import { computePayoutValues } from "./helpers";

// Inside onMessage callback:
onMessage: (priceData) => {
  const payoutValue = Number(priceData.price);
  setButtonStates((prev) => ({
    ...prev,
    [button.actionName]: {
      loading: false,
      error: null,
      payout: payoutValue,
      reconnecting: false,
    },
  }));
  setPayouts({
    max: 50000,
    values: computePayoutValues(buttonStates, button.actionName, payoutValue),
  });
},

Similarly, consider moving repeated SSE handler logic into a helper or a custom hook to further reduce inline nesting while keeping functionality intact.

className={className}
/>

{isDesktop ? (
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider extracting the conditional wrapper of the "TradeParam" component to reduce JSX duplication.

Consider extracting the conditional wrapper into its own component or helper function so that you only render the <TradeParam> once. This reduces the duplicated props and branching in your JSX.

For example, you can create a simple wrapper component:

const ConditionalWrapper: React.FC<{ children: React.ReactNode }> = ({ children }) =>
  isDesktop ? <DesktopTradeFieldCard isSelected={isOpen}>{children}</DesktopTradeFieldCard> : <>{children}</>;

return (
  <div className="relative">
    <ConditionalWrapper>
      <TradeParam
        label="Duration"
        value={duration}
        onClick={handleClick}
        className={className}
      />
    </ConditionalWrapper>
    {isDesktop && isOpen && (
      <Popover
        isOpen={isOpen}
        onClose={handleClose}
        style={{
          position: "absolute",
          right: "100%",
          top: "-8px",
          marginRight: "16px",
        }}
      >
        <DurationController onClose={handleClose} />
      </Popover>
    )}
  </div>
);

This approach retains all functionality while reducing duplication and simplifying the rendering logic.

@ahmadtaimoor-deriv ahmadtaimoor-deriv merged commit f99be95 into master Feb 18, 2025
2 of 3 checks passed
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.

1 participant