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 { .mlb-period {
display: flex; display: flex;
align-items: center; align-items: center;
flex-wrap: wrap;
gap: 8px; gap: 8px;
min-width: 220px; min-width: 220px;
} }

View File

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