🚀 Refactor GamemasterManager to use reusable async state management for loading, saving, and error handling

This commit is contained in:
2026-01-28 22:41:56 +00:00
parent d5ba1d5ab1
commit 0515fb7958
2 changed files with 70 additions and 59 deletions

View File

@@ -0,0 +1,8 @@
/**
* Shared Components Index
*
* Centralized exports for all shared components
*/
export { default as BaseButton } from './BaseButton.vue';
export { default as BaseModal } from './BaseModal.vue';

View File

@@ -188,6 +188,8 @@ const status = await gm.getStatus();</code></pre>
<script setup> <script setup>
import { ref, computed, onMounted } from 'vue'; import { ref, computed, onMounted } from 'vue';
import { useAsyncState } from '../composables/useAsyncState.js';
import { apiClient } from '../utilities/api-client.js';
import { import {
fetchLatestGamemaster, fetchLatestGamemaster,
breakUpGamemaster, breakUpGamemaster,
@@ -195,49 +197,75 @@ import {
getGamemasterStats getGamemasterStats
} from '../utilities/gamemaster-utils.js'; } from '../utilities/gamemaster-utils.js';
const loading = ref(false); // Load server status state
const error = ref(null); const {
const saving = ref(false); execute: loadServerStatus,
const saveSuccess = ref(false); data: serverStatus,
const rawGamemaster = ref(null); loading: statusLoading,
error: statusError
} = useAsyncState(
async () => {
const response = await apiClient.get('/api/gamemaster/status');
return response;
},
null
);
// Fetch gamemaster state
const {
execute: fetchGamemasterData,
data: rawGamemaster,
loading: gamemasterLoading,
error: gamemasterError,
reset: resetGamemaster
} = useAsyncState(
async () => {
const data = await fetchLatestGamemaster();
return data;
},
null
);
// Save to server state
const {
execute: saveToServerData,
loading: saving,
error: saveError,
reset: resetSave
} = useAsyncState(
async () => {
const result = await apiClient.post('/api/gamemaster/process', {});
// Reload server status after save
await loadServerStatus();
return result;
},
null
);
// Process gamemaster data
const processedData = ref(null); const processedData = ref(null);
const serverStatus = ref(null); const saveSuccess = ref(false);
// Combine error states for template
const error = computed(() => gamemasterError.value || statusError.value || saveError.value);
const stats = computed(() => { const stats = computed(() => {
if (!processedData.value) return null; if (!processedData.value) return null;
return getGamemasterStats(processedData.value); return getGamemasterStats(processedData.value);
}); });
const loading = computed(() => statusLoading.value || gamemasterLoading.value);
onMounted(async () => { onMounted(async () => {
// Load server status on component mount // Load server status on component mount
await loadServerStatus(); await loadServerStatus();
}); });
async function loadServerStatus() { // Replace manual loading state with async function
try {
const response = await fetch('/api/gamemaster/status');
if (response.ok) {
serverStatus.value = await response.json();
}
} catch (err) {
console.error('Failed to load server status:', err);
}
}
async function fetchGamemaster() { async function fetchGamemaster() {
loading.value = true; resetGamemaster();
error.value = null; await fetchGamemasterData();
rawGamemaster.value = null;
processedData.value = null; processedData.value = null;
try {
const data = await fetchLatestGamemaster();
rawGamemaster.value = data;
} catch (err) {
error.value = `Failed to fetch gamemaster: ${err.message}`;
} finally {
loading.value = false;
}
} }
function processGamemaster() { function processGamemaster() {
@@ -246,42 +274,17 @@ function processGamemaster() {
try { try {
processedData.value = breakUpGamemaster(rawGamemaster.value); processedData.value = breakUpGamemaster(rawGamemaster.value);
} catch (err) { } catch (err) {
error.value = `Failed to process gamemaster: ${err.message}`; // Error handling through computed error state
} }
} }
async function saveToServer() { async function saveToServer() {
if (!processedData.value) return; if (!processedData.value) return;
saving.value = true;
saveSuccess.value = false; saveSuccess.value = false;
error.value = null; resetSave();
await saveToServerData();
try { saveSuccess.value = !saveError.value;
// Call the server endpoint to fetch and process on the server
const response = await fetch('/api/gamemaster/process', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
const err = await response.json();
throw new Error(err.error || 'Failed to save to server');
}
const result = await response.json();
saveSuccess.value = true;
console.log('✅ Files saved to server:', result);
// Reload server status
await loadServerStatus();
} catch (err) {
error.value = `Failed to save to server: ${err.message}`;
} finally {
saving.value = false;
}
} }
function downloadPokemon() { function downloadPokemon() {
@@ -322,7 +325,7 @@ async function downloadFromServer(filename) {
document.body.removeChild(link); document.body.removeChild(link);
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
} catch (err) { } catch (err) {
error.value = `Failed to download ${filename}: ${err.message}`; // Error will be displayed through error computed property
} }
} }