Refactor CurrentlyBuilding to use centralized data store

Replaced local state and fetching logic with a Vuex-like data store for better state management. Updated import paths, removed redundant code, and ensured reactive updates using computed properties and watchers. This enhances maintainability and reduces code duplication.
This commit is contained in:
2025-04-14 21:42:52 +02:00
parent c864664536
commit 90be95afda
2 changed files with 53 additions and 107 deletions

View File

@@ -74,138 +74,84 @@
</v-list>
<v-sheet class="ps-4" color="transparent" rounded width="100%">
<h4 class="mb-2 font-weight-light text-grey">Queued</h4>
<QueuedPackagesList :packages="packages.queued" />
<queued-packages-list :packages="packages.queued" />
</v-sheet>
</v-card-text>
</v-card>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import type { Packages } from '@/types/Packages'
import { Package } from '@/types/Package'
import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
import { useDisplay } from 'vuetify'
import QueuedPackagesList from './QueuedPackagesList.vue'
const BASE_URL = 'https://api.alhp.dev/packages'
const UPDATE_INTERVAL_MINUTES = 5
import { useDataStore } from '@/stores/dataStore'
import QueuedPackagesList from '@/components/CurrentlyBuilding/QueuedPackagesList.vue'
const { mobile } = useDisplay()
const lastUpdatedTime = ref(0)
const dataStore = useDataStore()
const lastUpdatedSeconds = ref(0)
const updateFailed = ref(false)
const rtf = new Intl.RelativeTimeFormat('en', {
localeMatcher: 'best fit',
numeric: 'always',
style: 'long'
})
const packageCount = ref({
total: 0,
building: 0,
built: 0,
queued: 0
// Computed properties to access store data
const updateFailed = computed(() => !!dataStore.error)
const packageCount = computed(() => {
const stats = dataStore.getPackageStats() || { total: 0, building: 0, built: 0, queued: 0 }
return {
total: stats.total || 0,
building: stats.building || 0,
built: stats.built || 0,
queued: stats.queued || 0
}
})
const packages = ref<{
built: Array<Package>
building: Array<Package>
queued: Array<Package>
}>({
built: [],
building: [],
queued: []
const packages = computed(() => {
return {
building: dataStore.getPackagesByStatus('building').packages || [],
queued: dataStore.getPackagesByStatus('queued').packages || [],
built: dataStore.getPackagesByStatus('built').packages || []
}
})
let timerInterval: NodeJS.Timeout // Declare timerInterval outside the function
// Set up and clean up the timer for "last updated" display
let updateTimer: number | undefined
const fetchPackages = async (offset: string, status?: string) => {
let url = `${BASE_URL}?limit=0&offset=${offset}`
if (status) {
url += `&status=${status}`
}
try {
const response = await fetch(url, { method: 'GET' })
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const json: Packages = await response.json()
lastUpdatedTime.value = Date.now()
lastUpdatedSeconds.value = 0 // Reset seconds counter
updateFailed.value = false
return json
} catch (error) {
console.error('Error fetching packages:', error)
updateFailed.value = true
return null
}
const resetLastUpdatedCounter = () => {
lastUpdatedSeconds.value = 0
}
const getTotalPackages = async () => {
const json = await fetchPackages('1')
if (json) {
packageCount.value.total = json.total
const startLastUpdatedTimer = () => {
updateTimer = window.setInterval(() => {
lastUpdatedSeconds.value++
}, 1000)
}
watch(
() => dataStore.loading,
(isLoading) => {
if (!isLoading) {
resetLastUpdatedCounter()
console.log(updateFailed.value)
console.log(packageCount.value)
console.log(packages.value)
console.log(updateTimer)
}
}
const getBuiltPackages = async () => {
const json = await fetchPackages('0', 'built')
if (json) {
packageCount.value.built = json.total
packages.value.built = json.packages
}
}
const getBuildingPackages = async () => {
const json = await fetchPackages('0', 'building')
if (json) {
packageCount.value.building = json.total
packages.value.building = json.packages
}
}
const getQueuedPackages = async () => {
const json = await fetchPackages('0', 'queued')
if (json) {
packageCount.value.queued = json.total
packages.value.queued = json.packages
}
}
// Function to start the timer
const startTimer = () => {
stopTimer() // Clear any existing timer
getTotalPackages()
getBuiltPackages()
getBuildingPackages()
getQueuedPackages()
timerInterval = setInterval(
() => {
getTotalPackages()
getBuiltPackages()
getBuildingPackages()
getQueuedPackages()
},
UPDATE_INTERVAL_MINUTES * 60 * 1000
)
}
// Function to stop the timer
const stopTimer = () => {
clearInterval(timerInterval)
}
)
onMounted(() => {
startTimer() // Start the timer when the component is mounted
startLastUpdatedTimer()
})
// Update seconds counter every second
setInterval(() => {
lastUpdatedSeconds.value++
}, 1000)
onUnmounted(() => {
if (updateTimer) {
clearInterval(updateTimer)
}
})
</script>
<style lang="scss">