roles leaderboar visible role changes

This commit is contained in:
Abhijit 2026-12-02 11:25:07 +05:30
parent c97d136068
commit c00880dbde
3 changed files with 48 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import { Controller, useWatch } from 'react-hook-form';
import Select from 'react-select';
import { Button, Form, Table } from 'react-bootstrap';
function patchRow(rows, index, patch) {
@ -8,11 +9,12 @@ function patchRow(rows, index, patch) {
}
function emptyRow() {
return { title: '', role: '', limit: 10, sort_order: 0, view_mode: 'versus', scope: 'domain', show_as: 'user', highlight_count: 0, highlight_color: '' };
return { title: '', role: '', limit: 10, sort_order: 0, view_mode: 'versus', scope: 'domain', show_as: 'user', highlight_count: 0, highlight_color: '', visible_roles: [] };
}
export default function RankRolesField({ field, control }) {
const roles = field.roles || {};
const roleOptions = Object.entries(roles).map(([value, label]) => ({ value, label }));
const isTeam = useWatch({ control, name: 'leaderboard_roles_type' }) === 'team';
return (
<Controller
@ -37,6 +39,7 @@ export default function RankRolesField({ field, control }) {
{isTeam ? <th>What to show</th> : null}
{isTeam ? <th>Show</th> : null}
<th>Limit</th>
<th>Visible Roles</th>
<th>Highlight</th>
<th>Color</th>
<th>Sort Order</th>
@ -110,6 +113,19 @@ export default function RankRolesField({ field, control }) {
onChange={(e) => rhf.onChange(patchRow(rows, index, { limit: e.target.value }))}
/>
</td>
<td>
<Select
isMulti
options={roleOptions}
value={roleOptions.filter((option) => (row.visible_roles || []).map(String).includes(String(option.value)))}
onChange={(selected) => rhf.onChange(patchRow(rows, index, {
visible_roles: (selected || []).map((option) => option.value),
}))}
classNamePrefix="react-select"
menuPortalTarget={document.body}
styles={{ menuPortal: (base) => ({ ...base, zIndex: 9999 }), container: (base) => ({ ...base, minWidth: 180 }) }}
/>
</td>
<td>
<Form.Control
type="number"

View File

@ -220,6 +220,7 @@ const leaderboards = {
scope: next.leaderboard_roles_type === 'team' && row.show_as !== 'team' && row.scope === 'own_team' ? 'own_team' : 'domain',
highlight_count: Number(row.highlight_count) > 0 ? Number(row.highlight_count) : 0,
highlight_color: /^#[0-9A-Fa-f]{6}$/.test(row.highlight_color || '') ? row.highlight_color : '',
visible_roles: asArray(row.visible_roles),
}))
.sort(bySortOrder);
next.metrics = (Array.isArray(next.metrics) ? next.metrics : [])
@ -269,6 +270,7 @@ const leaderboards = {
scope: showAs === 'user' && leaderboardType === 'team' && row.scope === 'own_team' ? 'own_team' : 'domain',
highlight_count: Number(row.highlight_count) > 0 ? Number(row.highlight_count) : 0,
highlight_color: /^#[0-9A-Fa-f]{6}$/.test(row.highlight_color || '') ? row.highlight_color : '',
visible_roles: asArray(row.visible_roles),
};
});
payload.metrics = (Array.isArray(payload.metrics) ? payload.metrics : [])

View File

@ -4,6 +4,7 @@ import { Button, Card, Container, Form, Spinner, Table } from 'react-bootstrap';
import { signedGet, isSuccess } from '@/api/client';
import PageBreadcrumb from '@/components/PageBreadcrumb';
import { downloadCsv } from '@/helpers/csv';
import { useAuth } from '@/auth/AuthProvider';
import { asArray, rankRoleId, rankRoleName, rankRolePillLabel } from '@/views/dashboard/helpers';
function uniquePeriods(periods) {
@ -99,7 +100,7 @@ function periodLabelFrom(view) {
return `${firstDay}${firstDay && lastDay ? ' – ' : ''}${lastDay}`.trim();
}
function shapeView({ id, roleId, period, dashboard, item }) {
function shapeView({ id, roleId, period, dashboard, item, userRoles }) {
const view = dashboard?.view || {};
const groups = asArray(dashboard?.leaderboard);
const periods = uniquePeriods(view.periods);
@ -108,7 +109,29 @@ function shapeView({ id, roleId, period, dashboard, item }) {
const title = view.name || item?.name || 'Leaderboard';
const periodLabel = periodLabelFrom(view);
const hasTeams = isTeamView(view.team_view) || teamRows.length;
const rolePills = buildPills(id, roleId, item?.rank_roles, groups, period);
const savedRankRoles = asArray(item?.rank_roles);
const mine = asArray(userRoles).map(String);
const allowedRankRoles = savedRankRoles.filter((row) => {
const allowed = asArray(row?.visible_roles).map(String);
return !allowed.length || allowed.some((role) => mine.includes(role));
});
if (savedRankRoles.length && !allowedRankRoles.length) {
return {
error: 'No Leaderboards Found',
isTeam: false,
title,
periodLabel: '',
periods,
selectedPeriod,
users: [],
metrics: [],
pills: [],
selectedRole: '',
highlightCount: 0,
highlightColor: '',
};
}
const rolePills = buildPills(id, roleId, allowedRankRoles, groups, period);
const configured = rolePills.length > 0;
const pills = configured ? rolePills : withTeamPill(rolePills, id, period, roleId, hasTeams);
@ -203,6 +226,7 @@ function shapeView({ id, roleId, period, dashboard, item }) {
}
export default function LeaderboardDashboard() {
const { user } = useAuth();
const [searchParams, setSearchParams] = useSearchParams();
const id = String(searchParams.get('id') || '').trim();
const roleId = String(searchParams.get('role') || '').trim();
@ -236,6 +260,7 @@ export default function LeaderboardDashboard() {
period,
dashboard: {},
item: isSuccess(oneRes) ? oneRes.data?.item : null,
userRoles: user?.roles,
}),
);
setError(dashboardRes?.message || 'Unable to load leaderboard.');
@ -247,6 +272,7 @@ export default function LeaderboardDashboard() {
period,
dashboard: dashboardRes.data || {},
item: isSuccess(oneRes) ? oneRes.data?.item : null,
userRoles: user?.roles,
});
setView(next);
setError(next.error || '');
@ -258,7 +284,7 @@ export default function LeaderboardDashboard() {
}
};
load();
}, [id, roleId, period]);
}, [id, roleId, period, user]);
const title = view?.title || 'Leaderboard';
const periods = view?.periods || [];