Skip to content

Commit

Permalink
SpellList class
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Ladyman committed May 18, 2024
1 parent 229e6a2 commit 1f3d94b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dnd-battle-tracker",
"version": "5.106.1",
"version": "5.107.0",
"private": true,
"dependencies": {
"@apollo/client": "^3.9.2",
Expand Down
8 changes: 4 additions & 4 deletions src/components/app/DungeonMasterApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ import {
dismissErrors,
updateErrors,
} from '../../state/ErrorManager';
import { getSpells } from '../../client/dnd5eapi';
import { getSpellList } from '../../domain/spellcasting';
import SrdContext from './SrdContext';

function DungeonMasterApp({
state, setState, shareBattle, onlineError,
}) {
const [srdSpells, setSrdSpells] = useState([]);
const [spellList, setSpellList] = useState([]);
const creaturesRef = useRef(null);

const updateBattle = (update, doShare = true) => (...args) => {
Expand Down Expand Up @@ -112,7 +112,7 @@ function DungeonMasterApp({
} = state;

useEffect(() => {
getSpells().then(setSrdSpells);
getSpellList().then(setSpellList);
}, []);

useEffect(() => {
Expand Down Expand Up @@ -158,7 +158,7 @@ function DungeonMasterApp({
loadBattle,
}), [state]);

const srd = useMemo(() => ({ srdSpells }));
const srd = useMemo(() => ({ spellList }));

const onScrollActiveInitiative = () => {
creaturesRef.current.scrollToCreature(activeCreatureId);
Expand Down
15 changes: 2 additions & 13 deletions src/components/creature/toolbar/tools/spellcasting/SpellList.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,6 @@ function Spells({
);
}

function getFilteredSpells(name, spellData) {
if (name.length < 2) return [];
return spellData
.filter((spell) => spell.name && spell.name.toLowerCase().includes(name.toLowerCase()))
.map((spell) => ({
...spell,
text: spell.name,
id: spell.index,
}));
}

export default function SpellList({
spells,
spellProperty,
Expand All @@ -110,7 +99,7 @@ export default function SpellList({
}) {
const [name, setName] = useState('');
const srd = useContext(SrdContext);
const { srdSpells } = srd;
const { spellList } = srd;
const spellRightControls = {
rightTitle: 'Add spell',
RightSubmitIcon: <CrossIcon />,
Expand All @@ -121,7 +110,7 @@ export default function SpellList({
<ComboboxList
value={name}
setValue={setName}
list={getFilteredSpells(name, srdSpells)}
list={spellList.search(name)}
id={`${creatureId}-${id}-spells`}
dropdownId={`${creatureId}-${id}-spells-dropdown`}
dropdownLabel="Select spell"
Expand Down
23 changes: 23 additions & 0 deletions src/domain/spellcasting.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
import { getSpells } from '../client/dnd5eapi';

class SpellList {
constructor(spells) {
this.spells = spells;
}

search(name) {
if (name.length < 2) return [];
return this.spells
.filter((spell) => spell.name && spell.name.toLowerCase().includes(name.toLowerCase()))
.map((spell) => ({
text: spell.name,
id: spell.index,
}));
}
}

export async function getSpellList() {
const spells = await getSpells();
return new SpellList(spells);
}

export const maxSpellSlots = {
'1st': 4,
'2nd': 3,
Expand Down

0 comments on commit 1f3d94b

Please sign in to comment.