Add current state of the Vue frontend

This commit is contained in:
Tim Weber 2019-04-03 17:54:05 +02:00
parent 31aafd3b86
commit 8799cc86e9
17 changed files with 11630 additions and 0 deletions

21
vue/.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
.DS_Store
node_modules
/dist
# local env files
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw*

29
vue/README.md Normal file
View File

@ -0,0 +1,29 @@
# hsadmin-vue
## Project setup
```
npm install
```
### Compiles and hot-reloads for development
```
npm run serve
```
### Compiles and minifies for production
```
npm run build
```
### Run your tests
```
npm run test
```
### Lints and fixes files
```
npm run lint
```
### Customize configuration
See [Configuration Reference](https://cli.vuejs.org/config/).

11184
vue/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

49
vue/package.json Normal file
View File

@ -0,0 +1,49 @@
{
"name": "hsadmin-vue",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"axios": "^0.18.0",
"core-js": "^2.6.5",
"vue": "^2.6.6",
"vue-router": "^3.0.1"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^3.5.0",
"@vue/cli-plugin-eslint": "^3.5.0",
"@vue/cli-service": "^3.5.0",
"babel-eslint": "^10.0.1",
"eslint": "^5.8.0",
"eslint-plugin-vue": "^5.0.0",
"vue-template-compiler": "^2.5.21"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint"
}
},
"postcss": {
"plugins": {
"autoprefixer": {}
}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}

BIN
vue/public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

17
vue/public/index.html Normal file
View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>hsadmin-vue</title>
</head>
<body>
<noscript>
<strong>We're sorry but hsadmin-vue doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>

32
vue/src/App.vue Normal file
View File

@ -0,0 +1,32 @@
<template>
<div id="app">
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link :to="{name:'login'}">Login</router-link> |
<router-link :to="{name:'customers'}">Customers</router-link>
</div>
<router-view/>
</div>
</template>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
#nav {
padding: 30px;
}
#nav a {
font-weight: bold;
color: #2c3e50;
}
#nav a.router-link-exact-active {
color: #42b983;
}
</style>

BIN
vue/src/assets/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

View File

@ -0,0 +1,25 @@
<template>
<!-- TODO: the v-fors need IDs -->
<table>
<tr>
<th v-for="column in columns">{{ column }}</th>
</tr>
<tr v-for="entity in entities">
<td v-for="column in columns">
<router-link :to="{name:'customer', params:{id:entity[column]}}" v-if="column === linkColumn">{{ entity[column] }}</router-link>
<template v-else>{{ entity[column] }}</template>
</td>
</tr>
</table>
</template>
<script>
export default {
name: "EntityList",
props: {
columns: Array,
entities: Array,
linkColumn: String,
},
}
</script>

View File

@ -0,0 +1,58 @@
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<p>
For a guide and recipes on how to configure / customize this project,<br>
check out the
<a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>.
</p>
<h3>Installed CLI Plugins</h3>
<ul>
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li>
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank" rel="noopener">eslint</a></li>
</ul>
<h3>Essential Links</h3>
<ul>
<li><a href="https://vuejs.org" target="_blank" rel="noopener">Core Docs</a></li>
<li><a href="https://forum.vuejs.org" target="_blank" rel="noopener">Forum</a></li>
<li><a href="https://chat.vuejs.org" target="_blank" rel="noopener">Community Chat</a></li>
<li><a href="https://twitter.com/vuejs" target="_blank" rel="noopener">Twitter</a></li>
<li><a href="https://news.vuejs.org" target="_blank" rel="noopener">News</a></li>
</ul>
<h3>Ecosystem</h3>
<ul>
<li><a href="https://router.vuejs.org" target="_blank" rel="noopener">vue-router</a></li>
<li><a href="https://vuex.vuejs.org" target="_blank" rel="noopener">vuex</a></li>
<li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank" rel="noopener">vue-devtools</a></li>
<li><a href="https://vue-loader.vuejs.org" target="_blank" rel="noopener">vue-loader</a></li>
<li><a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">awesome-vue</a></li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
msg: String
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>

28
vue/src/hsadmin.js Normal file
View File

@ -0,0 +1,28 @@
import axios from "axios"
export default class HSAdmin {
constructor(options = {}) {
this._ax = axios.create({
baseURL: options.baseURL || "http://localhost:45678/api", // TODO: change to production URL
});
}
get token() { return this._token; }
async get(endpoint, config = {}) {
return this._ax.get(endpoint, config);
}
async post(endpoint, data) {
return this._ax.post(endpoint, data);
}
async authenticate(user = "admin", pass = "admin") {
const res = await this.post("authenticate", { username: user, password: pass});
if (!res.data.id_token) {
throw new Error("authentication response is missing id_token value");
}
this._token = res.data.id_token;
this._ax.defaults.headers.common["Authorization"] = "Bearer " + this._token;
}
}

43
vue/src/router.js Normal file
View File

@ -0,0 +1,43 @@
import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import HSAdmin from "./hsadmin.js"
Vue.use(Router);
const hsa = new HSAdmin();
const routeProps = (route) => {
return Object.assign({}, route.params,{
hsadmin: hsa
});
};
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path: "/login",
name: "login",
component: () => import(/* webpackChunkName: "login" */ "./views/Login.vue"),
props: routeProps,
},
{
path: "/customers",
name: "customers",
component: () => import(/* webpackChunkName: "customers" */ "./views/Customers.vue"),
props: routeProps,
},
{
path: "/customers/:id",
name: "customer",
component: () => import(/* webpackChunkName: "customers" */ "./views/Customer.vue"),
props: routeProps,
},
]
})

5
vue/src/views/About.vue Normal file
View File

@ -0,0 +1,5 @@
<template>
<div class="about">
<h1>This is an about page</h1>
</div>
</template>

View File

@ -0,0 +1,55 @@
<template>
<div>
<div v-if="error">{{ error }}</div>
This is customer {{ number }} with prefix {{ prefix }}.
<h3>Contacts</h3>
<EntityList :columns="['role','contactId','id','firstName','lastName']" :entities="contacts" />
</div>
</template>
<script>
import HSAdmin from "../hsadmin";
import EntityList from "../components/EntityList";
export default {
name: "Customer",
components: {EntityList},
data () { return {
error: false,
number: undefined,
prefix: undefined,
contacts: [],
}},
props: { hsadmin: HSAdmin, id: Number },
created () { this.fetch() },
watch: { "$route": "fetch" },
methods: {
async fetch () {
this.fetchCustomer();
this.fetchContacts();
},
async fetchCustomer () {
const res = await this.hsadmin.get(`customers/${this.id}`);
this.number = res.data.number;
this.prefix = res.data.prefix;
},
async fetchContacts () {
const relationsResponse = await this.hsadmin.get('customer-contacts', { params: {
"customerId.equals": this.id,
}});
const relations = relationsResponse.data;
// TODO: better param serializing
const contactIdQuery = relations.map(relation => `id.in=${relation.contactId}`).join("&");
const contactsResponse = await this.hsadmin.get(`contacts?${contactIdQuery}`);
const contactsById = {};
for (const contact of contactsResponse.data) {
contactsById[contact.id] = contact;
}
for (const relation of relations) {
Object.assign(relation, contactsById[relation.contactId]);
}
this.contacts = relations;
},
},
}
</script>

View File

@ -0,0 +1,29 @@
<template>
<div>
<div v-if="error">{{ error }}</div>
<EntityList :columns="['id', 'number', 'prefix']" link-column="id" :entities="customers" />
</div>
</template>
<script>
import HSAdmin from "../hsadmin";
import EntityList from "../components/EntityList";
export default {
name: "Customers",
components: {EntityList},
data () { return {
error: false,
customers: [],
}},
props: { hsadmin: HSAdmin },
created () { this.fetch() },
watch: { "$route": "fetch" },
methods: {
async fetch () {
const res = await this.hsadmin.get("customers");
this.customers = res.data;
}
},
}
</script>

18
vue/src/views/Home.vue Normal file
View File

@ -0,0 +1,18 @@
<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
</div>
</template>
<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
export default {
name: 'home',
components: {
HelloWorld
}
}
</script>

37
vue/src/views/Login.vue Normal file
View File

@ -0,0 +1,37 @@
<template>
<div class="hsa-login">
<!-- TODO: the <input>s need unique IDs in order to create labels, see e.g. https://stackoverflow.com/a/50121385/417040 -->
<input type="text" class="user" v-model="user" :disabled="working" />:<input type="password" class="pass" v-model="pass" :disabled="working" />
<button v-on:click="clickHandler" :disabled="working">Log In</button>
<div v-if="error">{{ error }}</div>
</div>
</template>
<script>
import HSAdmin from "../hsadmin";
export default {
name: "Login",
props: { hsadmin: HSAdmin },
data () { return {
user: "admin",
pass: "admin",
error: false,
working: false,
}},
methods: {
clickHandler: async function () {
this.error = false;
this.working = true;
try {
await this.hsadmin.authenticate(this.user, this.pass);
this.working = false;
} catch (error) {
this.working = false;
this.error = error;
return;
}
this.$router.push({name: "home"});
}
},
}
</script>