roles leaderboar visible role changes
This commit is contained in:
parent
c97d136068
commit
c00880dbde
|
|
@ -1,4 +1,5 @@
|
||||||
import { Controller, useWatch } from 'react-hook-form';
|
import { Controller, useWatch } from 'react-hook-form';
|
||||||
|
import Select from 'react-select';
|
||||||
import { Button, Form, Table } from 'react-bootstrap';
|
import { Button, Form, Table } from 'react-bootstrap';
|
||||||
|
|
||||||
function patchRow(rows, index, patch) {
|
function patchRow(rows, index, patch) {
|
||||||
|
|
@ -8,11 +9,12 @@ function patchRow(rows, index, patch) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function emptyRow() {
|
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 }) {
|
export default function RankRolesField({ field, control }) {
|
||||||
const roles = field.roles || {};
|
const roles = field.roles || {};
|
||||||
|
const roleOptions = Object.entries(roles).map(([value, label]) => ({ value, label }));
|
||||||
const isTeam = useWatch({ control, name: 'leaderboard_roles_type' }) === 'team';
|
const isTeam = useWatch({ control, name: 'leaderboard_roles_type' }) === 'team';
|
||||||
return (
|
return (
|
||||||
<Controller
|
<Controller
|
||||||
|
|
@ -37,6 +39,7 @@ export default function RankRolesField({ field, control }) {
|
||||||
{isTeam ? <th>What to show</th> : null}
|
{isTeam ? <th>What to show</th> : null}
|
||||||
{isTeam ? <th>Show</th> : null}
|
{isTeam ? <th>Show</th> : null}
|
||||||
<th>Limit</th>
|
<th>Limit</th>
|
||||||
|
<th>Visible Roles</th>
|
||||||
<th>Highlight</th>
|
<th>Highlight</th>
|
||||||
<th>Color</th>
|
<th>Color</th>
|
||||||
<th>Sort Order</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 }))}
|
onChange={(e) => rhf.onChange(patchRow(rows, index, { limit: e.target.value }))}
|
||||||
/>
|
/>
|
||||||
</td>
|
</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>
|
<td>
|
||||||
<Form.Control
|
<Form.Control
|
||||||
type="number"
|
type="number"
|
||||||
|
|
|
||||||
|
|
@ -220,6 +220,7 @@ const leaderboards = {
|
||||||
scope: next.leaderboard_roles_type === 'team' && row.show_as !== 'team' && row.scope === 'own_team' ? 'own_team' : 'domain',
|
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_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 : '',
|
highlight_color: /^#[0-9A-Fa-f]{6}$/.test(row.highlight_color || '') ? row.highlight_color : '',
|
||||||
|
visible_roles: asArray(row.visible_roles),
|
||||||
}))
|
}))
|
||||||
.sort(bySortOrder);
|
.sort(bySortOrder);
|
||||||
next.metrics = (Array.isArray(next.metrics) ? next.metrics : [])
|
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',
|
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_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 : '',
|
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 : [])
|
payload.metrics = (Array.isArray(payload.metrics) ? payload.metrics : [])
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import { Button, Card, Container, Form, Spinner, Table } from 'react-bootstrap';
|
||||||
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';
|
||||||
|
import { useAuth } from '@/auth/AuthProvider';
|
||||||
import { asArray, rankRoleId, rankRoleName, rankRolePillLabel } from '@/views/dashboard/helpers';
|
import { asArray, rankRoleId, rankRoleName, rankRolePillLabel } from '@/views/dashboard/helpers';
|
||||||
|
|
||||||
function uniquePeriods(periods) {
|
function uniquePeriods(periods) {
|
||||||
|
|
@ -99,7 +100,7 @@ function periodLabelFrom(view) {
|
||||||
return `${firstDay}${firstDay && lastDay ? ' – ' : ''}${lastDay}`.trim();
|
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 view = dashboard?.view || {};
|
||||||
const groups = asArray(dashboard?.leaderboard);
|
const groups = asArray(dashboard?.leaderboard);
|
||||||
const periods = uniquePeriods(view.periods);
|
const periods = uniquePeriods(view.periods);
|
||||||
|
|
@ -108,7 +109,29 @@ function shapeView({ id, roleId, period, dashboard, item }) {
|
||||||
const title = view.name || item?.name || 'Leaderboard';
|
const title = view.name || item?.name || 'Leaderboard';
|
||||||
const periodLabel = periodLabelFrom(view);
|
const periodLabel = periodLabelFrom(view);
|
||||||
const hasTeams = isTeamView(view.team_view) || teamRows.length;
|
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 configured = rolePills.length > 0;
|
||||||
const pills = configured ? rolePills : withTeamPill(rolePills, id, period, roleId, hasTeams);
|
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() {
|
export default function LeaderboardDashboard() {
|
||||||
|
const { user } = useAuth();
|
||||||
const [searchParams, setSearchParams] = useSearchParams();
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
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();
|
||||||
|
|
@ -236,6 +260,7 @@ export default function LeaderboardDashboard() {
|
||||||
period,
|
period,
|
||||||
dashboard: {},
|
dashboard: {},
|
||||||
item: isSuccess(oneRes) ? oneRes.data?.item : null,
|
item: isSuccess(oneRes) ? oneRes.data?.item : null,
|
||||||
|
userRoles: user?.roles,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
setError(dashboardRes?.message || 'Unable to load leaderboard.');
|
setError(dashboardRes?.message || 'Unable to load leaderboard.');
|
||||||
|
|
@ -247,6 +272,7 @@ export default function LeaderboardDashboard() {
|
||||||
period,
|
period,
|
||||||
dashboard: dashboardRes.data || {},
|
dashboard: dashboardRes.data || {},
|
||||||
item: isSuccess(oneRes) ? oneRes.data?.item : null,
|
item: isSuccess(oneRes) ? oneRes.data?.item : null,
|
||||||
|
userRoles: user?.roles,
|
||||||
});
|
});
|
||||||
setView(next);
|
setView(next);
|
||||||
setError(next.error || '');
|
setError(next.error || '');
|
||||||
|
|
@ -258,7 +284,7 @@ export default function LeaderboardDashboard() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
load();
|
load();
|
||||||
}, [id, roleId, period]);
|
}, [id, roleId, period, user]);
|
||||||
|
|
||||||
const title = view?.title || 'Leaderboard';
|
const title = view?.title || 'Leaderboard';
|
||||||
const periods = view?.periods || [];
|
const periods = view?.periods || [];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue