changes of custom date

This commit is contained in:
Abhijit 2026-12-02 16:22:46 +05:30
parent c00880dbde
commit 7330f4d4d6
2 changed files with 66 additions and 9 deletions

View File

@ -326,6 +326,7 @@
.mlb-period {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
min-width: 220px;
}

View File

@ -1,6 +1,7 @@
import { useEffect, useMemo, useState } from 'react';
import { Link, useSearchParams } from 'react-router';
import { Button, Card, Container, Form, Spinner, Table } from 'react-bootstrap';
import { TbArrowRight } from 'react-icons/tb';
import { signedGet, isSuccess } from '@/api/client';
import PageBreadcrumb from '@/components/PageBreadcrumb';
import { downloadCsv } from '@/helpers/csv';
@ -54,7 +55,7 @@ function rowsWithMetrics(rows, readInfo) {
return { metrics, users };
}
function buildPills(leaderboardId, selectedRoleId, rankRoles, groups, period) {
function buildPills(leaderboardId, selectedRoleId, rankRoles, groups, period, startDate, endDate) {
let roles = asArray(rankRoles);
if (!roles.length) {
roles = asArray(groups)
@ -66,6 +67,10 @@ function buildPills(leaderboardId, selectedRoleId, rankRoles, groups, period) {
const roleId = rankRoleId(rankRole);
const params = new URLSearchParams({ id: leaderboardId, role: roleId });
if (period) params.set('period', period);
if (period === 'custom' && startDate && endDate) {
params.set('start_date', startDate);
params.set('end_date', endDate);
}
return {
label: rankRolePillLabel(rankRole?.title, rankRoleName(rankRole)),
to: `/leaderboard/dashboard?${params.toString()}`,
@ -75,10 +80,14 @@ function buildPills(leaderboardId, selectedRoleId, rankRoles, groups, period) {
});
}
function withTeamPill(pills, leaderboardId, period, roleId, hasTeams) {
function withTeamPill(pills, leaderboardId, period, roleId, hasTeams, startDate, endDate) {
if (!hasTeams) return pills;
const params = new URLSearchParams({ id: leaderboardId });
if (period) params.set('period', period);
if (period === 'custom' && startDate && endDate) {
params.set('start_date', startDate);
params.set('end_date', endDate);
}
return [
{ label: 'Teams', to: `/leaderboard/dashboard?${params.toString()}`, active: !roleId, roleId: 'teams' },
...pills.map((pill) => ({ ...pill, active: Boolean(roleId) && pill.active })),
@ -100,7 +109,7 @@ function periodLabelFrom(view) {
return `${firstDay}${firstDay && lastDay ? ' – ' : ''}${lastDay}`.trim();
}
function shapeView({ id, roleId, period, dashboard, item, userRoles }) {
function shapeView({ id, roleId, period, startDate, endDate, dashboard, item, userRoles }) {
const view = dashboard?.view || {};
const groups = asArray(dashboard?.leaderboard);
const periods = uniquePeriods(view.periods);
@ -131,9 +140,9 @@ function shapeView({ id, roleId, period, dashboard, item, userRoles }) {
highlightColor: '',
};
}
const rolePills = buildPills(id, roleId, allowedRankRoles, groups, period);
const rolePills = buildPills(id, roleId, allowedRankRoles, groups, period, startDate, endDate);
const configured = rolePills.length > 0;
const pills = configured ? rolePills : withTeamPill(rolePills, id, period, roleId, hasTeams);
const pills = configured ? rolePills : withTeamPill(rolePills, id, period, roleId, hasTeams, startDate, endDate);
if (hasTeams && !roleId && !configured) {
const shaped = rowsWithMetrics(teamRows, (row) => {
@ -231,10 +240,20 @@ export default function LeaderboardDashboard() {
const id = String(searchParams.get('id') || '').trim();
const roleId = String(searchParams.get('role') || '').trim();
const period = String(searchParams.get('period') || '').trim();
const startDate = String(searchParams.get('start_date') || '').trim();
const endDate = String(searchParams.get('end_date') || '').trim();
const waitingForDates = period === 'custom' && (!startDate || !endDate);
const [draftStart, setDraftStart] = useState(startDate);
const [draftEnd, setDraftEnd] = useState(endDate);
const [view, setView] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
useEffect(() => {
setDraftStart(startDate);
setDraftEnd(endDate);
}, [startDate, endDate]);
useEffect(() => {
const load = async () => {
if (!id) {
@ -243,11 +262,20 @@ export default function LeaderboardDashboard() {
setLoading(false);
return;
}
if (period === 'custom' && (!startDate || !endDate)) {
setLoading(false);
setError('');
return;
}
setLoading(true);
setError('');
try {
const params = { id };
if (period) params.period = period;
if (period === 'custom') {
params.start_date = startDate;
params.end_date = endDate;
}
const [dashboardRes, oneRes] = await Promise.all([
signedGet('reports', 'roles_leaderboard/dashboard', params, { timeout: 120000 }),
signedGet('reports', 'roles_leaderboard/get_one', { id }),
@ -258,6 +286,8 @@ export default function LeaderboardDashboard() {
id,
roleId,
period,
startDate,
endDate,
dashboard: {},
item: isSuccess(oneRes) ? oneRes.data?.item : null,
userRoles: user?.roles,
@ -270,6 +300,8 @@ export default function LeaderboardDashboard() {
id,
roleId,
period,
startDate,
endDate,
dashboard: dashboardRes.data || {},
item: isSuccess(oneRes) ? oneRes.data?.item : null,
userRoles: user?.roles,
@ -284,7 +316,7 @@ export default function LeaderboardDashboard() {
}
};
load();
}, [id, roleId, period, user]);
}, [id, roleId, period, startDate, endDate, user]);
const title = view?.title || 'Leaderboard';
const periods = view?.periods || [];
@ -309,6 +341,19 @@ export default function LeaderboardDashboard() {
const next = new URLSearchParams(searchParams);
if (value) next.set('period', value);
else next.delete('period');
if (value !== 'custom') {
next.delete('start_date');
next.delete('end_date');
}
setSearchParams(next, { replace: true });
};
const applyCustomDates = () => {
if (!draftStart || !draftEnd) return;
const next = new URLSearchParams(searchParams);
next.set('period', 'custom');
next.set('start_date', draftStart);
next.set('end_date', draftEnd);
setSearchParams(next, { replace: true });
};
@ -332,7 +377,7 @@ export default function LeaderboardDashboard() {
</Button>
) : null}
</div>
{view?.periodLabel ? <p className="text-muted mb-3">{view.periodLabel}</p> : null}
{view?.periodLabel && !waitingForDates ? <p className="text-muted mb-3">{view.periodLabel}</p> : null}
<Card>
<Card.Body>
{pills.length || periods.length ? (
@ -350,7 +395,7 @@ export default function LeaderboardDashboard() {
<Form.Select
id="leaderboard-period"
size="sm"
value={view?.selectedPeriod || ''}
value={period || view?.selectedPeriod || ''}
onChange={(event) => onPeriodChange(event.target.value)}
>
{periods.map((item) => (
@ -359,11 +404,22 @@ export default function LeaderboardDashboard() {
</option>
))}
</Form.Select>
{period === 'custom' ? (
<>
<Form.Control type="date" size="sm" aria-label="Start date" value={draftStart} onChange={(event) => setDraftStart(event.target.value)} />
<Form.Control type="date" size="sm" aria-label="End date" value={draftEnd} onChange={(event) => setDraftEnd(event.target.value)} />
<Button type="button" size="sm" variant="primary" className="custom-date-go" aria-label="Apply dates" disabled={!draftStart || !draftEnd} onClick={applyCustomDates}>
<TbArrowRight />
</Button>
</>
) : null}
</div>
) : null}
</div>
) : null}
{loading ? (
{waitingForDates ? (
<p className="text-muted mb-0">Select a start date and an end date, then press the arrow.</p>
) : loading ? (
<div className="text-center py-4">
<Spinner animation="border" size="sm" />
</div>