From 2803af9fa6a2d6ed3c919edca7647d84873516c1 Mon Sep 17 00:00:00 2001 From: Collin Herber Date: Sun, 24 Nov 2024 16:45:44 -0500 Subject: [PATCH] feat: adding in mob data editing and exporting --- src/lib/services/http-service.ts | 34 ++++++++++++++------ src/lib/services/mod-data-service.ts | 18 ++++++++++- src/lib/services/player-service.ts | 2 +- src/lib/services/user-service.ts | 2 +- src/routes/mod-data/mobs/+page.svelte | 46 +++++++++++++++++---------- 5 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/lib/services/http-service.ts b/src/lib/services/http-service.ts index 2c9f2ce..84f2712 100644 --- a/src/lib/services/http-service.ts +++ b/src/lib/services/http-service.ts @@ -1,10 +1,11 @@ -import axios from 'axios'; -import {GetJwtToken} from './session-service'; +import axios, {type AxiosRequestConfig, type AxiosResponse} from 'axios'; +import { GetJwtToken } from './session-service'; +import { toast } from "@zerodevx/svelte-toast"; export class HttpService { private static instance: HttpService; - constructor() { + private constructor() { axios.defaults.baseURL = import.meta.env.VITE_API_BASE_URL; axios.interceptors.request.use((config) => { if (GetJwtToken()) { @@ -12,28 +13,41 @@ export class HttpService { } return config; }, (error) => { - // Do something with request error return Promise.reject(error); }); + + axios.interceptors.response.use(function (response) { + return response; + }, function (error) { + toast.push('Failed to fetch'); + return Promise.reject(error); + }); + } + + public static getInstance(): HttpService { + if (!HttpService.instance) { + HttpService.instance = new HttpService(); + } + return HttpService.instance; } - public async get(url: string): Promise { + public async get(url: string): Promise { return await axios.get(url); } - public async post(url: string, data: any): Promise { - return await axios.post(url, data); + public async post(url: string, data: any, options?: AxiosRequestConfig): Promise { + return await axios.post(url, data, options); } - public async put(url: string, data: any): Promise { + public async put(url: string, data: any): Promise { return await axios.put(url, data); } - public async delete(url: string): Promise { + public async delete(url: string): Promise { return await axios.delete(url); } - public async patch(url: string, data: any): Promise { + public async patch(url: string, data: any): Promise { return await axios.patch(url, data); } } \ No newline at end of file diff --git a/src/lib/services/mod-data-service.ts b/src/lib/services/mod-data-service.ts index b9a6670..d016266 100644 --- a/src/lib/services/mod-data-service.ts +++ b/src/lib/services/mod-data-service.ts @@ -2,7 +2,7 @@ import { HttpService } from "$lib/services/http-service"; import type {IMobData} from "$lib/models/mob-data"; export class ModDataService { - httpService = new HttpService(); + httpService = HttpService.getInstance(); public async getMobData(): Promise { let response = await this.httpService.get('ModData/Mobs'); @@ -11,4 +11,20 @@ export class ModDataService { } return []; } + + public async exportMobData(data: IMobData[]): Promise { + const response = await this.httpService.post('ModData/ExportMobs', data, { + responseType: 'blob', + }); + const url = window.URL.createObjectURL(response.data); + + const a = document.createElement('a'); + a.href = url; + a.download = 'mob_data.zip'; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + + window.URL.revokeObjectURL(url); + } } diff --git a/src/lib/services/player-service.ts b/src/lib/services/player-service.ts index fd3a884..1a46c44 100644 --- a/src/lib/services/player-service.ts +++ b/src/lib/services/player-service.ts @@ -12,7 +12,7 @@ export interface IPlayerStats { } export class PlayerService { - httpService = new HttpService(); + httpService = HttpService.getInstance(); public async getTop50Players(): Promise { let response = await this.httpService.get('Player/Leaderboard?count=50&skip=0'); diff --git a/src/lib/services/user-service.ts b/src/lib/services/user-service.ts index f5bcfa4..b59d889 100644 --- a/src/lib/services/user-service.ts +++ b/src/lib/services/user-service.ts @@ -3,7 +3,7 @@ import {GetJwtToken, SetJwtToken} from "$lib/services/session-service"; import {type IUser, user} from "$lib/stores/user-store"; export class UserService { - httpService = new HttpService(); + httpService = HttpService.getInstance(); user: IUser | null = null; public async getUserProfile() { diff --git a/src/routes/mod-data/mobs/+page.svelte b/src/routes/mod-data/mobs/+page.svelte index 20079ed..0e36f97 100644 --- a/src/routes/mod-data/mobs/+page.svelte +++ b/src/routes/mod-data/mobs/+page.svelte @@ -2,6 +2,7 @@ import { onMount } from 'svelte'; import { ModDataService } from '$lib/services/mod-data-service'; import EditModal from '$lib/components/EditMobDataModal.svelte'; + import { Button } from 'flowbite-svelte'; let modDataService = new ModDataService(); import type { IMobData } from '$lib/models/mob-data'; @@ -49,27 +50,40 @@ filteredMobData = [...mobData]; closeEditModal(); } + + async function exportMobData() { + try { + await modDataService.exportMobData(mobData); + } catch (err) { + error = 'Failed to export mob data'; + } + }

Mob Data

- -
- - + +
+
+ + +
+