Skip to content

Commit

Permalink
chore: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
gitanjli525 committed Sep 23, 2024
1 parent 9af5878 commit a75e77d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 85 deletions.
11 changes: 2 additions & 9 deletions src/screens/NewAnalytics/Graphs/LineGraph/LineGraph.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ external lineGraphOptionsToJson: LineGraphTypes.lineGraphOptions => JSON.t = "%i
@react.component
let make = (~entity, ~data: JSON.t, ~className="") => {
open NewAnalyticsTypes
let data = entity.getObjects(data)
let default = entity.getChatOptions(data)->lineGraphOptionsToJson
let (options, setOptions) = React.useState(_ => default)

React.useEffect(() => {
// to re-rendor the chart
setOptions(_ => entity.getChatOptions(data)->lineGraphOptionsToJson)
None
}, [])
let object = entity.getObjects(data)
let options = entity.getChatOptions(object)->lineGraphOptionsToJson

<div className>
<Highcharts.Chart options highcharts={Highcharts.highcharts} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
// helper functions
open LogicUtils

let getCategories = (json: JSON.t): array<string> => {
json
->getArrayFromJson([])
->Array.flatMap(item => {
item
->getDictFromJsonObject
->getArrayFromDict("queryData", [])
->Array.map(item => item->getDictFromJsonObject->getString("time_bucket", ""))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,40 @@ let make = (

let getPaymentsProcessed = async () => {
try {
let data =
getData
->LogicUtils.getDictFromJsonObject
->LogicUtils.getArrayFromDict("queryData", [])
->JSON.Encode.array
let response = [
{
"queryData": [
{"count": 24, "amount": 952, "currency": "USD", "time_bucket": "2024-08-13 18:30:00"},
{"count": 28, "amount": 1020, "currency": "USD", "time_bucket": "2024-08-14 18:30:00"},
{"count": 35, "amount": 1450, "currency": "USD", "time_bucket": "2024-08-15 18:30:00"},
{"count": 30, "amount": 1150, "currency": "USD", "time_bucket": "2024-08-16 18:30:00"},
{"count": 40, "amount": 1600, "currency": "USD", "time_bucket": "2024-08-17 18:30:00"},
{"count": 29, "amount": 1200, "currency": "USD", "time_bucket": "2024-08-18 18:30:00"},
{"count": 31, "amount": 1300, "currency": "USD", "time_bucket": "2024-08-19 18:30:00"},
{"count": 56, "amount": 3925, "currency": "EUR", "time_bucket": "2024-08-13 18:30:00"},
{"count": 50, "amount": 3750, "currency": "EUR", "time_bucket": "2024-08-14 18:30:00"},
{"count": 42, "amount": 3150, "currency": "EUR", "time_bucket": "2024-08-15 18:30:00"},
{"count": 38, "amount": 2900, "currency": "EUR", "time_bucket": "2024-08-16 18:30:00"},
{"count": 44, "amount": 3300, "currency": "EUR", "time_bucket": "2024-08-17 18:30:00"},
{"count": 50, "amount": 3750, "currency": "EUR", "time_bucket": "2024-08-18 18:30:00"},
{"count": 60, "amount": 4500, "currency": "EUR", "time_bucket": "2024-08-19 18:30:00"},
{"count": 48, "amount": 3600, "currency": "GBP", "time_bucket": "2024-08-13 18:30:00"},
{"count": 45, "amount": 3400, "currency": "GBP", "time_bucket": "2024-08-14 18:30:00"},
{"count": 40, "amount": 3000, "currency": "GBP", "time_bucket": "2024-08-15 18:30:00"},
{"count": 43, "amount": 3200, "currency": "GBP", "time_bucket": "2024-08-16 18:30:00"},
{"count": 46, "amount": 3500, "currency": "GBP", "time_bucket": "2024-08-17 18:30:00"},
{"count": 50, "amount": 3800, "currency": "GBP", "time_bucket": "2024-08-18 18:30:00"},
{"count": 52, "amount": 4000, "currency": "GBP", "time_bucket": "2024-08-19 18:30:00"},
],
"metaData": [
{"count": 217, "amount": 8672, "currency": "USD"},
{"count": 340, "amount": 25575, "currency": "EUR"},
{"count": 324, "amount": 24500, "currency": "GBP"},
],
},
]->Identity.genericTypeToJson

setpaymentsProcessed(_ => data)
setpaymentsProcessed(_ => response)
} catch {
| _ => ()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
open PaymentsProcessedTypes
let paymentsProcessedMapper = (_json): LineGraphTypes.lineGraphPayload => {
open LineGraphTypes
let categories = [
"01 Aug",
"02 Aug",
"03 Aug",
"04 Aug",
"05 Aug",
"06 Aug",
"07 Aug",
"08 Aug",
"09 Aug",
"10 Aug",
"11 Aug",
]
let data = [
{
open NewPaymentAnalyticsUtils
open LogicUtils

let getData = (json: JSON.t): LineGraphTypes.data => {
json
->getArrayFromJson([])
->Array.mapWithIndex((item, index) => {
let data =
item
->getDictFromJsonObject
->getArrayFromDict("queryData", [])
->Array.map(item => {
item->getDictFromJsonObject->getInt("amount", 0)
})
let dataObj: LineGraphTypes.dataObj = {
showInLegend: false,
name: "Series 1",
data: [3000, 5000, 7000, 5360, 4500, 6800, 5400, 3000, 0, 0],
name: `Series ${index->Int.toString}`,
data,
color: "#2f7ed8",
},
]
}
dataObj
})
}

let paymentsProcessedMapper = (json: JSON.t): LineGraphTypes.lineGraphPayload => {
open LineGraphTypes
let categories = getCategories(json)
let data = getData(json)
let title = {
text: "USD",
}
Expand All @@ -40,7 +45,6 @@ let colMapper = (col: paymentsProcessedCols) => {
}

let tableItemToObjMapper: Dict.t<JSON.t> => paymentsProcessedObject = dict => {
open LogicUtils
{
count: dict->getInt(Count->colMapper, 0),
amount: dict->getFloat(Amount->colMapper, 0.0),
Expand All @@ -50,7 +54,6 @@ let tableItemToObjMapper: Dict.t<JSON.t> => paymentsProcessedObject = dict => {
}

let getObjects: JSON.t => array<paymentsProcessedObject> = json => {
open LogicUtils
json
->LogicUtils.getArrayFromJson([])
->Array.map(item => {
Expand All @@ -77,38 +80,6 @@ let getCell = (obj, colType): Table.cell => {
}
}

// Remove this
let getData = {
"queryData": [
{"count": 24, "amount": 952, "currency": "USD", "time_bucket": "2024-08-13 18:30:00"},
{"count": 28, "amount": 1020, "currency": "USD", "time_bucket": "2024-08-14 18:30:00"},
{"count": 35, "amount": 1450, "currency": "USD", "time_bucket": "2024-08-15 18:30:00"},
{"count": 30, "amount": 1150, "currency": "USD", "time_bucket": "2024-08-16 18:30:00"},
{"count": 40, "amount": 1600, "currency": "USD", "time_bucket": "2024-08-17 18:30:00"},
{"count": 29, "amount": 1200, "currency": "USD", "time_bucket": "2024-08-18 18:30:00"},
{"count": 31, "amount": 1300, "currency": "USD", "time_bucket": "2024-08-19 18:30:00"},
{"count": 56, "amount": 3925, "currency": "EUR", "time_bucket": "2024-08-13 18:30:00"},
{"count": 50, "amount": 3750, "currency": "EUR", "time_bucket": "2024-08-14 18:30:00"},
{"count": 42, "amount": 3150, "currency": "EUR", "time_bucket": "2024-08-15 18:30:00"},
{"count": 38, "amount": 2900, "currency": "EUR", "time_bucket": "2024-08-16 18:30:00"},
{"count": 44, "amount": 3300, "currency": "EUR", "time_bucket": "2024-08-17 18:30:00"},
{"count": 50, "amount": 3750, "currency": "EUR", "time_bucket": "2024-08-18 18:30:00"},
{"count": 60, "amount": 4500, "currency": "EUR", "time_bucket": "2024-08-19 18:30:00"},
{"count": 48, "amount": 3600, "currency": "GBP", "time_bucket": "2024-08-13 18:30:00"},
{"count": 45, "amount": 3400, "currency": "GBP", "time_bucket": "2024-08-14 18:30:00"},
{"count": 40, "amount": 3000, "currency": "GBP", "time_bucket": "2024-08-15 18:30:00"},
{"count": 43, "amount": 3200, "currency": "GBP", "time_bucket": "2024-08-16 18:30:00"},
{"count": 46, "amount": 3500, "currency": "GBP", "time_bucket": "2024-08-17 18:30:00"},
{"count": 50, "amount": 3800, "currency": "GBP", "time_bucket": "2024-08-18 18:30:00"},
{"count": 52, "amount": 4000, "currency": "GBP", "time_bucket": "2024-08-19 18:30:00"},
],
"metaData": [
{"count": 217, "amount": 8672, "currency": "USD"},
{"count": 340, "amount": 25575, "currency": "EUR"},
{"count": 324, "amount": 24500, "currency": "GBP"},
],
}->Identity.genericObjectOrRecordToJson

let getData2 = {
"queryData": [
{"count": 24, "amount": 952, "time_bucket": "2024-08-13 18:30:00"},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
open LogicUtils
open NewPaymentAnalyticsUtils

let getTimeBucket = (json: JSON.t): array<string> => {
json
->getArrayFromJson([])
->Array.flatMap(item => {
item
->getDictFromJsonObject
->getArrayFromDict("queryData", [])
->Array.map(item => item->getDictFromJsonObject->getString("time_bucket", ""))
})
}

let createData = (json: JSON.t): LineGraphTypes.data => {
let getData = (json: JSON.t): LineGraphTypes.data => {
json
->getArrayFromJson([])
->Array.mapWithIndex((item, index) => {
Expand All @@ -34,8 +24,8 @@ let createData = (json: JSON.t): LineGraphTypes.data => {

let paymentsSuccessRateMapper = (json: JSON.t): LineGraphTypes.lineGraphPayload => {
open LineGraphTypes
let categories = getTimeBucket(json)
let data = createData(json)
let categories = getCategories(json)
let data = getData(json)
let title = {
text: "Payments Success Rate",
}
Expand Down

0 comments on commit a75e77d

Please sign in to comment.