-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New statistic - Customer Renetion Rate
- Loading branch information
1 parent
3029a79
commit 264b861
Showing
11 changed files
with
293 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...components/customers/retention-customer-rate/customers-retention-customer-rate-number.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2024 RSC-Labs, https://rsoftcon.com/ | ||
* | ||
* MIT License | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Heading } from "@medusajs/ui"; | ||
import { Grid } from "@mui/material"; | ||
import { PercentageComparison } from "../../common/percentage-comparison"; | ||
import { IconComparison } from "../../common/icon-comparison"; | ||
import { CustomersRetentionCustomerRateResponse } from "../types" | ||
|
||
export const RetentionCustomerRateNumber = ({retentionCustomerRateResponse, compareEnabled} : {retentionCustomerRateResponse: CustomersRetentionCustomerRateResponse, compareEnabled?: boolean}) => { | ||
const currentPercentage: number | undefined = | ||
retentionCustomerRateResponse.analytics.current !== undefined ? | ||
parseInt(retentionCustomerRateResponse.analytics.current) : undefined; | ||
const previousPercentage: number | undefined = | ||
retentionCustomerRateResponse.analytics.previous !== undefined ? | ||
parseInt(retentionCustomerRateResponse.analytics.previous) : undefined; | ||
|
||
return ( | ||
<Grid container alignItems={'center'} spacing={2}> | ||
<Grid item> | ||
{currentPercentage !== undefined ? | ||
<Heading level="h1"> | ||
{`${currentPercentage}%`} | ||
</Heading> : | ||
<Heading level="h3"> | ||
{`No orders or customers`} | ||
</Heading> | ||
} | ||
</Grid> | ||
{compareEnabled && retentionCustomerRateResponse.analytics.dateRangeFromCompareTo && currentPercentage !== undefined && | ||
<Grid item> | ||
<Grid container alignItems={'center'}> | ||
<Grid item> | ||
<IconComparison current={currentPercentage} previous={previousPercentage ? previousPercentage : undefined}/> | ||
</Grid> | ||
{previousPercentage !== undefined && <Grid item> | ||
<PercentageComparison current={currentPercentage.toString()} previous={previousPercentage.toString()} label="%"/> | ||
</Grid>} | ||
</Grid> | ||
</Grid> | ||
} | ||
</Grid> | ||
); | ||
} |
95 changes: 95 additions & 0 deletions
95
src/ui-components/customers/retention-customer-rate/customers-retention-customer-rate.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2024 RSC-Labs, https://rsoftcon.com/ | ||
* | ||
* MIT License | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Heading, Alert } from "@medusajs/ui"; | ||
import { ShoppingBag } from "@medusajs/icons"; | ||
import { CircularProgress, Grid } from "@mui/material"; | ||
import { useAdminCustomQuery } from "medusa-react" | ||
import { DateRange } from "../../utils/types"; | ||
import { CustomersRetentionCustomerRateResponse } from "../types"; | ||
import { RetentionCustomerRateNumber } from "./customers-retention-customer-rate-number"; | ||
import { OrderStatus } from "../../utils/types"; | ||
|
||
type AdminCustomersStatisticsQuery = { | ||
orderStatuses: string[], | ||
dateRangeFrom: number, | ||
dateRangeTo: number, | ||
dateRangeFromCompareTo?: number, | ||
dateRangeToCompareTo?: number, | ||
} | ||
|
||
const RetentionCustomerRateDetails = ({orderStatuses, dateRange, dateRangeCompareTo, compareEnabled} : | ||
{orderStatuses: OrderStatus[], dateRange?: DateRange, dateRangeCompareTo?: DateRange, compareEnabled?: boolean}) => { | ||
const { data, isLoading, isError, error } = useAdminCustomQuery< | ||
AdminCustomersStatisticsQuery, | ||
CustomersRetentionCustomerRateResponse | ||
>( | ||
`/customers-analytics/retention-customer-rate`, | ||
[orderStatuses, dateRange, dateRangeCompareTo], | ||
{ | ||
orderStatuses: Object.values(orderStatuses), | ||
dateRangeFrom: dateRange ? dateRange.from.getTime() : undefined, | ||
dateRangeTo: dateRange ? dateRange.to.getTime() : undefined, | ||
dateRangeFromCompareTo: dateRangeCompareTo ? dateRangeCompareTo.from.getTime() : undefined, | ||
dateRangeToCompareTo: dateRangeCompareTo ? dateRangeCompareTo.to.getTime() : undefined, | ||
} | ||
) | ||
|
||
if (isLoading) { | ||
return <CircularProgress size={12}/> | ||
} | ||
|
||
if (isError) { | ||
const trueError = error as any; | ||
const errorText = `Error when loading data. It shouldn't have happened - please raise an issue. For developer: ${trueError?.response?.data?.message}` | ||
return <Alert variant="error">{errorText}</Alert> | ||
} | ||
|
||
if (data.analytics == undefined) { | ||
return <Heading level="h3">Cannot get orders or customers</Heading> | ||
} | ||
|
||
if (data.analytics.dateRangeFrom) { | ||
return ( | ||
<Grid container> | ||
<Grid item xs={12} md={12}> | ||
<RetentionCustomerRateNumber retentionCustomerRateResponse={data} compareEnabled={compareEnabled}/> | ||
</Grid> | ||
</Grid> | ||
) | ||
} else { | ||
return <Heading level="h3">No orders or customers</Heading> | ||
} | ||
} | ||
|
||
export const CustomersRetentionCustomerRate = ({orderStatuses, dateRange, dateRangeCompareTo, compareEnabled} : | ||
{orderStatuses: OrderStatus[], dateRange?: DateRange, dateRangeCompareTo?: DateRange, compareEnabled: boolean}) => { | ||
return ( | ||
<Grid container paddingBottom={2} spacing={3}> | ||
<Grid item xs={12} md={12}> | ||
<Grid container spacing={2}> | ||
<Grid item> | ||
<ShoppingBag/> | ||
</Grid> | ||
<Grid item> | ||
<Heading level="h2"> | ||
Retention customer rate | ||
</Heading> | ||
</Grid> | ||
</Grid> | ||
</Grid> | ||
<Grid item xs={12} md={12}> | ||
<RetentionCustomerRateDetails orderStatuses={orderStatuses} dateRange={dateRange} dateRangeCompareTo={dateRangeCompareTo} compareEnabled={compareEnabled}/> | ||
</Grid> | ||
</Grid> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters