Started moving user component to use models/services
All checks were successful
the build was successful

This commit is contained in:
kolaente 2019-02-26 09:01:33 +01:00
parent c67366de01
commit 62d5bf38e1
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
7 changed files with 115 additions and 51 deletions

View File

@ -11,7 +11,7 @@
<div class="field is-grouped">
<p class="control is-expanded" v-bind:class="{ 'is-loading': loading}">
<multiselect
v-model="newUser"
v-model="user"
:options="foundUsers"
:multiple="false"
:searchable="true"
@ -23,7 +23,7 @@
track-by="id">
<template slot="clear" slot-scope="props">
<div class="multiselect__clear" v-if="newUser.id !== 0" @mousedown.prevent.stop="clearAll(props.search)"></div>
<div class="multiselect__clear" v-if="user.id !== 0" @mousedown.prevent.stop="clearAll(props.search)"></div>
</template>
<span slot="noResult">Oops! No users found. Consider changing the search query.</span>
</multiselect>
@ -77,7 +77,7 @@
Admin
</template>
</button>
<button @click="userToDelete = u.id; showUserDeleteModal = true" class="button is-danger" v-if="u.id !== currentUser.id">
<button @click="user.user_id = u.id; showUserDeleteModal = true" class="button is-danger" v-if="u.id !== currentUser.id">
<span class="icon is-small">
<icon icon="trash-alt"/>
</span>
@ -100,13 +100,16 @@
</template>
<script>
import {HTTP} from '../../http-common'
import auth from '../../auth'
import message from '../../message'
import multiselect from 'vue-multiselect'
import 'vue-multiselect/dist/vue-multiselect.min.css'
import UserService from '../../services/user'
import UserModel from '../../models/user'
//import UserModel from '../../models/user'
import UserNamespaceModel from '../../models/userNamespace'
import UserListModel from '../../models/userList'
import UserListService from '../../services/userList'
import UserNamespaceService from '../../services/userNamespace'
export default {
name: 'user',
@ -117,16 +120,16 @@
},
data() {
return {
userService: UserService,
userService: UserService, // To search for users
userStuffService: Object, // This will be either UserListService or UserNamespaceService
user: Object, // This will be either UserNamespaceModel or UserListModel
loading: false,
currentUser: auth.user.infos,
typeString: '',
showUserDeleteModal: false,
users: [],
newUser: UserModel,
userToDelete: 0,
newUserid: 0,
foundUsers: [],
}
},
@ -137,8 +140,12 @@
this.userService = new UserService()
if (this.type === 'list') {
this.typeString = `list`
this.userStuffService = new UserListService()
this.user = new UserListModel({listID: this.id})
} else if (this.type === 'namespace') {
this.typeString = `namespace`
this.userStuffService = new UserNamespaceService()
this.user = new UserNamespaceModel({namespaceID: this.id})
} else {
throw new Error('Unknown type: ' + this.type)
}
@ -147,74 +154,61 @@
},
methods: {
loadUsers() {
const cancel = message.setLoading(this)
HTTP.get(this.typeString + `s/` + this.id + `/users`, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
this.userStuffService.getAll()
.then(response => {
//response.data.push(this.list.owner)
this.$set(this, 'users', response.data)
cancel()
this.$set(this, 'users', response)
})
.catch(e => {
cancel()
this.handleError(e)
})
},
deleteUser() {
const cancel = message.setLoading(this)
HTTP.delete(this.typeString + `s/` + this.id + `/users/` + this.userToDelete, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
this.userStuffService.delete(this.user)
.then(() => {
this.showUserDeleteModal = false;
this.handleSuccess({message: 'The user was successfully deleted from the ' + this.typeString + '.'})
this.loadUsers()
cancel()
})
.catch(e => {
cancel()
this.handleError(e)
})
},
addUser(admin) {
const cancel = message.setLoading(this)
if(admin === null) {
admin = false
}
this.newUser.right = 0
addUser(admin = false) {
this.user.right = 0
if (admin) {
this.newUser.right = 2
this.user.right = 2
}
// The api wants the user id as user_id
this.user.userID = this.user.id
// TODO
// let newUserShare = new UserShareModel({user_id: this.newUser.id, listID: lorem.
this.$set(this, 'foundUsers', [])
HTTP.put(this.typeString + `s/` + this.id + `/users`, this.newUser, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
this.userStuffService.create(this.user)
.then(() => {
this.loadUsers()
this.newUser = {}
this.handleSuccess({message: 'The user was successfully added.'})
cancel()
})
.catch(e => {
cancel()
this.handleError(e)
})
},
toggleUserType(userid, current) {
const cancel = message.setLoading(this)
let right = 0
this.user.userID = userid
this.user.right = 0
if (!current) {
right = 2
this.user.right = 2
}
HTTP.post(this.typeString + `s/` + this.id + `/users/` + userid, {right: right}, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
this.userStuffService.update(this.user)
.then(() => {
this.loadUsers()
this.handleSuccess({message: 'The user right was successfully updated.'})
cancel()
})
.catch(e => {
cancel()
this.handleError(e)
})
},
@ -226,7 +220,7 @@
return
}
this.$set(this, 'newUser', new UserModel)
//this.$set(this, 'user', new UserModel())
this.userService.getAll({}, {s: query})
.then(response => {

14
src/models/userList.js Normal file
View File

@ -0,0 +1,14 @@
import UserShareBaseModel from './userShareBase'
import {merge} from 'lodash'
// This class extends the user share model with a 'rights' parameter which is used in sharing
export default class UserListModel extends UserShareBaseModel {
defaults() {
return merge(
super.defaults(),
{
listID: 0,
}
)
}
}

View File

@ -0,0 +1,14 @@
import UserShareBaseModel from "./userShareBase";
import {merge} from 'lodash'
// This class extends the user share model with a 'rights' parameter which is used in sharing
export default class UserNamespaceModel extends UserShareBaseModel {
defaults() {
return merge(
super.defaults(),
{
namespaceID: 0,
}
)
}
}

View File

@ -1,15 +0,0 @@
import UserModel from "./user";
import {merge} from 'lodash'
// This class extends the user model with a 'rights' parameter which is used in sharing
export default class UserShareModel extends UserModel {
defaults() {
return merge(
super.defaults(),
{
right: 0,
}
)
}
}

View File

@ -0,0 +1,13 @@
import AbstractModel from './abstractModel'
export default class UserShareBaseModel extends AbstractModel {
defaults() {
return {
userID: 0,
right: 0,
created: 0,
updated: 0,
}
}
}

22
src/services/userList.js Normal file
View File

@ -0,0 +1,22 @@
import AbstractService from './abstractService'
import UserListModel from '../models/userList'
import UserModel from '../models/user'
export default class UserListService extends AbstractService {
constructor() {
super({
create: '/lists/{listID}/users',
getAll: '/lists/{listID}/users',
update: '/lists/{listID}/users/{userID}',
delete: '/lists/{listID}/users/{userID}',
})
}
modelFactory(data) {
return new UserListModel(data)
}
modelGetAllFactory(data) {
return new UserModel(data)
}
}

View File

@ -0,0 +1,22 @@
import AbstractService from './abstractService'
import UserNamespaceModel from '../models/userNamespace'
import UserModel from '../models/user'
export default class UserNamespaceService extends AbstractService {
constructor() {
super({
create: '/namespaces/{namespaceID}/users',
getAll: '/namespaces/{namespaceID}/users',
update: '/namespaces/{namespaceID}/users/{userID}',
delete: '/namespaces/{namespaceID}/users/{userID}',
})
}
modelFactory(data) {
return new UserNamespaceModel(data)
}
modelGetAllFactory(data) {
return new UserModel(data)
}
}