Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de7286c8d4 | ||
|
|
1c3bf5add6 |
+3
-3
@@ -9,11 +9,11 @@
|
|||||||
"astro": "astro"
|
"astro": "astro"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@astrojs/vue": "^7.0.2",
|
"@astrojs/vue": "^5.1.3",
|
||||||
"@nanostores/vue": "^1.0.1",
|
"@nanostores/vue": "^1.0.1",
|
||||||
"astro": "^7.2.10",
|
"astro": "^5.16.6",
|
||||||
"nanostores": "^1.1.0",
|
"nanostores": "^1.1.0",
|
||||||
"vue": "^3.5.42"
|
"vue": "^3.5.26"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nanostores/logger": "^1.0.0"
|
"@nanostores/logger": "^1.0.0"
|
||||||
|
|||||||
Generated
+2253
-1845
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import {ref} from "vue";
|
import {ref} from "vue";
|
||||||
import {saveToken} from "./scripts/load_token.js";
|
import {setLoggedInUser} from "./stores/user.js";
|
||||||
import * as gateway from "./lib/gateway.js";
|
import * as gateway from "./lib/gateway.js";
|
||||||
|
|
||||||
const formData = ref({username: '', password: ''})
|
const formData = ref({username: '', password: ''})
|
||||||
@@ -17,7 +17,7 @@ async function formSubmit() {
|
|||||||
if(response.status === 200) {
|
if(response.status === 200) {
|
||||||
const responseData = await response.json()
|
const responseData = await response.json()
|
||||||
console.log(responseData)
|
console.log(responseData)
|
||||||
saveToken(responseData)
|
setLoggedInUser(responseData.token, responseData.expiresAt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import UserAction from './top_nav/UserAction.vue'
|
|||||||
<li><Link href="/auctions">Auctions</Link></li>
|
<li><Link href="/auctions">Auctions</Link></li>
|
||||||
</ul>
|
</ul>
|
||||||
<ul class="right">
|
<ul class="right">
|
||||||
<li><UserAction client:only /></li>
|
<li><UserAction client:load /></li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
|
|||||||
@@ -1,43 +1,31 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import {ref, computed, onMounted} from "vue";
|
import {ref, watch} from 'vue'
|
||||||
import {request} from "../lib/gateway.js";
|
import {useStore} from '@nanostores/vue'
|
||||||
import {loadToken} from "../scripts/load_token.js";
|
import {$user} from '../stores/user'
|
||||||
|
import * as gateway from '../lib/gateway.js'
|
||||||
|
|
||||||
const pending = Symbol('pending')
|
const user = useStore($user)
|
||||||
|
const auctions = ref([])
|
||||||
|
|
||||||
const qref = ref(pending)
|
watch(user, async (userVal) => {
|
||||||
const isPending = computed(() => qref.value === pending)
|
if(userVal == null) return;
|
||||||
const data = computed(() => qref.value.data)
|
|
||||||
|
|
||||||
async function loadAuctions() { // TODO: add params
|
const response = await gateway.request('/auctions',
|
||||||
let token = loadToken()
|
{cors: true, token: userVal.token, searchParams: {page: 1, per: 50}}
|
||||||
if(!token.token) return;
|
)
|
||||||
|
|
||||||
try {
|
if (response.ok) {
|
||||||
let response = await request("/auctions", {cors: true, token: token.token})
|
auctions.value = await response.json()
|
||||||
qref.value = {error: null, headers: response.headers, data: await response.json()}
|
|
||||||
} catch (error) {
|
|
||||||
qref.value = {error}
|
|
||||||
}
|
}
|
||||||
}
|
}, {immediate: true})
|
||||||
|
|
||||||
onMounted(async () => {
|
|
||||||
await loadAuctions()
|
|
||||||
})
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="placeholder" v-if="isPending"></div>
|
<ul>
|
||||||
<ul v-else-if="!isPending && data != null && data.length > 0">
|
<li v-for="auction in auctions" :key="auction.id">
|
||||||
<li v-for="auction in data" :key="auction.id">
|
<h3>{{ auction.summary }}</h3>
|
||||||
<span>{{ auction.name }}</span>
|
<a :href="`/auctions/auction?id=${auction.id}`">See more</a>
|
||||||
<span v-if="auction.current_bid">{{ auction.current_bid }}</span>
|
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p v-else>No auctions available</p>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
|
||||||
|
|
||||||
</style>
|
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<script setup>
|
||||||
|
import {useStore} from "@nanostores/vue";
|
||||||
|
import {$user} from "../stores/user.js";
|
||||||
|
|
||||||
|
const id = new URL(location).searchParams.get("id")
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<template v-if="id">
|
||||||
|
<span>{{ id }}</span>
|
||||||
|
</template>
|
||||||
|
<div v-else>ERROR</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
|
||||||
|
</style>
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import {loadToken, Anonymous} from "../scripts/load_token.js";
|
|
||||||
|
|
||||||
if(loadToken() === Anonymous) { location.replace("/login") }
|
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
import { ref, computed } from 'vue'
|
|
||||||
|
|
||||||
export async function request(path, options) {
|
export async function request(path, options) {
|
||||||
options ||= {}
|
options ||= {}
|
||||||
if(path.startsWith('/')) {
|
if(path.startsWith('/')) {
|
||||||
@@ -11,7 +9,7 @@ export async function request(path, options) {
|
|||||||
searchParams.forEach((v, k) => url.searchParams.append(k, v))
|
searchParams.forEach((v, k) => url.searchParams.append(k, v))
|
||||||
|
|
||||||
const safeFetch = (options.method === 'GET') || (options.method === 'HEAD')
|
const safeFetch = (options.method === 'GET') || (options.method === 'HEAD')
|
||||||
const authorization = (options.token) ? `Bearer ${options.token}` : ''
|
const authorization = (options.token) ? `Bearer ${options.token}` : null
|
||||||
|
|
||||||
const fetchInit = {
|
const fetchInit = {
|
||||||
method: options.method || 'GET',
|
method: options.method || 'GET',
|
||||||
@@ -21,26 +19,9 @@ export async function request(path, options) {
|
|||||||
headers: {
|
headers: {
|
||||||
'Content-Type': options.contentType ||
|
'Content-Type': options.contentType ||
|
||||||
(safeFetch ? null : 'application/json'),
|
(safeFetch ? null : 'application/json'),
|
||||||
'X-Requested-With': 'fetch',
|
|
||||||
'Authorization': authorization
|
'Authorization': authorization
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add processing for 4xx and 5xx
|
|
||||||
return await fetch(url, fetchInit)
|
return await fetch(url, fetchInit)
|
||||||
}
|
}
|
||||||
|
|
||||||
const Pending = Symbol('pending')
|
|
||||||
|
|
||||||
export function useRequest(path, options) {
|
|
||||||
let responseRef = ref(Pending)
|
|
||||||
request(path, options).then(e => responseRef.value = {error: null, headers: e.headers, data: e.json()})
|
|
||||||
.catch(reason => responseRef.value = {error: reason})
|
|
||||||
|
|
||||||
return {
|
|
||||||
isPending: computed(() => responseRef.value === Pending),
|
|
||||||
data: computed(() => responseRef.value.data),
|
|
||||||
error: computed(() => responseRef.value.error),
|
|
||||||
rref: responseRef
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
const LOCAL_STORAGE_KEY="token"
|
|
||||||
|
|
||||||
export const Anonymous = Symbol("anonymous")
|
|
||||||
class Token {
|
|
||||||
constructor({token, expiresAt}) {
|
|
||||||
this.token = token
|
|
||||||
this.expiresAt = new Date(expiresAt)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function loadToken() {
|
|
||||||
let data = localStorage[LOCAL_STORAGE_KEY]
|
|
||||||
if (data == null) { return Anonymous }
|
|
||||||
data = JSON.parse(data)
|
|
||||||
if (data == null || data.token == null) { return Anonymous }
|
|
||||||
|
|
||||||
let token = new Token(data)
|
|
||||||
if(token.expiresAt < new Date()) { return Anonymous }
|
|
||||||
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
export function saveToken(value) {
|
|
||||||
if(value == null || !value.token) return;
|
|
||||||
localStorage[LOCAL_STORAGE_KEY] = JSON.stringify(value)
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import {$user, setAnonUser, setLoggedInUser} from "../stores/user.js";
|
||||||
|
|
||||||
|
const LOCAL_STORAGE_KEY="user"
|
||||||
|
|
||||||
|
$user.subscribe(value => {
|
||||||
|
if(value == null || !value.initialized) return;
|
||||||
|
localStorage[LOCAL_STORAGE_KEY] = JSON.stringify(value)
|
||||||
|
})
|
||||||
|
|
||||||
|
window.addEventListener('load', () => {
|
||||||
|
let data = localStorage[LOCAL_STORAGE_KEY]
|
||||||
|
if (data == null) { return setAnonUser() }
|
||||||
|
data = JSON.parse(data)
|
||||||
|
if (data == null || data.token == null) { return setAnonUser() }
|
||||||
|
if(Date.parse(data.expiresAt) < Date()) { return setAnonUser() }
|
||||||
|
|
||||||
|
setLoggedInUser(data.token, data.expiresAt)
|
||||||
|
})
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { atom } from 'nanostores'
|
||||||
|
|
||||||
|
export const $user = atom({})
|
||||||
|
|
||||||
|
export function setAnonUser() {
|
||||||
|
$user.set({initialized: true})
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setLoggedInUser(token, expiresAt) {
|
||||||
|
$user.set({initialized: true, loggedIn: true, token: token, expiresAt: expiresAt})
|
||||||
|
}
|
||||||
@@ -1,19 +1,14 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import {ref, onMounted} from "vue";
|
import {useStore} from '@nanostores/vue'
|
||||||
import {loadToken, Anonymous} from "../scripts/load_token.js";
|
import {$user} from "../stores/user.js";
|
||||||
|
|
||||||
const Loading = Symbol("loading")
|
const user = useStore($user)
|
||||||
const token = ref(Loading)
|
|
||||||
|
|
||||||
onMounted(() => {
|
|
||||||
token.value = loadToken()
|
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div v-if="token === Loading" class="placeholder"></div> <!-- TODO: replace with an actual placeholder -->
|
<div v-if="!user.initialized" class="placeholder"></div> <!-- TODO: replace with an actual placeholder -->
|
||||||
<a href="/login" v-else-if="token === Anonymous">Login</a>
|
<a href="/login" v-else-if="!user.loggedIn">Login</a>
|
||||||
<span v-else>{{ token.username || "Username" }}</span>
|
<span v-else>{{ user.username || "Username" }}</span>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ const {title} = Astro.props
|
|||||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg"/>
|
<link rel="icon" type="image/svg+xml" href="/favicon.svg"/>
|
||||||
<meta name="generator" content={Astro.generator}/>
|
<meta name="generator" content={Astro.generator}/>
|
||||||
<title>Neon Vertigo</title>
|
<title>Neon Vertigo</title>
|
||||||
<script src="../components/scripts/load_token.js"></script>
|
<script src="../components/scripts/user_store.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header>
|
<header>
|
||||||
@@ -25,8 +25,9 @@ const {title} = Astro.props
|
|||||||
{import.meta.env.DEV &&
|
{import.meta.env.DEV &&
|
||||||
<script>
|
<script>
|
||||||
import {logger} from '@nanostores/logger'
|
import {logger} from '@nanostores/logger'
|
||||||
|
import {$user} from "../components/stores/user";
|
||||||
|
|
||||||
let _destroy = logger({})
|
let _destroy = logger({user: $user})
|
||||||
</script>
|
</script>
|
||||||
}
|
}
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
---
|
|
||||||
import Layout from '../layouts/Layout.astro';
|
|
||||||
|
|
||||||
import Auctions from '../components/auctions/Index.vue'
|
|
||||||
---
|
|
||||||
|
|
||||||
<Layout title="Auctions">
|
|
||||||
<Auctions client:load />
|
|
||||||
|
|
||||||
<script src="../components/lib/check_user.js"></script>
|
|
||||||
</Layout>
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
import Layout from "../../layouts/Layout.astro";
|
||||||
|
import AuctionShow from "../../components/auctions/Show.vue"
|
||||||
|
---
|
||||||
|
|
||||||
|
<Layout title="See auction">
|
||||||
|
<AuctionShow client:only />
|
||||||
|
</Layout>
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
import Layout from "../../layouts/Layout.astro";
|
||||||
|
import AuctionsIndex from '../../components/auctions/Index.vue'
|
||||||
|
---
|
||||||
|
|
||||||
|
<Layout title="Auctions">
|
||||||
|
<AuctionsIndex client:load />
|
||||||
|
</Layout>
|
||||||
Reference in New Issue
Block a user