refactor Party.svelte grid operations
- replace handleSwap/handleMove with executeGridOperation - replace 3 remove methods with generic removeGridItem - replace 3 update methods with generic updateGridItem - saves ~103 lines
This commit is contained in:
parent
5919548bc1
commit
e6cdfa210a
1 changed files with 47 additions and 123 deletions
|
|
@ -26,6 +26,7 @@
|
||||||
import { extractErrorMessage } from '$lib/utils/errors'
|
import { extractErrorMessage } from '$lib/utils/errors'
|
||||||
import { transformSkillsToArray } from '$lib/utils/jobSkills'
|
import { transformSkillsToArray } from '$lib/utils/jobSkills'
|
||||||
import { findNextEmptySlot, SLOT_NOT_FOUND } from '$lib/utils/gridHelpers'
|
import { findNextEmptySlot, SLOT_NOT_FOUND } from '$lib/utils/gridHelpers'
|
||||||
|
import { executeGridOperation, removeGridItem, updateGridItem } from '$lib/utils/gridOperations'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
party?: Party
|
party?: Party
|
||||||
|
|
@ -125,40 +126,14 @@
|
||||||
throw new Error('Cannot swap items in unsaved party')
|
throw new Error('Cannot swap items in unsaved party')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Both source and target should have items for swap
|
return executeGridOperation(
|
||||||
if (!source.itemId || !target.itemId) {
|
'swap',
|
||||||
throw new Error('Invalid swap operation - missing items')
|
source,
|
||||||
}
|
target,
|
||||||
|
{ partyId: party.id, shortcode: party.shortcode, editKey },
|
||||||
// Call appropriate grid service method based on type
|
gridService,
|
||||||
if (source.type === 'weapon') {
|
partyService
|
||||||
await gridService.moveWeapon(party.id, source.itemId, target.position, editKey || undefined, {
|
|
||||||
shortcode: party.shortcode
|
|
||||||
})
|
|
||||||
} else if (source.type === 'character') {
|
|
||||||
await gridService.moveCharacter(
|
|
||||||
party.id,
|
|
||||||
source.itemId,
|
|
||||||
target.position,
|
|
||||||
editKey || undefined,
|
|
||||||
{
|
|
||||||
shortcode: party.shortcode
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
} else if (source.type === 'summon') {
|
|
||||||
await gridService.moveSummon(party.id, source.itemId, target.position, editKey || undefined, {
|
|
||||||
shortcode: party.shortcode
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
throw new Error(`Unknown item type: ${source.type}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear cache and refresh party data
|
|
||||||
partyService.clearPartyCache(party.shortcode)
|
|
||||||
const updated = await partyService.getByShortcode(party.shortcode)
|
|
||||||
return updated
|
|
||||||
|
|
||||||
throw new Error(`Unknown item type: ${source.type}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleMove(source: any, target: any): Promise<Party> {
|
async function handleMove(source: any, target: any): Promise<Party> {
|
||||||
|
|
@ -166,36 +141,14 @@
|
||||||
throw new Error('Cannot move items in unsaved party')
|
throw new Error('Cannot move items in unsaved party')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Source should have an item, target should be empty
|
return executeGridOperation(
|
||||||
if (!source.itemId || target.itemId) {
|
'move',
|
||||||
throw new Error('Invalid move operation')
|
source,
|
||||||
}
|
target,
|
||||||
|
{ partyId: party.id, shortcode: party.shortcode, editKey },
|
||||||
// Call appropriate grid service method based on type
|
gridService,
|
||||||
if (source.type === 'character') {
|
partyService
|
||||||
await gridService.moveCharacter(
|
|
||||||
party.id,
|
|
||||||
source.itemId,
|
|
||||||
target.position,
|
|
||||||
editKey || undefined,
|
|
||||||
{ shortcode: party.shortcode }
|
|
||||||
)
|
)
|
||||||
} else if (source.type === 'weapon') {
|
|
||||||
await gridService.moveWeapon(party.id, source.itemId, target.position, editKey || undefined, {
|
|
||||||
shortcode: party.shortcode
|
|
||||||
})
|
|
||||||
} else if (source.type === 'summon') {
|
|
||||||
await gridService.moveSummon(party.id, source.itemId, target.position, editKey || undefined, {
|
|
||||||
shortcode: party.shortcode
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
throw new Error(`Unknown item type: ${source.type}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear cache and refresh party data
|
|
||||||
partyService.clearPartyCache(party.shortcode)
|
|
||||||
const updated = await partyService.getByShortcode(party.shortcode)
|
|
||||||
return updated
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Localized name helper: accepts either an object with { name: { en, ja } }
|
// Localized name helper: accepts either an object with { name: { en, ja } }
|
||||||
|
|
@ -554,17 +507,15 @@
|
||||||
const clientGridService = {
|
const clientGridService = {
|
||||||
async removeWeapon(partyId: string, gridWeaponId: string, _editKey?: string) {
|
async removeWeapon(partyId: string, gridWeaponId: string, _editKey?: string) {
|
||||||
try {
|
try {
|
||||||
// Remove returns null, so we need to update local state
|
return await removeGridItem(
|
||||||
await gridService.removeWeapon(partyId, gridWeaponId, editKey || undefined, {
|
'weapon',
|
||||||
shortcode: party.shortcode
|
partyId,
|
||||||
})
|
gridWeaponId,
|
||||||
|
party,
|
||||||
// Update local state by removing the weapon
|
party.shortcode,
|
||||||
const updatedParty = { ...party }
|
editKey,
|
||||||
if (updatedParty.weapons) {
|
gridService
|
||||||
updatedParty.weapons = updatedParty.weapons.filter((w: any) => w.id !== gridWeaponId)
|
)
|
||||||
}
|
|
||||||
return updatedParty
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to remove weapon:', err)
|
console.error('Failed to remove weapon:', err)
|
||||||
throw err
|
throw err
|
||||||
|
|
@ -572,17 +523,15 @@
|
||||||
},
|
},
|
||||||
async removeSummon(partyId: string, gridSummonId: string, _editKey?: string) {
|
async removeSummon(partyId: string, gridSummonId: string, _editKey?: string) {
|
||||||
try {
|
try {
|
||||||
// Remove returns null, so we need to update local state
|
return await removeGridItem(
|
||||||
await gridService.removeSummon(partyId, gridSummonId, editKey || undefined, {
|
'summon',
|
||||||
shortcode: party.shortcode
|
partyId,
|
||||||
})
|
gridSummonId,
|
||||||
|
party,
|
||||||
// Update local state by removing the summon
|
party.shortcode,
|
||||||
const updatedParty = { ...party }
|
editKey,
|
||||||
if (updatedParty.summons) {
|
gridService
|
||||||
updatedParty.summons = updatedParty.summons.filter((s: any) => s.id !== gridSummonId)
|
)
|
||||||
}
|
|
||||||
return updatedParty
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to remove summon:', err)
|
console.error('Failed to remove summon:', err)
|
||||||
throw err
|
throw err
|
||||||
|
|
@ -590,19 +539,15 @@
|
||||||
},
|
},
|
||||||
async removeCharacter(partyId: string, gridCharacterId: string, _editKey?: string) {
|
async removeCharacter(partyId: string, gridCharacterId: string, _editKey?: string) {
|
||||||
try {
|
try {
|
||||||
// Remove returns null, so we need to update local state
|
return await removeGridItem(
|
||||||
await gridService.removeCharacter(partyId, gridCharacterId, editKey || undefined, {
|
'character',
|
||||||
shortcode: party.shortcode
|
partyId,
|
||||||
})
|
gridCharacterId,
|
||||||
|
party,
|
||||||
// Update local state by removing the character
|
party.shortcode,
|
||||||
const updatedParty = { ...party }
|
editKey,
|
||||||
if (updatedParty.characters) {
|
gridService
|
||||||
updatedParty.characters = updatedParty.characters.filter(
|
|
||||||
(c: any) => c.id !== gridCharacterId
|
|
||||||
)
|
)
|
||||||
}
|
|
||||||
return updatedParty
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to remove character:', err)
|
console.error('Failed to remove character:', err)
|
||||||
throw err
|
throw err
|
||||||
|
|
@ -610,14 +555,7 @@
|
||||||
},
|
},
|
||||||
async updateWeapon(partyId: string, gridWeaponId: string, updates: any, _editKey?: string) {
|
async updateWeapon(partyId: string, gridWeaponId: string, updates: any, _editKey?: string) {
|
||||||
try {
|
try {
|
||||||
// Use the grid service to update weapon
|
return await updateGridItem('weapon', partyId, gridWeaponId, updates, editKey, gridService)
|
||||||
const updated = await gridService.updateWeapon(
|
|
||||||
partyId,
|
|
||||||
gridWeaponId,
|
|
||||||
updates,
|
|
||||||
editKey || undefined
|
|
||||||
)
|
|
||||||
return updated
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to update weapon:', err)
|
console.error('Failed to update weapon:', err)
|
||||||
throw err
|
throw err
|
||||||
|
|
@ -625,14 +563,7 @@
|
||||||
},
|
},
|
||||||
async updateSummon(partyId: string, gridSummonId: string, updates: any, _editKey?: string) {
|
async updateSummon(partyId: string, gridSummonId: string, updates: any, _editKey?: string) {
|
||||||
try {
|
try {
|
||||||
// Use the grid service to update summon
|
return await updateGridItem('summon', partyId, gridSummonId, updates, editKey, gridService)
|
||||||
const updated = await gridService.updateSummon(
|
|
||||||
partyId,
|
|
||||||
gridSummonId,
|
|
||||||
updates,
|
|
||||||
editKey || undefined
|
|
||||||
)
|
|
||||||
return updated
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to update summon:', err)
|
console.error('Failed to update summon:', err)
|
||||||
throw err
|
throw err
|
||||||
|
|
@ -645,14 +576,7 @@
|
||||||
_editKey?: string
|
_editKey?: string
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
// Use the grid service to update character
|
return await updateGridItem('character', partyId, gridCharacterId, updates, editKey, gridService)
|
||||||
const updated = await gridService.updateCharacter(
|
|
||||||
partyId,
|
|
||||||
gridCharacterId,
|
|
||||||
updates,
|
|
||||||
editKey || undefined
|
|
||||||
)
|
|
||||||
return updated
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to update character:', err)
|
console.error('Failed to update character:', err)
|
||||||
throw err
|
throw err
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue