feat: improve character update logic and handle haki fields more effectively
This commit is contained in:
@@ -85,24 +85,33 @@ export const actions: Actions = {
|
||||
}
|
||||
|
||||
try {
|
||||
const updates: Record<string, any> = {
|
||||
// Initialize boolean fields to false (they'll be set to true if present in formData)
|
||||
hakiObservation: false,
|
||||
hakiArmament: false,
|
||||
hakiConqueror: false
|
||||
};
|
||||
const [originalCharacter] = await db
|
||||
.select({
|
||||
hakiObservation: character.hakiObservation,
|
||||
hakiArmament: character.hakiArmament,
|
||||
hakiConqueror: character.hakiConqueror
|
||||
})
|
||||
.from(character)
|
||||
.where(eq(character.id, id))
|
||||
.limit(1);
|
||||
|
||||
if (!originalCharacter) {
|
||||
return fail(404, { error: 'Character not found' });
|
||||
}
|
||||
|
||||
const updates: Record<string, any> = {};
|
||||
|
||||
formData.forEach((value, key) => {
|
||||
if (key !== 'id') {
|
||||
// Handle checkboxes (haki fields)
|
||||
if (key === 'hakiObservation' || key === 'hakiArmament' || key === 'hakiConqueror') {
|
||||
updates[key] = value === 'on';
|
||||
}
|
||||
// Handle integers (age, bounty, height, devilFruitId, arcId)
|
||||
else if (key === 'age' || key === 'bounty' || key === 'height' || key === 'devilFruitId' || key === 'arcId') {
|
||||
if (key === 'age' || key === 'bounty' || key === 'height' || key === 'devilFruitId' || key === 'arcId') {
|
||||
const strValue = value as string;
|
||||
updates[key] = strValue && strValue !== '' ? parseInt(strValue) : null;
|
||||
}
|
||||
// Handle checkboxes (haki fields) after parsing all form data
|
||||
else if (key === 'hakiObservation' || key === 'hakiArmament' || key === 'hakiConqueror') {
|
||||
return;
|
||||
}
|
||||
// Handle strings (name, gender, status, origin, affiliations, epithets, pictureUrl, url, firstAppearance)
|
||||
else {
|
||||
updates[key] = value || null;
|
||||
@@ -110,6 +119,17 @@ export const actions: Actions = {
|
||||
}
|
||||
});
|
||||
|
||||
const submittedHakiObservation = formData.has('hakiObservation');
|
||||
const submittedHakiArmament = formData.has('hakiArmament');
|
||||
const submittedHakiConqueror = formData.has('hakiConqueror');
|
||||
|
||||
updates.hakiObservation =
|
||||
submittedHakiObservation === originalCharacter.hakiObservation ? null : submittedHakiObservation;
|
||||
updates.hakiArmament =
|
||||
submittedHakiArmament === originalCharacter.hakiArmament ? null : submittedHakiArmament;
|
||||
updates.hakiConqueror =
|
||||
submittedHakiConqueror === originalCharacter.hakiConqueror ? null : submittedHakiConqueror;
|
||||
|
||||
// Update or insert into characterOverride table
|
||||
await db
|
||||
.insert(characterOverride)
|
||||
|
||||
Reference in New Issue
Block a user