173 lines
7.5 KiB
JavaScript
173 lines
7.5 KiB
JavaScript
import { Controller, useWatch } from 'react-hook-form';
|
|
import Select from 'react-select';
|
|
import { Button, Form, Table } from 'react-bootstrap';
|
|
|
|
function patchRow(rows, index, patch) {
|
|
const next = [...(rows || [])];
|
|
next[index] = { ...next[index], ...patch };
|
|
return next;
|
|
}
|
|
|
|
function emptyRow() {
|
|
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
|
|
name={field.key}
|
|
control={control}
|
|
render={({ field: rhf }) => {
|
|
const rows = rhf.value || [];
|
|
return (
|
|
<div>
|
|
<div className="d-flex justify-content-end mb-2">
|
|
<Button variant="info" size="sm" type="button" onClick={() => rhf.onChange([...rows, emptyRow()])}>
|
|
Add Rank Role
|
|
</Button>
|
|
</div>
|
|
<div className="table-responsive">
|
|
<Table className="mb-0 table-sm">
|
|
<thead>
|
|
<tr>
|
|
<th>Title</th>
|
|
<th>Role</th>
|
|
<th>View Mode</th>
|
|
{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>
|
|
<th />
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{rows.map((row, index) => (
|
|
<tr key={index}>
|
|
<td>
|
|
<Form.Control
|
|
value={row.title || ''}
|
|
onChange={(e) => rhf.onChange(patchRow(rows, index, { title: e.target.value }))}
|
|
/>
|
|
</td>
|
|
<td>
|
|
<Form.Select
|
|
disabled={isTeam && row.show_as === 'team'}
|
|
value={row.role || ''}
|
|
onChange={(e) => rhf.onChange(patchRow(rows, index, { role: e.target.value }))}
|
|
>
|
|
<option value=""> -- Select Role -- </option>
|
|
{Object.entries(roles).map(([value, label]) => (
|
|
<option key={value} value={value}>
|
|
{label}
|
|
</option>
|
|
))}
|
|
</Form.Select>
|
|
</td>
|
|
<td>
|
|
<Form.Select
|
|
value={['versus', 'podium', 'list'].includes(row.view_mode) ? row.view_mode : 'versus'}
|
|
onChange={(e) => rhf.onChange(patchRow(rows, index, { view_mode: e.target.value }))}
|
|
>
|
|
<option value="versus">Versus</option>
|
|
<option value="podium">Podium</option>
|
|
<option value="list">List</option>
|
|
</Form.Select>
|
|
</td>
|
|
{isTeam ? (
|
|
<td>
|
|
<Form.Select
|
|
value={row.show_as === 'team' ? 'team' : 'user'}
|
|
onChange={(e) => rhf.onChange(patchRow(rows, index, {
|
|
show_as: e.target.value,
|
|
...(e.target.value === 'team' ? { role: '', scope: 'domain' } : {}),
|
|
}))}
|
|
>
|
|
<option value="user">Users</option>
|
|
<option value="team">Teams</option>
|
|
</Form.Select>
|
|
</td>
|
|
) : null}
|
|
{isTeam ? (
|
|
<td>
|
|
<Form.Select
|
|
disabled={row.show_as === 'team'}
|
|
value={row.scope === 'own_team' ? 'own_team' : 'domain'}
|
|
onChange={(e) => rhf.onChange(patchRow(rows, index, { scope: e.target.value }))}
|
|
>
|
|
<option value="domain">Domain wise</option>
|
|
<option value="own_team">Own team</option>
|
|
</Form.Select>
|
|
</td>
|
|
) : null}
|
|
<td>
|
|
<Form.Control
|
|
type="number"
|
|
min={1}
|
|
value={row.limit ?? 10}
|
|
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"
|
|
min={0}
|
|
value={row.highlight_count ?? 0}
|
|
onChange={(e) => rhf.onChange(patchRow(rows, index, { highlight_count: e.target.value }))}
|
|
/>
|
|
</td>
|
|
<td>
|
|
<Form.Control
|
|
type="color"
|
|
value={/^#[0-9A-Fa-f]{6}$/.test(row.highlight_color || '') ? row.highlight_color : '#ffffff'}
|
|
onChange={(e) => rhf.onChange(patchRow(rows, index, { highlight_color: e.target.value }))}
|
|
/>
|
|
</td>
|
|
<td>
|
|
<Form.Control
|
|
type="number"
|
|
value={row.sort_order ?? 0}
|
|
onChange={(e) => rhf.onChange(patchRow(rows, index, { sort_order: e.target.value }))}
|
|
/>
|
|
</td>
|
|
<td>
|
|
<Button
|
|
variant="soft-danger"
|
|
size="sm"
|
|
type="button"
|
|
onClick={() => rhf.onChange(rows.filter((_, i) => i !== index))}
|
|
>
|
|
Remove
|
|
</Button>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</Table>
|
|
</div>
|
|
{rows.length === 0 ? <div className="text-muted fs-12 mt-1">No rank roles</div> : null}
|
|
</div>
|
|
);
|
|
}}
|
|
/>
|
|
);
|
|
}
|