Added fallback for token statuses empty values and duplicates

This commit is contained in:
Mitchell McCaffrey 2021-01-21 15:55:47 +11:00
parent 2f9a55f176
commit 34fcff9052
2 changed files with 8 additions and 7 deletions

View File

@ -59,15 +59,14 @@ function TokenMenu({
if (!tokenState) { if (!tokenState) {
return; return;
} }
const statuses = tokenState.statuses; const statuses = new Set(tokenState.statuses.filter((s) => s));
let newStatuses = []; if (statuses.has(status)) {
if (statuses.includes(status)) { statuses.delete(status);
newStatuses = statuses.filter((s) => s !== status);
} else { } else {
newStatuses = [...statuses, status]; statuses.add(status);
} }
onTokenStateChange({ onTokenStateChange({
[tokenState.id]: { ...tokenState, statuses: newStatuses }, [tokenState.id]: { ...tokenState, statuses: [...statuses] },
}); });
} }

View File

@ -4,9 +4,11 @@ import { Circle, Group } from "react-konva";
import colors from "../../helpers/colors"; import colors from "../../helpers/colors";
function TokenStatus({ tokenState, width, height }) { function TokenStatus({ tokenState, width, height }) {
// Ensure statuses is an array and filter empty values
const statuses = [...new Set((tokenState?.statuses || []).filter((s) => s))];
return ( return (
<Group x={width} y={height} offsetX={width / 2} offsetY={height / 2}> <Group x={width} y={height} offsetX={width / 2} offsetY={height / 2}>
{tokenState.statuses.map((status, index) => ( {statuses.map((status, index) => (
<Circle <Circle
key={status} key={status}
width={width} width={width}