/
index.html
182 lines (172 loc) · 9.05 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<html class="h-full bg-white">
<head>
<title>Admin Portal</title>
<link rel="icon" type="image/svg" href="/img/logo.svg">
<link rel="stylesheet" href="/css/app.css">
<script async src="https://ga.jspm.io/npm:es-module-shims@1.6.3/dist/es-module-shims.js"></script><!--safari polyfill-->
<script type="importmap">
{
"imports": {
"vue": "/lib/mjs/vue.mjs",
"@servicestack/client": "/lib/mjs/servicestack-client.mjs",
"@servicestack/vue": "/lib/mjs/servicestack-vue.mjs",
"dtos.mjs": "/mjs/dtos.mjs"
}
}
</script>
</head>
<body class="h-full">
<div id="app" v-cloak>
<sidebar-layout ref="sidebar">
<div class="flex h-16 shrink-0 items-center">
<a href="/portal/" class="text-2xl whitespace-nowrap overflow-x-hidden flex items-center">
<img class="mr-1 h-8 w-auto text-indigo-600" src="/img/logo.svg" alt="Logo">
<span class="hidden sm:block text-2xl font-semibold">Client Admin</span>
</a>
</div>
<nav class="flex flex-1 flex-col">
<ul role="list" class="flex flex-1 flex-col gap-y-7">
<li v-for="group in new Set(Object.keys(sections).flatMap(k => sections[k].group))">
<div v-if="group" class="text-sm font-semibold leading-6 text-gray-400">{{group}}</div>
<ul role="list" class="-mx-2 space-y-1">
<li v-for="section in Object.keys(sections).map(k => sections[k].group === group ? sections[k] : null).filter(x => !!x)">
<a @click.prevent="navTo(section.id)" :class="[activeSection.id === section.id ? 'bg-gray-50 text-indigo-600' : 'cursor-pointer text-gray-700 hover:text-indigo-600 hover:bg-gray-50', 'group flex gap-x-3 rounded-md p-2 text-sm leading-6 font-semibold select-none']" @click="$forceUpdate()">
<icon :svg="section.icon" class="h-6 w-6 shrink-0 text-indigo-600"></icon>
{{section.label}}
</a>
</li>
</ul>
</li>
<li class="-mx-6 mt-auto">
<div v-if="showUserMenu" class="font-normal absolute z-10 -mt-10 left-6 w-60 origin-top-right rounded-md bg-white dark:bg-black py-1 shadow-lg ring-1 ring-black dark:ring-gray-600 ring-opacity-5 focus:outline-none right-2" role="menu" aria-orientation="vertical" aria-labelledby="user-menu-button" tabindex="-1">
<a href="/auth/logout" class="block px-4 py-2 text-sm text-gray-700 dark:text-gray-200 hover:bg-gray-100 dark:hover:bg-gray-800" role="menuitem" tabindex="-1">Logout</a>
</div>
<span v-if="user" @click="showUserMenu=!showUserMenu" class="flex cursor-pointer bg-gray-50 items-center gap-x-4 px-6 py-3 text-sm font-semibold leading-6 text-gray-900 hover:bg-gray-50">
<img class="h-8 w-8 rounded-full bg-gray-50" :src="user.profileUrl" alt="">
<span class="sr-only">Your profile</span>
<span aria-hidden="true">{{user.displayName}}</span>
</span>
</li>
</ul>
</nav>
<template #mobiletitlebar>
<div class="flex-1 text-sm font-semibold leading-6 text-gray-900">{{ activeSection.title }}</div>
<span v-if="user" class="cursor-pointer bg-gray-50">
<span class="sr-only">Your profile</span>
<img class="h-8 w-8 rounded-full bg-gray-50" :src="user.profileUrl" alt="">
</span>
</template>
</sidebar-layout>
<main class="lg:pl-72">
<div v-if="init" class="px-4 sm:px-6 lg:px-8">
<sign-in v-if="!user"></sign-in>
<div v-else>
<h1 class="hidden lg:block pt-4 mb-2 text-3xl font-bold leading-tight tracking-tight text-gray-900">{{ activeSection.title }}</h1>
<component :key="refreshKey" :is="activeSection.component" @nav="navTo" :type="activeSection.type"></component>
</div>
</div>
</main>
</div>
<script type="module">
import { createApp, ref, onMounted, shallowRef } from "vue"
import { JsonApiClient, leftPart, rightPart, appendQueryString, humanize } from "@servicestack/client"
import ServiceStackVue, { useClient, useAuth, useMetadata } from "@servicestack/vue"
import { Authenticate, AdminData } from "dtos.mjs"
const { loadMetadata, metadataApi, typeOf } = useMetadata()
await loadMetadata({
olderThan: window.Server ? null : location.search.includes('clear=metadata') ? 0 : 60 * 60 * 1000, //1hr
})
const sections = {
Dashboard: {
icon: `<svg fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25" /></svg>`,
component: {
template:`
<div>
<dl class="mt-5 grid grid-cols-1 gap-5 sm:grid-cols-3">
<div v-for="stat in stats" @click="$emit('nav',stat.label)" class="cursor-pointer hover:bg-gray-50 overflow-hidden rounded-lg bg-white px-4 py-5 shadow sm:p-6">
<dt class="truncate text-sm font-medium text-gray-500">Total {{humanize(stat.label)}}</dt>
<dd class="mt-1 text-3xl font-semibold tracking-tight text-gray-900">{{formatNumber(stat.total)}}</dd>
</div>
</dl>
<p class="mt-8 text-sm font-semibold leading-6">
Go to <a href="/admin/" class="text-indigo-600">Server Rendered Admin <span aria-hidden="true">→</span></a>
</p>
</div>`,
setup() {
const client = useClient()
const stats = ref([])
client.swr(new AdminData(), r => stats.value = r.response?.pageStats || [])
const formatNumber = value => new Intl.NumberFormat().format(value)
return { stats, humanize, formatNumber }
}
},
},
Bookings: {
type: 'Booking',
component: {
template:`<AutoQueryGrid :type="type"
selected-columns="id,name,roomType,roomNumber,bookingStartDate,bookingEndDate,cost,couponId,discount"
:header-titles="{ roomNumber:'Room No', bookingStartDate:'Start Date', bookingEndDate:'End Date', couponId:'Coupon' }"
:visible-from="{ roomNumber:'lg', bookingEndDate:'xl', cost:'md', couponId:'xl', discount:'never' }" />`,
},
},
Coupons: {
type: 'Coupon',
component: {
template:`<AutoQueryGrid :type="type" />`,
},
},
}
function getIcon(id) {
const section = sections[id]
return section.icon || typeOf(section.type)?.icon?.svg ||
`<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8V6a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2v2M3 8v6m0-6h6m12 0v6m0-6H9m12 6v4a2 2 0 0 1-2 2H9m12-6H9m-6 0v4a2 2 0 0 0 2 2h4m-6-6h6m0-6v6m0 0v6m6-12v12"></path></svg>`
}
Object.keys(sections).map(id => {
const section = sections[id]
section.id = id
section.label = section.label || humanize(id)
section.title = section.title || section.label
section.icon = getIcon(id)
section.component.props = section.component.props || ['type']
section.component.emits = section.component.emits || ['nav']
})
const App = {
setup() {
const { user, signIn } = useAuth()
const client = useClient()
const sidebar = ref()
const refreshKey = ref(1)
const showUserMenu = ref(false)
const init = ref(false)
const getSection = () => rightPart(leftPart(location.hash,'?'), '#')
const activeSection = shallowRef(sections[getSection()] || sections.Dashboard)
function navTo(section, args, pushState=true) {
if (!args) args = {}
refreshKey.value++
activeSection.value = sections[section] || sections.Dashboard
if (pushState) history.pushState({ section, ...args }, null, appendQueryString(`#${section}`, args))
sidebar.value.toggle(false)
}
onMounted(async () => {
window.addEventListener('popstate', event => {
navTo(event.state?.section || getSection(), {}, false)
})
const api = await client.api(new Authenticate())
if (api.succeeded) {
signIn(api.response)
}
init.value = true
})
return { init, sidebar, user, sections, activeSection, navTo, refreshKey, showUserMenu }
}
}
const client = JsonApiClient.create()
const app = createApp(App)
app.use(ServiceStackVue)
app.component('RouterLink', ServiceStackVue.component('RouterLink'))
app.provide('client', client)
app.mount('#app')
</script>
</body>
</html>