Peter
This commit is contained in:
@@ -105,44 +105,102 @@ const GameMasterPage = () => {
|
||||
setIsEditing(true);
|
||||
};
|
||||
|
||||
// Update handleUpdate to fetch once after saving
|
||||
const handleUpdate = async () => {
|
||||
try {
|
||||
let response;
|
||||
if (editType === 'item') {
|
||||
response = await axios.put(`http://localhost:5000/games/item/${selectedItem.ItemID}`, formData);
|
||||
} else if (editType === 'npc') {
|
||||
response = await axios.put(`http://localhost:5000/games/npc/${selectedItem.NPCID}`, formData);
|
||||
} else if (editType === 'character') {
|
||||
response = await axios.put(`http://localhost:5000/games/character/${selectedItem.CharID}`, formData);
|
||||
}
|
||||
|
||||
if (response.status === 200) {
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
const pcsResponse = await axios.get(`http://localhost:5000/games/${gameId}/playerchars`);
|
||||
const processedPCs = Array.isArray(pcsResponse.data) ? pcsResponse.data : [pcsResponse.data];
|
||||
setPlayerCharacters(processedPCs.filter(pc => pc !== null));
|
||||
|
||||
const npcsResponse = await axios.get(`http://localhost:5000/games/${gameId}/npcs`);
|
||||
const processedNPCs = Array.isArray(npcsResponse.data) ? npcsResponse.data : [npcsResponse.data];
|
||||
setNpcs(processedNPCs.filter(npc => npc !== null));
|
||||
|
||||
const itemsResponse = await axios.get(`http://localhost:5000/games/${gameId}/items`);
|
||||
const processedItems = Array.isArray(itemsResponse.data) ? itemsResponse.data : [itemsResponse.data];
|
||||
setItems(processedItems.filter(item => item !== null));
|
||||
} catch (error) {
|
||||
console.error('Error fetching data:', error);
|
||||
}
|
||||
};
|
||||
await fetchData(); // Fetch once after successful update
|
||||
setEditModalOpen(false);
|
||||
setFormData(null);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating:', error);
|
||||
// Update the handleUpdate function
|
||||
const handleUpdate = async () => {
|
||||
try {
|
||||
let response;
|
||||
if (editType === 'item') {
|
||||
const itemData = {
|
||||
ItemName: formData.ItemName,
|
||||
Type: formData.Type,
|
||||
Art: formData.Art,
|
||||
Rarity: formData.Rarity,
|
||||
MaxDurability: formData.MaxDurability,
|
||||
CurrentDurability: formData.CurrentDurability,
|
||||
GoldValue: formData.GoldValue,
|
||||
Abilities: formData.Abilities,
|
||||
OwnerID: formData.OwnerID,
|
||||
AP: formData.AP
|
||||
};
|
||||
response = await axios.put(`http://localhost:5000/games/item/${selectedItem.ItemID}`, itemData);
|
||||
} else if (editType === 'npc') {
|
||||
// Update NPC data
|
||||
const npcData = {
|
||||
Name: formData.Name,
|
||||
Race: formData.Race,
|
||||
Sex: formData.Sex,
|
||||
Age: formData.Age,
|
||||
Job: formData.Job,
|
||||
Description: formData.Description,
|
||||
MaxHealth: formData.MaxHealth,
|
||||
CurrentHealth: formData.CurrentHealth,
|
||||
MaxMana: formData.MaxMana,
|
||||
CurrentMana: formData.CurrentMana,
|
||||
Strength: formData.Strength,
|
||||
Dexterity: formData.Dexterity,
|
||||
Agility: formData.Agility,
|
||||
Endurance: formData.Endurance,
|
||||
Level: formData.Level,
|
||||
Allied: formData.Allied
|
||||
};
|
||||
response = await axios.put(`http://localhost:5000/games/npc/${selectedItem.NpcID}`, npcData);
|
||||
} else if (editType === 'character') {
|
||||
// Update Player Character data
|
||||
const charData = {
|
||||
charName: formData.CharName,
|
||||
race: formData.Race,
|
||||
sex: formData.Sex,
|
||||
age: formData.Age,
|
||||
job: formData.Job,
|
||||
description: formData.Description,
|
||||
maxHealth: formData.MaxHealth,
|
||||
currentHealth: formData.CurrentHealth,
|
||||
maxMana: formData.MaxMana,
|
||||
currentMana: formData.CurrentMana,
|
||||
strength: formData.Strength,
|
||||
dexterity: formData.Dexterity,
|
||||
agility: formData.Agility,
|
||||
endurance: formData.Endurance,
|
||||
level: formData.Level,
|
||||
gold: formData.Gold
|
||||
};
|
||||
response = await axios.put(`http://localhost:5000/games/character/${selectedItem.CharID}`, charData);
|
||||
}
|
||||
};
|
||||
|
||||
if (response.status === 200) {
|
||||
// Fetch updated data after successful update
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
// Fetch player characters
|
||||
const pcsResponse = await axios.get(`http://localhost:5000/games/${gameId}/playerchars`);
|
||||
setPlayerCharacters(
|
||||
Array.isArray(pcsResponse.data) ? pcsResponse.data : [pcsResponse.data]
|
||||
);
|
||||
|
||||
// Fetch NPCs
|
||||
const npcsResponse = await axios.get(`http://localhost:5000/games/${gameId}/npcs`);
|
||||
setNpcs(
|
||||
Array.isArray(npcsResponse.data) ? npcsResponse.data : [npcsResponse.data]
|
||||
);
|
||||
|
||||
// Fetch items
|
||||
const itemsResponse = await axios.get(`http://localhost:5000/games/${gameId}/items`);
|
||||
setItems(
|
||||
Array.isArray(itemsResponse.data) ? itemsResponse.data : [itemsResponse.data]
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error fetching updated data:', error);
|
||||
}
|
||||
};
|
||||
|
||||
await fetchData();
|
||||
setEditModalOpen(false);
|
||||
setFormData(null);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleModalClose = () => {
|
||||
setEditModalOpen(false);
|
||||
|
||||
Reference in New Issue
Block a user