question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactor components to use dateRange via datastore

See original GitHub issue

Feature Description

Once the datastore foundation is in place in #1529, the existing DateRangeSelector should be refactored to use the datastore for its state, rather than WP hooks


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • DateRangeSelector should use datastore state internally instead of the current filter-based implementation
  • All other components/functions which consume/reference dateRange state (e.g. dataAPI) must be updated to source the value from the datastore, without using @wordpress/hooks

Implementation Brief

Legacy non-functional components that can’t be rewritten as functional components will need to be updated to be wrapped with withSelect and withDispatch as needed which provide registry selected data or dispatch functions to the consuming components as props.
This can be done by wrapping the component before it’s exported.

Update DateRangeSelector

should be reimplemented as functional component

Displaying all options Rather than defining all options internally, it can get them via the new getAvailableDateRanges utility

Selecting a new date range Replace the current hook with a dispatch call when a new range is selected https://github.com/google/site-kit-wp/blob/09a45d7c0f2a8af533335d8d7076e434f04051b5/assets/js/components/date-range-selector.js#L77-L84

  • Replace with useDispatch(...).setDateRange( slug )

Triggering new data to be fetched Currently this is done like so https://github.com/google/site-kit-wp/blob/09a45d7c0f2a8af533335d8d7076e434f04051b5/assets/js/components/date-range-selector.js#L86-L88

This can be done with useEffect( () => {...}, [ dateRange ] ) to run every time the date range changes

The component will still need to use WordPress hooks for this until we’re no longer using the legacy dataAPI.

dataAPI and utils

After all other components are refactored to use date range state from the datastore, dataAPI will be all that’s left, using it via these utilities

  • getCurrentDateRange
  • getCurrentDateRangeSlug

These can be updated to use slug = Data.select( 'core/user' ).getDateRange() directly in place of the filters. The date range label is available via getAvailableDateRanges( slug ).label

Remaining components

  • AnalyticsDashboardWidgetTopAcquisitionSources
    • clean refactor to functional component with useSelect as it only has a render method
  • AnalyticsDashboardWidget
    • refactor with data HOCs to provide dateRange
  • GoogleSitekitSearchConsoleDashboardWidget
    • refactor with data HOCs to provide dateRange

QA Brief

Changelog entry

  • Migrate the existing date range selector component to rely on the centrally managed date range from datastore.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
aaemnnosttvcommented, Jun 9, 2020

Can you update the function names in the IB? After that, it’s good from my end.

@felixarntz updated! I removed the part about initial state as well since that will be handled in #1529 anyways.

Assuming this is IB ✅ - moving to backlog 👍

1reaction
felixarntzcommented, Jun 8, 2020

Right, but that means we don’t need to handle this here, since getDateRange would never return undefined right?

Can you update the function names in the IB? After that, it’s good from my end.

Read more comments on GitHub >

github_iconTop Results From Across the Web

32 Using More Complex Databound ADF Faces Components
This chapter describes how to add the ADF Faces Calendar and Carousel components to your pages in the Fusion web application.
Read more >
How to refactor React components to use Hooks
Refactoring your existing application or components to use React Hooks comes with its own unique set of challenges. In this article, we'll ...
Read more >
Record Type Object - Appian 20.3
User filters offer the ability to filter subsets of record data by selecting options in a list or using a date range style...
Read more >
How to Refactor Large React Components (The Story of a ...
It's simple to split our massive component to one-third of its current size just by putting things where they should be! An example:...
Read more >
Date Range | Retool Component Library
It supports start and end values, and minimum and maximum dates. Date Range can display values using different time zones and adjust for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found