refactor Party.svelte to use new utilities
- replace duplicate error extraction logic (2 locations) - replace duplicate skills transformation (3 locations) - replace complex slot-finding logic (40+ lines) - saves ~111 lines
This commit is contained in:
parent
112e8c39a9
commit
048cbeb1ad
1 changed files with 10 additions and 116 deletions
|
|
@ -23,6 +23,9 @@
|
||||||
import { Gender } from '$lib/utils/jobUtils'
|
import { Gender } from '$lib/utils/jobUtils'
|
||||||
import { openJobSelectionSidebar, openJobSkillSelectionSidebar } from '$lib/features/job/openJobSidebar.svelte'
|
import { openJobSelectionSidebar, openJobSkillSelectionSidebar } from '$lib/features/job/openJobSidebar.svelte'
|
||||||
import { partyAdapter } from '$lib/api/adapters/party.adapter'
|
import { partyAdapter } from '$lib/api/adapters/party.adapter'
|
||||||
|
import { extractErrorMessage } from '$lib/utils/errors'
|
||||||
|
import { transformSkillsToArray } from '$lib/utils/jobSkills'
|
||||||
|
import { findNextEmptySlot, SLOT_NOT_FOUND } from '$lib/utils/gridHelpers'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
party?: Party
|
party?: Party
|
||||||
|
|
@ -405,12 +408,7 @@
|
||||||
console.log('[Party] New skill:', skill)
|
console.log('[Party] New skill:', skill)
|
||||||
|
|
||||||
// Convert skills object to array format expected by API
|
// Convert skills object to array format expected by API
|
||||||
const skillsArray = Object.entries(updatedSkills)
|
const skillsArray = transformSkillsToArray(updatedSkills)
|
||||||
.filter(([_, skill]) => skill !== null && skill !== undefined)
|
|
||||||
.map(([slotKey, skill]) => ({
|
|
||||||
id: skill!.id,
|
|
||||||
slot: parseInt(slotKey)
|
|
||||||
}))
|
|
||||||
|
|
||||||
console.log('[Party] Skills array to send:', skillsArray)
|
console.log('[Party] Skills array to send:', skillsArray)
|
||||||
|
|
||||||
|
|
@ -420,33 +418,7 @@
|
||||||
)
|
)
|
||||||
party = updated
|
party = updated
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
// Extract detailed error message from nested structure
|
error = extractErrorMessage(e, 'Failed to update skill')
|
||||||
let errorDetails = e?.details
|
|
||||||
|
|
||||||
// Navigate through nested details structure
|
|
||||||
while (errorDetails?.details) {
|
|
||||||
errorDetails = errorDetails.details
|
|
||||||
}
|
|
||||||
|
|
||||||
if (errorDetails?.errors) {
|
|
||||||
if (errorDetails.errors.message) {
|
|
||||||
// Simple message format
|
|
||||||
error = errorDetails.errors.message
|
|
||||||
} else {
|
|
||||||
// Field-based errors
|
|
||||||
const errorMessages = Object.entries(errorDetails.errors)
|
|
||||||
.map(([field, messages]) => {
|
|
||||||
if (Array.isArray(messages)) {
|
|
||||||
return messages.join(', ')
|
|
||||||
}
|
|
||||||
return String(messages)
|
|
||||||
})
|
|
||||||
.join('; ')
|
|
||||||
error = errorMessages || e.message || 'Failed to update skill'
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
error = e?.message || 'Failed to update skill'
|
|
||||||
}
|
|
||||||
console.error('Failed to update skill:', e)
|
console.error('Failed to update skill:', e)
|
||||||
} finally {
|
} finally {
|
||||||
loading = false
|
loading = false
|
||||||
|
|
@ -466,12 +438,7 @@
|
||||||
console.log('[Party] Updated jobSkills after removal:', updatedSkills)
|
console.log('[Party] Updated jobSkills after removal:', updatedSkills)
|
||||||
|
|
||||||
// Convert skills object to array format expected by API
|
// Convert skills object to array format expected by API
|
||||||
const skillsArray = Object.entries(updatedSkills)
|
const skillsArray = transformSkillsToArray(updatedSkills)
|
||||||
.filter(([_, skill]) => skill !== null && skill !== undefined)
|
|
||||||
.map(([slotKey, skill]) => ({
|
|
||||||
id: skill!.id,
|
|
||||||
slot: parseInt(slotKey)
|
|
||||||
}))
|
|
||||||
|
|
||||||
console.log('[Party] Skills array to send after removal:', skillsArray)
|
console.log('[Party] Skills array to send after removal:', skillsArray)
|
||||||
|
|
||||||
|
|
@ -481,33 +448,7 @@
|
||||||
)
|
)
|
||||||
party = updated
|
party = updated
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
// Extract detailed error message from nested structure
|
error = extractErrorMessage(e, 'Failed to remove skill')
|
||||||
let errorDetails = e?.details
|
|
||||||
|
|
||||||
// Navigate through nested details structure
|
|
||||||
while (errorDetails?.details) {
|
|
||||||
errorDetails = errorDetails.details
|
|
||||||
}
|
|
||||||
|
|
||||||
if (errorDetails?.errors) {
|
|
||||||
if (errorDetails.errors.message) {
|
|
||||||
// Simple message format
|
|
||||||
error = errorDetails.errors.message
|
|
||||||
} else {
|
|
||||||
// Field-based errors
|
|
||||||
const errorMessages = Object.entries(errorDetails.errors)
|
|
||||||
.map(([field, messages]) => {
|
|
||||||
if (Array.isArray(messages)) {
|
|
||||||
return messages.join(', ')
|
|
||||||
}
|
|
||||||
return String(messages)
|
|
||||||
})
|
|
||||||
.join('; ')
|
|
||||||
error = errorMessages || e.message || 'Failed to remove skill'
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
error = e?.message || 'Failed to remove skill'
|
|
||||||
}
|
|
||||||
console.error('Failed to remove skill:', e)
|
console.error('Failed to remove skill:', e)
|
||||||
} finally {
|
} finally {
|
||||||
loading = false
|
loading = false
|
||||||
|
|
@ -529,12 +470,7 @@
|
||||||
delete updatedSkills[String(slot) as keyof typeof updatedSkills]
|
delete updatedSkills[String(slot) as keyof typeof updatedSkills]
|
||||||
|
|
||||||
// Convert skills object to array format expected by API
|
// Convert skills object to array format expected by API
|
||||||
const skillsArray = Object.entries(updatedSkills)
|
const skillsArray = transformSkillsToArray(updatedSkills)
|
||||||
.filter(([_, skill]) => skill !== null && skill !== undefined)
|
|
||||||
.map(([slotKey, skill]) => ({
|
|
||||||
id: skill!.id,
|
|
||||||
slot: parseInt(slotKey)
|
|
||||||
}))
|
|
||||||
|
|
||||||
const updated = await partyAdapter.updateJobSkills(
|
const updated = await partyAdapter.updateJobSkills(
|
||||||
party.shortcode,
|
party.shortcode,
|
||||||
|
|
@ -590,50 +526,8 @@
|
||||||
party = updated
|
party = updated
|
||||||
|
|
||||||
// Find next empty slot for continuous adding
|
// Find next empty slot for continuous adding
|
||||||
let nextEmptySlot = -999 // sentinel value meaning no empty slot found
|
const nextEmptySlot = findNextEmptySlot(party, activeTab)
|
||||||
|
if (nextEmptySlot !== SLOT_NOT_FOUND) {
|
||||||
if (activeTab === GridType.Weapon) {
|
|
||||||
// Check mainhand first (position -1)
|
|
||||||
if (!party.weapons.find((w) => w.position === -1 || w.mainhand)) {
|
|
||||||
nextEmptySlot = -1
|
|
||||||
} else {
|
|
||||||
// Check grid slots 0-8
|
|
||||||
for (let i = 0; i < 9; i++) {
|
|
||||||
if (!party.weapons.find((w) => w.position === i)) {
|
|
||||||
nextEmptySlot = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (activeTab === GridType.Summon) {
|
|
||||||
// Check main summon first (position -1)
|
|
||||||
if (!party.summons.find((s) => s.position === -1 || s.main)) {
|
|
||||||
nextEmptySlot = -1
|
|
||||||
} else {
|
|
||||||
// Check grid slots 0-5
|
|
||||||
for (let i = 0; i < 6; i++) {
|
|
||||||
if (!party.summons.find((s) => s.position === i)) {
|
|
||||||
nextEmptySlot = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Check friend summon (position 6)
|
|
||||||
if (nextEmptySlot === -999 && !party.summons.find((s) => s.position === 6 || s.friend)) {
|
|
||||||
nextEmptySlot = 6
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (activeTab === GridType.Character) {
|
|
||||||
// Check character slots 0-4
|
|
||||||
for (let i = 0; i < 5; i++) {
|
|
||||||
if (!party.characters.find((c) => c.position === i)) {
|
|
||||||
nextEmptySlot = i
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there's another empty slot, update selectedSlot to it
|
|
||||||
if (nextEmptySlot !== -999) {
|
|
||||||
selectedSlot = nextEmptySlot
|
selectedSlot = nextEmptySlot
|
||||||
}
|
}
|
||||||
// Note: Sidebar stays open for continuous adding
|
// Note: Sidebar stays open for continuous adding
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue