Skip to content

Commit

Permalink
feat: Multi filters support (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagarnaikjuspay authored Jan 3, 2024
1 parent 543e074 commit fd1aa3d
Show file tree
Hide file tree
Showing 21 changed files with 1,542 additions and 768 deletions.
2 changes: 1 addition & 1 deletion src/components/DynamicChart.res
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ let make = (
}

let currentTheme = ThemeProvider.useTheme()
let {filterValue} = React.useContext(AnalyticsUrlUpdaterContext.urlUpdaterContext)
let {filterValue} = React.useContext(FilterContext.filterContext)
let (_switchToMobileView, setSwitchToMobileView) = React.useState(_ => false)
let (selectedTabState, setSelectedTabState) = React.useState(_ => selectedTab)

Expand Down
4 changes: 1 addition & 3 deletions src/components/DynamicFilter.res
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ let make = (
let defaultFilters = ""->Js.Json.string
let (showModal, setShowModal) = React.useState(_ => false)

let {updateExistingKeys, filterValue, removeKeys} = React.useContext(
AnalyticsUrlUpdaterContext.urlUpdaterContext,
)
let {updateExistingKeys, filterValue, removeKeys} = React.useContext(FilterContext.filterContext)

let currentCustomFilterValue =
filterValue->Js.Dict.get(customFilterKey)->Belt.Option.getWithDefault("")
Expand Down
3 changes: 2 additions & 1 deletion src/components/DynamicSingleStat.res
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ let make = (
~statThreshold=?,
~wrapperClass=?,
) => {
let {filterValueJson} = React.useContext(FilterContext.filterContext)
let fetchApi = AuthHooks.useApiFetcher()
let getAllFilter = UrlUtils.useGetFilterDictFromUrl("")
let getAllFilter = filterValueJson
let isMobileView = MatchMedia.useMobileChecker()
let (showStats, setShowStats) = React.useState(_ => false)

Expand Down
3 changes: 2 additions & 1 deletion src/components/DynamicTabs.res
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ let make = (
| None => _ => ()
}
let {addConfig, getConfig} = React.useContext(UserPrefContext.userPrefContext)
let getTabNames = UrlUtils.useGetFilterDictFromUrl(tabId)
let {filterValueJson} = React.useContext(FilterContext.filterContext)
let getTabNames = filterValueJson
let getTitle = key => {
(
tabs
Expand Down
39 changes: 21 additions & 18 deletions src/components/FilterUtils.res
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
let generateDefaultUrl = dict => {
dict
->Js.Dict.entries
->Belt.Array.keepMap(entry => {
let (key, val) = entry

let strValue = RemoteFiltersUtils.getStrFromJson(key, val)
if strValue !== "" {
Some(`${key}=${strValue}`)
} else {
None
}
let parseFilterString = queryString => {
queryString
->Js.Global.decodeURI
->Js.String2.split("&")
->Belt.Array.keepMap(str => {
let arr = str->Js.String2.split("=")
let key = arr->Belt.Array.get(0)->Belt.Option.getWithDefault("-")
let val = arr->Belt.Array.sliceToEnd(1)->Js.Array2.joinWith("=")
key === "" || val === "" ? None : Some((key, val))
})
->Js.Array2.joinWith("&")
->Js.Dict.fromArray
}

let updateURLWithDefaultFilter = (~path, ~filterParam, ~filterString) => {
if path->Js.String2.length > 0 && filterParam->Js.String2.length == 0 {
let finalUrl = `${path}?${filterString}`
RescriptReactRouter.replace(finalUrl)
}
let parseFilterDict = dict => {
let searchParam =
dict
->Js.Dict.entries
->Js.Array2.map(item => {
let (key, value) = item
`${key}=${value}`
})
->Js.Array2.joinWith("&")

searchParam
}
18 changes: 7 additions & 11 deletions src/components/RemoteFilter.res
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ module ClearFilters = {
~isCountRequired=true,
~outsidefilter=false,
) => {
let url = RescriptReactRouter.useUrl()
let {updateExistingKeys} = React.useContext(FilterContext.filterContext)
let isMobileView = MatchMedia.useMobileChecker()
let outerClass = if isMobileView {
"flex items-center justify-end"
Expand Down Expand Up @@ -160,8 +160,7 @@ module ClearFilters = {
})
->Js.Array2.joinWith("&")

let path = url.path->Belt.List.toArray->Js.Array2.joinWith("/")
RescriptReactRouter.replace(`/${path}?${searchStr}`)
searchStr->FilterUtils.parseFilterString->updateExistingKeys
}
}

Expand Down Expand Up @@ -197,7 +196,7 @@ module ClearFilters = {
module AnalyticsClearFilters = {
@react.component
let make = (~defaultFilterKeys=[], ~clearFilters=?, ~outsidefilter=false) => {
let url = RescriptReactRouter.useUrl()
let {updateExistingKeys} = React.useContext(FilterContext.filterContext)
let formState: ReactFinalForm.formState = ReactFinalForm.useFormState(
ReactFinalForm.useFormSubscription(["values", "initialValues"])->Js.Nullable.return,
)
Expand Down Expand Up @@ -231,8 +230,7 @@ module AnalyticsClearFilters = {
})
->Js.Array2.joinWith("&")

let path = url.path->Belt.List.toArray->Js.Array2.joinWith("/")
RescriptReactRouter.replace(`/${path}?${searchStr}`)
searchStr->FilterUtils.parseFilterString->updateExistingKeys
}
}

Expand Down Expand Up @@ -551,6 +549,7 @@ let make = (
~disableURIdecode=false,
~revampedFilter=false,
) => {
let {query} = React.useContext(FilterContext.filterContext)
let alreadySelectedFiltersUserpref = `remote_filters_selected_keys_${tableName->Belt.Option.getWithDefault(
"",
)}`
Expand Down Expand Up @@ -584,17 +583,14 @@ let make = (
}, [updatedSelectedList->Js.Json.stringify])

let getNewQuery = DateRefreshHooks.useConstructQueryOnBasisOfOpt()
let url = RescriptReactRouter.useUrl()
let (isButtonDisabled, setIsButtonDisabled) = React.useState(_ => false)
let queryStr = url.search

let totalFilters = selectedFiltersList->Js.Array2.length + localOptions->Js.Array2.length
let (checkedFilters, setCheckedFilters) = React.useState(_ => [])
let (clearFilterAfterRefresh, setClearFilterAfterRefresh) = React.useState(_ => false)
let (count, setCount) = React.useState(_ => initalCount)

let url = RescriptReactRouter.useUrl()
let searchParams = disableURIdecode ? url.search : url.search->Js.Global.decodeURI
let searchParams = disableURIdecode ? query : query->Js.Global.decodeURI

let isMobileView = MatchMedia.useMobileChecker()

Expand Down Expand Up @@ -836,7 +832,7 @@ let make = (

let handleRefresh = _ => {
let newQueryStr = getNewQuery(
~queryString=queryStr,
~queryString=query,
~disableFutureDates=true,
~disablePastDates=false,
~startKey="startTime",
Expand Down
109 changes: 0 additions & 109 deletions src/context/AnalyticsUrlUpdaterContext.res

This file was deleted.

Loading

0 comments on commit fd1aa3d

Please sign in to comment.