Skip to content

Commit

Permalink
chore: filter & list api for payment & refunds (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitanjli525 authored May 23, 2024
1 parent 997494e commit 5900070
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 156 deletions.
112 changes: 7 additions & 105 deletions src/components/Filter.res
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ module ClearFilters = {
->Option.getOr(Dict.make())
->Dict.toArray
->Array.filter(entry => {
let (key, value) = entry
let (_, value) = entry
let isEmptyValue = switch value->JSON.Classify.classify {
| String(str) => str->LogicUtils.isEmptyString
| Array(arr) => arr->Array.length === 0
| Null => true
| _ => false
}

!(defaultFilterKeys->Array.includes(key)) && !isEmptyValue
!isEmptyValue
})
->Array.length > 0
}, (formState.initialValues, defaultFilterKeys))
Expand Down Expand Up @@ -119,97 +118,6 @@ let getStrFromJson = (key, val) => {
}
}

module ApplyFilterButton = {
@react.component
let make = (
~autoApply,
~totalFilters,
~filterButtonStyle,
~defaultFilterKeys,
~allFilters: array<FormRenderer.fieldInfoType>,
) => {
let defaultinputField = FormRenderer.makeInputFieldInfo(~name="-", ())
let inputFieldsDict =
allFilters
->Array.map(filter => {
let inputFieldsArr = filter.inputFields
let inputField = inputFieldsArr->LogicUtils.getValueFromArray(0, defaultinputField)
(inputField.name, inputField)
})
->Dict.fromArray

let formState: ReactFinalForm.formState = ReactFinalForm.useFormState(
ReactFinalForm.useFormSubscription(["values", "dirtyFields", "initialValues"])->Nullable.make,
)

let formCurrentValues =
formState.values
->LogicUtils.getDictFromJsonObject
->DictionaryUtils.deleteKeys(defaultFilterKeys)
let formInitalValues =
formState.initialValues
->LogicUtils.getDictFromJsonObject
->DictionaryUtils.deleteKeys(defaultFilterKeys)
let dirtyFields = formState.dirtyFields->Dict.keysToArray

let getFormattedDict = dict => {
dict
->Dict.toArray
->Array.map(entry => {
let (key, value) = entry
let inputField = inputFieldsDict->Dict.get(key)->Option.getOr(defaultinputField)
let formattor = inputField.format
let value = switch formattor {
| Some(fn) => fn(. ~value, ~name=key)
| None => value
}
(key, value)
})
->Dict.fromArray
}

let showApplyFilter = {
let formattedInitialValues = formInitalValues->getFormattedDict
let formattedCurrentValues = formCurrentValues->getFormattedDict

let equalDictCheck = DictionaryUtils.checkEqualJsonDicts(
formattedInitialValues,
formattedCurrentValues,
~checkKeys=dirtyFields,
~ignoreKeys=["opt"],
)

let otherCheck =
formattedCurrentValues
->Dict.toArray
->Array.reduce(true, (acc, item) => {
let (_, value) = item
switch value->JSON.Classify.classify {
| String(str) => str->LogicUtils.isEmptyString
| Array(arr) => arr->Array.length === 0
| Object(dict) => dict->Dict.toArray->Array.length === 0
| Null => true
| _ => false
} &&
acc
})
!equalDictCheck && !otherCheck
}

// if all values are empty then don't show the apply filters let it be the clear filters visible

if autoApply || totalFilters === 0 {
React.null
} else if showApplyFilter {
<div className={`flex justify-between items-center ${filterButtonStyle}`}>
<FormRenderer.SubmitButton text="Apply Filters" icon={Button.FontAwesome("check")} />
</div>
} else {
React.null
}
}
}

@react.component
let make = (
~defaultFilters,
Expand Down Expand Up @@ -251,22 +159,21 @@ let make = (
let verticalGap = !isMobileView ? "gap-y-3" : ""

React.useEffect1(_ => {
if remoteFilters->Array.length >= allFilters->Array.length {
setAllFilters(_ => remoteFilters->Array.map(item => item.field))
}
let updatedAllFilters = remoteFilters->Array.map(item => item.field)
setAllFilters(_ => updatedAllFilters)
None
}, remoteFilters)

let localFilterJson = RemoteFiltersUtils.getInitialValuesFromUrl(
~searchParams,
~initialFilters=localFilters,
~initialFilters={Array.concat(localFilters, fixedFilters)},
(),
)

let clearFilterJson =
RemoteFiltersUtils.getInitialValuesFromUrl(
~searchParams,
~initialFilters=localFilters,
~initialFilters={Array.concat(localFilters, fixedFilters)},
~options=remoteOptions,
(),
)
Expand Down Expand Up @@ -437,12 +344,7 @@ let make = (
}
`${activeClasses} font-medium`
}>
<div className="mr-5">
{option.inputNames
->Array.get(0)
->Option.getOr("")
->React.string}
</div>
<div className="mr-5"> {option.label->React.string} </div>
</button>
</div>}
</Menu.Item>
Expand Down
2 changes: 1 addition & 1 deletion src/components/FilterSelectBox.res
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ module BaseSelect = {

<div
id="neglectTopbarTheme"
className={`${widthClass} ${outerClass} ${borderClass} ${animationClass} ${dropdownClassName}`}>
className={`${widthClass} ${outerClass} ${borderClass} ${animationClass} ${dropdownClassName} max-h-80`}>
{switch searchable {
| Some(val) =>
if val {
Expand Down
61 changes: 30 additions & 31 deletions src/screens/HSwitchRemoteFilter.res
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ module SearchBarFilter = {
}

module RemoteTableFilters = {
open LogicUtils
@react.component
let make = (
~filterUrl,
~filterUrlV2,
~setFilters,
~endTimeFilterKey,
~startTimeFilterKey,
Expand All @@ -130,6 +129,7 @@ module RemoteTableFilters = {
let {filterValue, updateExistingKeys, filterValueJson, reset} =
FilterContext.filterContext->React.useContext
let defaultFilters = {""->JSON.Encode.string}
let showToast = ToastState.useShowToast()

React.useEffect0(() => {
if filterValueJson->Dict.keysToArray->Array.length === 0 {
Expand All @@ -139,40 +139,27 @@ module RemoteTableFilters = {
None
})

let endTimeVal = filterValueJson->getString(endTimeFilterKey, "")
let startTimeVal = filterValueJson->getString(startTimeFilterKey, "")

let filterBody = React.useMemo3(() => {
[
(startTimeFilterKey, startTimeVal->JSON.Encode.string),
(endTimeFilterKey, endTimeVal->JSON.Encode.string),
]->Dict.fromArray
}, (startTimeVal, endTimeVal, filterValue))

open APIUtils

let (filterDataJson, setFilterDataJson) = React.useState(_ => None)
let updateDetails = useUpdateMethod()
let {filterValueJson} = FilterContext.filterContext->React.useContext
let startTimeVal = filterValueJson->getString("start_time", "")
let endTimeVal = filterValueJson->getString("end_time", "")

let getFilters = async () => {
let json = await updateDetails(filterUrl, filterBody->JSON.Encode.object, Post, ())
setFilterDataJson(_ => json->Some)
}

React.useEffect3(() => {
setFilterDataJson(_ => None)
if startTimeVal->isNonEmptyString && endTimeVal->isNonEmptyString {
try {
getFilters()->ignore
} catch {
| _ => ()
}
let fetchDetails = useGetMethod()

let fetchAllFilters = async () => {
try {
setFilterDataJson(_ => None)
let response = await fetchDetails(filterUrlV2)
setFilterDataJson(_ => response->Some)
} catch {
| _ => showToast(~message="Failed to load filters", ~toastType=ToastError, ())
}
}

React.useEffect0(() => {
fetchAllFilters()->ignore
None
}, (startTimeVal, endTimeVal, filterBody->JSON.Encode.object->JSON.stringify))
})

let filterData = filterDataJson->Option.getOr(Dict.make()->JSON.Encode.object)

let setInitialFilters = useSetInitialFilters(
Expand Down Expand Up @@ -201,7 +188,19 @@ module RemoteTableFilters = {
None
}, [filterValue])

let remoteFilters = filterData->initialFilters
let getAllFilter =
filterValue
->Dict.toArray
->Array.map(item => {
let (key, value) = item
(key, value->UrlFetchUtils.getFilterValue)
})
->Dict.fromArray

let remoteFilters = React.useMemo1(() => {
filterData->initialFilters(getAllFilter)
}, [getAllFilter])

let initialDisplayFilters =
remoteFilters->Array.filter((item: EntityType.initialFilters<'t>) =>
item.localFilter->Option.isSome
Expand Down
Loading

0 comments on commit 5900070

Please sign in to comment.