This commit is contained in:
sam 2023-12-19 17:40:02 +01:00
commit 69b4c9116b
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
30 changed files with 3428 additions and 0 deletions

28
src/stores/account.ts Normal file
View file

@ -0,0 +1,28 @@
import apiFetch from "@/lib/api-fetch";
import type { Account } from "@/lib/api/entities/account";
import { defineStore } from "pinia";
import { ref } from "vue";
const localStorageKey = "vulpine-current-user";
const tokenKey = "vulpine-token";
export const useAccountStore = defineStore("current-user", () => {
const json = localStorage.getItem(localStorageKey);
const user = ref<Account>(json ? JSON.parse(json) : undefined);
const token = ref(localStorage.getItem(tokenKey) || undefined);
function setUser(meUser: Account) {
user.value = meUser;
localStorage.setItem(localStorageKey, JSON.stringify(user.value));
}
async function fetchUser() {
user.value = await apiFetch<Account>("/api/v1/accounts/verify_credentials");
localStorage.setItem(localStorageKey, JSON.stringify(user.value));
}
function setToken(newToken: string) {
localStorage.setItem(tokenKey, newToken);
token.value = newToken;
}
return { user, token, setUser, setToken, fetchUser };
});

29
src/stores/application.ts Normal file
View file

@ -0,0 +1,29 @@
import apiFetch from "@/lib/api-fetch";
import type Application from "@/lib/api/entities/application";
import { defineStore } from "pinia";
import { ref } from "vue";
const localStorageKey = "vulpine-oauth-app";
export const useAppStore = defineStore("oauth-app", () => {
const json = localStorage.getItem(localStorageKey)
const app = ref<Application>(json ? JSON.parse(json) : undefined);
async function getApp() {
if (app.value) return app.value;
const resp = await apiFetch<Application>("/api/v1/apps", {
method: "POST",
body: {
client_name: "vulpine-fe",
redirect_uris: `${window.location.origin}/auth/callback`,
scopes: "read write follow push",
}
})
app.value = resp;
localStorage.setItem(localStorageKey, JSON.stringify(resp));
return app.value;
}
return { app, getApp }
})

12
src/stores/counter.ts Normal file
View file

@ -0,0 +1,12 @@
import { ref, computed } from "vue";
import { defineStore } from "pinia";
export const useCounterStore = defineStore("counter", () => {
const count = ref(0);
const doubleCount = computed(() => count.value * 2);
function increment() {
count.value++;
}
return { count, doubleCount, increment };
});