-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSpellNameField.tsx
More file actions
100 lines (85 loc) · 3.13 KB
/
Copy pathSpellNameField.tsx
File metadata and controls
100 lines (85 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { ChangeEvent, ChangeEventHandler, useState } from "react";
import { useAppDispatch, useAppSelector } from "../../stores/hooks";
import { acceptSuggestionThunk, fetchSpells } from "../../stores/thunks";
import { updateActiveCard } from "../../stores/uiStateReducer";
import { SrdType } from "../../utils/models"
function SuggestedSpells({
input,
allSrdSpells,
acceptSuggestion,
}: {
input: string,
allSrdSpells: SrdType[],
acceptSuggestion: Function,
}) {
let message = <p className="text-xs">Start typing, and I’ll look for your spell in the SRD database</p>;
let possibleSpells: SrdType[] = [];
if (input.length < 2) {
possibleSpells = [];
} else {
possibleSpells = allSrdSpells.filter((item) => {
return item.name.toLowerCase().includes(input.toLowerCase())
})
}
if(possibleSpells.length === 0 && input.length >= 2) {
const searchQuery = input.toLowerCase().split(" ").join("+");
message = <p className="text-xs">
Custom spell! <a className="text-blue-700" href={`https://www.google.com/search?q=dnd+5e+${searchQuery}`} rel="noreferrer" target="_blank">Google “dnd 5e {input}”</a>
</p>
}
const select = (sp: SrdType) => {
acceptSuggestion(sp)
}
const spellsList = possibleSpells.map((sp) => {
return (
<button key={sp.index} onClick={() => select(sp)} className="flex w-full pl-1 border-b justify-start hover:bg-slate-200">
<span className="flex-grow text-left">{sp.name}</span>
<span className="text-green-700 inline-block px-2 bg-green-300 bg-opacity-30">+</span>
</button>
)
})
return (
<div className="block">
{ message }
{ spellsList.length > 0 && (<div className="border border-b-0">
{ spellsList }
</div>)}
</div>
)
}
function SpellNameField() {
const cardData = useAppSelector((state) => state.ui.activeCardData);
const srdSpells = useAppSelector((state) => state.ui.srdSpells);
const dispatch = useAppDispatch();
const [showSuggestions, setShowSuggestions] = useState(cardData.name.length === 0);
if(srdSpells.length == 0) {
console.log("huh, you're here");
dispatch(fetchSpells());
}
const setName = (newName: string) => {
const newData = {...cardData};
newData.name = newName;
dispatch(updateActiveCard(newData));
}
const acceptSuggestion = (srd: SrdType) => {
// setName(srd.name);
// const srdData = SpellApiService.get(srd.index);
// srdData.then((data: SpellType) => {
// dispatch(updateActiveCard(data));
// })
dispatch(acceptSuggestionThunk(srd));
setShowSuggestions(false);
}
const onChange: ChangeEventHandler = (event: ChangeEvent<HTMLInputElement>) => {
setShowSuggestions(true);
setName(event.target.value);
}
return (
<>
<label className="font-bold text-sm" htmlFor='name'>Spell Name</label>
<input value={ cardData.name } className="border w-full block px-1" type="text" name='name' onChange={ onChange } />
{ showSuggestions && <SuggestedSpells input={ cardData.name } allSrdSpells={ srdSpells } acceptSuggestion={ acceptSuggestion } /> }
</>
)
}
export default SpellNameField