Skip to content

Commit

Permalink
refactor: Hide setFilters function inside urlToAction
Browse files Browse the repository at this point in the history
This more closely explains what's this function purpose, and it also allows us to heavily simplify our types
  • Loading branch information
rafaeelaudibert committed Jan 8, 2025
1 parent 27caacf commit c0910c3
Showing 1 changed file with 74 additions and 77 deletions.
151 changes: 74 additions & 77 deletions frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1857,10 +1857,80 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
}
}),

urlToAction(({ actions, values }) => ({
'/web': (pathParams, queryParams) => setFilters(actions, values, pathParams, queryParams),
'/web/:productTab': (pathParams, queryParams) => setFilters(actions, values, pathParams, queryParams),
})),
urlToAction(({ actions, values }) => {
const toAction = (
{ productTab = ProductTab.ANALYTICS }: { productTab?: ProductTab },
{
filters,
'conversionGoal.actionId': conversionGoalActionId,
'conversionGoal.customEventName': conversionGoalCustomEventName,
date_from,
date_to,
interval,
device_tab,
source_tab,
graphs_tab,
path_tab,
geography_tab,
path_cleaning,
filter_test_accounts,
compare_filter,
}: Record<string, any>
): void => {
const parsedFilters = isWebAnalyticsPropertyFilters(filters) ? filters : undefined

if (parsedFilters && !objectsEqual(parsedFilters, values.webAnalyticsFilters)) {
actions.setWebAnalyticsFilters(parsedFilters)
}
if (
conversionGoalActionId &&
conversionGoalActionId !== (values.conversionGoal as ActionConversionGoal)?.actionId
) {
actions.setConversionGoal({ actionId: parseInt(conversionGoalActionId, 10) })
} else if (
conversionGoalCustomEventName &&
conversionGoalCustomEventName !== (values.conversionGoal as CustomEventConversionGoal)?.customEventName
) {
actions.setConversionGoal({ customEventName: conversionGoalCustomEventName })
}
if (
(date_from && date_from !== values.dateFilter.dateFrom) ||
(date_to && date_to !== values.dateFilter.dateTo) ||
(interval && interval !== values.dateFilter.interval)
) {
actions.setDatesAndInterval(date_from, date_to, interval)
}
if (device_tab && device_tab !== values._deviceTab) {
actions.setDeviceTab(device_tab)
}
if (source_tab && source_tab !== values._sourceTab) {
actions.setSourceTab(source_tab)
}
if (graphs_tab && graphs_tab !== values._graphsTab) {
actions.setGraphsTab(graphs_tab)
}
if (path_tab && path_tab !== values._pathTab) {
actions.setPathTab(path_tab)
}
if (geography_tab && geography_tab !== values._geographyTab) {
actions.setGeographyTab(geography_tab)
}
if (path_cleaning && path_cleaning !== values.isPathCleaningEnabled) {
actions.setIsPathCleaningEnabled([true, 'true', 1, '1'].includes(path_cleaning))
}
if (filter_test_accounts && filter_test_accounts !== values.shouldFilterTestAccounts) {
actions.setShouldFilterTestAccounts([true, 'true', 1, '1'].includes(filter_test_accounts))
}
if (compare_filter && !objectsEqual(compare_filter, values.compareFilter)) {
actions.setCompareFilter(compare_filter)
}
if (productTab && productTab !== values.productTab) {
actions.setProductTab(productTab)
}
}

return { '/web': toAction, '/web/:productTab': toAction }
}),

listeners(({ values, actions }) => {
const checkGraphsTabIsCompatibleWithConversionGoal = (
Expand Down Expand Up @@ -1939,76 +2009,3 @@ const checkCustomEventConversionGoalHasSessionIdsHelper = async (
setConversionGoalWarning(null)
}
}

const setFilters = (
actions: webAnalyticsLogicType['actions'],
values: webAnalyticsLogicType['values'],
{ productTab = ProductTab.ANALYTICS }: { productTab?: ProductTab },
{
filters,
'conversionGoal.actionId': conversionGoalActionId,
'conversionGoal.customEventName': conversionGoalCustomEventName,
date_from,
date_to,
interval,
device_tab,
source_tab,
graphs_tab,
path_tab,
geography_tab,
path_cleaning,
filter_test_accounts,
compare_filter,
}: Record<string, any>
): void => {
const parsedFilters = isWebAnalyticsPropertyFilters(filters) ? filters : undefined

if (parsedFilters && !objectsEqual(parsedFilters, values.webAnalyticsFilters)) {
actions.setWebAnalyticsFilters(parsedFilters)
}
if (
conversionGoalActionId &&
conversionGoalActionId !== (values.conversionGoal as ActionConversionGoal)?.actionId
) {
actions.setConversionGoal({ actionId: parseInt(conversionGoalActionId, 10) })
} else if (
conversionGoalCustomEventName &&
conversionGoalCustomEventName !== (values.conversionGoal as CustomEventConversionGoal)?.customEventName
) {
actions.setConversionGoal({ customEventName: conversionGoalCustomEventName })
}
if (
(date_from && date_from !== values.dateFilter.dateFrom) ||
(date_to && date_to !== values.dateFilter.dateTo) ||
(interval && interval !== values.dateFilter.interval)
) {
actions.setDatesAndInterval(date_from, date_to, interval)
}
if (device_tab && device_tab !== values._deviceTab) {
actions.setDeviceTab(device_tab)
}
if (source_tab && source_tab !== values._sourceTab) {
actions.setSourceTab(source_tab)
}
if (graphs_tab && graphs_tab !== values._graphsTab) {
actions.setGraphsTab(graphs_tab)
}
if (path_tab && path_tab !== values._pathTab) {
actions.setPathTab(path_tab)
}
if (geography_tab && geography_tab !== values._geographyTab) {
actions.setGeographyTab(geography_tab)
}
if (path_cleaning && path_cleaning !== values.isPathCleaningEnabled) {
actions.setIsPathCleaningEnabled([true, 'true', 1, '1'].includes(path_cleaning))
}
if (filter_test_accounts && filter_test_accounts !== values.shouldFilterTestAccounts) {
actions.setShouldFilterTestAccounts([true, 'true', 1, '1'].includes(filter_test_accounts))
}
if (compare_filter && !objectsEqual(compare_filter, values.compareFilter)) {
actions.setCompareFilter(compare_filter)
}
if (productTab && productTab !== values.productTab) {
actions.setProductTab(productTab)
}
}

0 comments on commit c0910c3

Please sign in to comment.