frontend: refactor of the new build viewer
extracting the logic into composables
This commit is contained in:
33
frontend/composables/useSummonerSpellMap.ts
Normal file
33
frontend/composables/useSummonerSpellMap.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Composable for fetching and managing summoner spell data from CDragon API
|
||||
* Returns a reactive Map of spell ID to spell data
|
||||
*/
|
||||
export const useSummonerSpellMap = () => {
|
||||
const { data: summonerSpellsData } = useFetch<Array<SummonerSpell>>(
|
||||
'/api/cdragon/summoner-spells',
|
||||
{
|
||||
lazy: true,
|
||||
server: false
|
||||
}
|
||||
)
|
||||
|
||||
const summonerSpellMap = ref<Map<number, SummonerSpell>>(new Map())
|
||||
|
||||
watch(
|
||||
summonerSpellsData,
|
||||
(newData) => {
|
||||
if (Array.isArray(newData)) {
|
||||
const map = new Map<number, SummonerSpell>()
|
||||
for (const spell of newData) {
|
||||
if (spell?.id) {
|
||||
map.set(spell.id, spell)
|
||||
}
|
||||
}
|
||||
summonerSpellMap.value = map
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
)
|
||||
|
||||
return { summonerSpellMap }
|
||||
}
|
||||
Reference in New Issue
Block a user