- {{ greeting.header[0] }}
- {{ mainStore.userName }} {{ greeting.header[1] }}
+ {{ greeting.header[0] }}{{ mainStore.userName }} {{ greeting.header[1] }}
{{ mainStore.userName }}
diff --git a/src/stores/api/account.js b/src/stores/api/account.js
index 6b1e826..134b484 100644
--- a/src/stores/api/account.js
+++ b/src/stores/api/account.js
@@ -56,3 +56,37 @@ export async function APIUpdatePassword(
throw error;
}
}
+
+export async function APIGetCards() {
+ try {
+ const data = await mainStore.callApi(`/user/card`);
+ return data.cards;
+ } catch (error) {
+ console.log("Error fetching cards:", error);
+ throw error;
+ }
+}
+
+export async function APIPutCard(cardId) {
+ try {
+ const data = await mainStore.callApi(`/user/card`, "POST", {
+ cardId: cardId,
+ });
+ return data;
+ } catch (error) {
+ console.log("Error adding card:", error);
+ throw error;
+ }
+}
+
+export async function APIDeleteCard(cardId) {
+ try {
+ const data = await mainStore.callApi(`/user/card`, "DELETE", {
+ cardId: cardId,
+ });
+ return data;
+ } catch (error) {
+ console.log("Error deleting card:", error);
+ throw error;
+ }
+}
diff --git a/src/stores/main.js b/src/stores/main.js
index b3ee699..897c10a 100644
--- a/src/stores/main.js
+++ b/src/stores/main.js
@@ -75,6 +75,15 @@ export const useMainStore = defineStore("main", {
},
async callApi(endpoint, method = "GET", data = null, extraHeaders = {}) {
+ if (method === "DELETE") {
+ const confirmed = window.confirm("Are you really?");
+ if (!confirmed) {
+ this.isSaving = false;
+ this.isLoading = false;
+ return null; // Exit if user does not confirm
+ }
+ }
+
const apiServer = import.meta.env.VITE_API_URL;
const apiKey = import.meta.env.VITE_API_KEY;
let loadingTimeout;
@@ -263,16 +272,6 @@ export const useMainStore = defineStore("main", {
}
},
- async getCards() {
- try {
- const data = await this.callApi(`/user/cards`);
- return data.cards;
- } catch (error) {
- console.log("Error fetching cards:", error);
- throw error;
- }
- },
-
async getArcade(arcadeId) {
try {
const data = await this.callApi(`/arcade/${arcadeId}`);
diff --git a/src/views/Profile/CardsView.vue b/src/views/Profile/CardsView.vue
index 74fbaa7..e1dd20b 100644
--- a/src/views/Profile/CardsView.vue
+++ b/src/views/Profile/CardsView.vue
@@ -15,23 +15,58 @@ import FormControl from "@/components/FormControl.vue";
import LayoutAuthenticated from "@/layouts/LayoutAuthenticated.vue";
import SectionTitleLine from "@/components/SectionTitleLine.vue";
-import { useMainStore } from "@/stores/main";
-const mainStore = useMainStore();
+import { APIGetCards, APIPutCard, APIDeleteCard } from "@/stores/api/account";
var cardData = ref([]);
+const cardLoading = ref(false);
const cardForm = reactive({
newCard: null,
});
-onMounted(async () => {
+async function loadCards() {
+ cardData.value = null;
try {
- const data = await mainStore.getCards();
+ const data = await APIGetCards();
cardData.value = data;
} catch (error) {
console.error("Failed to fetch card data:", error);
}
+}
+
+onMounted(async () => {
+ await loadCards();
});
+function cardInput(event) {
+ var input = event.target.value;
+ input = input.replace(/[^A-Za-z0-9]/g, "");
+
+ // Format the input to XXXX-XXXX-XXXX-XXXX
+ input = input
+ .toUpperCase()
+ .replace(/(.{4})/g, "$1-")
+ .replace(/-$/, "");
+
+ cardForm.newCard = input;
+}
+
+async function submitCard() {
+ cardLoading.value = true;
+ const response = await APIPutCard(cardForm.newCard);
+ if (response.status == "success") {
+ cardForm.newCard = null;
+ cardLoading.value = false;
+ await loadCards();
+ }
+}
+
+async function deleteCard(cardId) {
+ const response = await APIDeleteCard(dashCode(cardId));
+ if (response.status == "success") {
+ await loadCards();
+ }
+}
+
const copyToClipboard = (text) => {
navigator.clipboard
.writeText(text)
@@ -54,7 +89,7 @@ const copyToClipboard = (text) => {
title="Register Card"
main
/>
-
+
{
name="cardId"
type="card"
placeholder="XXXX-XXXX-XXXX-XXXX"
- minlength="16"
+ minlength="19"
maxlength="19"
required
+ @input="cardInput"
/>
{
{
label="Copy NFC-ID"
@click="copyToClipboard(card.id)"
/>
-
+