The LeadCountOverPeriodWidget displays the number of leads added for each status and the percentage difference between the current period's total and that of the previous equivalent period (e.g., if October is selected, the past period will be September). The component is built using the Ant DesignCarousel and LeadAmountDifferenceWidget components.
For correct display of widgets (4 per carousel page) we should chunk our array of data by smaller one's with size of 4. Then we well map chunked arrays and map each array sepaarately to generate widgets. Code example:
In LeadAmountDiffenceWidget we will display passed information and add a SparkLine with correct color and with direction based on percentageDifference. Inside LeadAmountDiffenceWidget we create a variable with value of "Ascend" | "Descend" | "none" called type:
Inside the component it will be 3 different svg lines which will returned depends on type. Also each line have linear gradient under it. To achieve correct gradient display we should create an unique gradient id in the following way:
As you can see, we need to pass it to the fill attribute of the first path tag with a URL to ensure the correct color. To apply the gradient under the line correctly, use linearGradient with a unique gradientId. If gradientId is not unique, all lines will appear black and lack gradient.
Backend
On backend we have countLeadsOverPeriod function which return us an array of object for each lead status with following fields:
name - the name of lead status
current - total amount of leads with current status
percentageChange - rounded difference between current and past period in percentage
color - status primary color
backgroundColor - status secondary color
To create that array, we first need to retrieve all statuses for the user's teamId. Then, we create a query for each status using a for loop, which will return the count of leads that were created within the required period and currently have the specified status. Query:
constcurrentQuery=this.leadsRepository.createQueryBuilder('lead').leftJoin('lead.business','business').leftJoin('lead.status','status').where('lead.statusId = :statusId', { statusId }).andWhere('lead.teamId = :teamId', { teamId }).andWhere('lead.createdAt BETWEEN :startOfDay AND :endOfDay', { startOfDay, endOfDay }).andWhere('business.id IN (:...businessesIds)', { businessesIds });
To get the lead count for the previous time period (to calculate the percentage difference), we will run almost the same query but with a different startOfDay and endOfDay. After that, we calculate the percentage difference and push an object with the required values into the resulting array. Here is the full code of the complete cycle: