/** * Import types from Nadal. */ import type { MaintenanceResponseData } from '@kira-dancer/nadal' import { DATE_TIME_FORMAT } from '#lib/constants' import { formatDate } from '#lib/utils' import { useMaintenanceGamesStore } from '#lib/stores' import { ref } from 'vue' import { useNadal } from '#lib/composables' /** * Provides functions and state for handling maintenance information for games. * * @returns {Object} An object with state and functions to manage maintenance information. * @namespace */ export function useMaintain() { const maintainStore = useMaintenanceGamesStore() const { site } = useNadal() const isLoading = ref(false) /** * Fetches maintenance information for games and updates the store. * * MaintenanceResponseData type is imported from the Nadal module. * * This function retrieves maintenance data from the API, formats the start and end times, * and updates the maintenance state in the store. It also handles the loading state. * * @returns {Promise} A promise that resolves when the maintenance data is successfully fetched and the store is updated. */ const getMaintenanceGames = async () => { try { isLoading.value = true const data = await site.maintenance() if (data?.length) { const startTime = formatDate( data[0].start_time, DATE_TIME_FORMAT.DATE_FORMAT_MAINTAIN, ) const endTime = formatDate( data[0].end_time, DATE_TIME_FORMAT.DATE_FORMAT_MAINTAIN, ) maintainStore.setMaintenanceGames(data[0].enable, startTime, endTime) } } catch (ex) { console.error(' ~ getMaintenanceGames ~ ex:', ex) } finally { isLoading.value = false } } return { isLoading, getMaintenanceGames, } }