Adding Delete user button and basic functionality

This commit is contained in:
Aran Zaiger 2020-05-23 22:02:04 +03:00
parent 4d934fbcdf
commit 3bfa60db28
11 changed files with 336 additions and 295 deletions

View file

@ -14,6 +14,18 @@ class AdminApiController {
// }); // });
return users; return users;
} }
async deleteUser({ request, response }) {
console.log('in delete user')
const { id } = request.params
const user = await User.find(id)
let userLinks = await user.links().fetch();
const links = userLinks.rows
const promises = [...links.map(l => (l.delete())), user.delete()];
return await Promise.all(promises);
}
async addStunServer({ request, response }) {} async addStunServer({ request, response }) {}
async addTurnServer({ request, response }) {} async addTurnServer({ request, response }) {}
@ -21,17 +33,20 @@ class AdminApiController {
try { try {
if (EmailUtils.sendTestEmail(auth.user)) { if (EmailUtils.sendTestEmail(auth.user)) {
return { return {
code: 0, data: {} code: 0,
data: {}
} }
} }
return { return {
code: 500, message: 'Something went wrong' code: 500,
message: 'Something went wrong'
} }
} catch (e) { } catch (e) {
response.code(500); response.code(500);
return { return {
code: 500, message: e.message code: 500,
message: e.message
} }
} }
} }

View file

@ -22,13 +22,13 @@ class User extends Model {
}) })
} }
publicJSON() { // publicJSON() {
const u = this.toJSON(); // const u = this.toJSON();
return { // return {
avatar: `https://api.adorable.io/avatars/285/${u.email}.png`, id: u.id, // avatar: `https://api.adorable.io/avatars/285/${u.email}.png`, id: u.id,
name: u.name, isAdmin: u.is_admin // name: u.name, isAdmin: u.is_admin
} // }
} // }
static get hidden() { static get hidden() {
return ['password'] return ['password']

View file

@ -134,7 +134,7 @@ module.exports = {
csrf: { csrf: {
enable: true, enable: true,
methods: ['POST', 'PUT', 'DELETE'], methods: ['POST', 'PUT', 'DELETE'],
filterUris: [/api\/v1\/client\/\w+/], // All Client API routes filterUris: [/api\/v1\/client\/\w+/, /api\/v1\/admin\/\w+/], // All Client API routes
cookieOptions: { httpOnly: false, sameSite: true, path: '/', maxAge: 7200 } cookieOptions: { httpOnly: false, sameSite: true, path: '/', maxAge: 7200 }
} }
} }

View file

@ -12,6 +12,7 @@
"migrate": "npx adonis migration:run -f", "migrate": "npx adonis migration:run -f",
"build": "npm run migrate && npm run build:css && npm run build:applications", "build": "npm run migrate && npm run build:css && npm run build:applications",
"start": "npm run migrate && node server.js", "start": "npm run migrate && node server.js",
"start:dev": "npx adonis serve --dev",
"clean": "bash clean-hot-update.sh", "clean": "bash clean-hot-update.sh",
"test": "node ace test" "test": "node ace test"
}, },

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -3,6 +3,9 @@
<Modal title="CreateUser" :isActive="showCreateUser" @close="showCreateUser=false" acceptText="Create" @accept="createUser()"> <Modal title="CreateUser" :isActive="showCreateUser" @close="showCreateUser=false" acceptText="Create" @accept="createUser()">
test test
</Modal> </Modal>
<Modal title="DeleteUser" :isActive="showDeleteUser" @close="showDeleteUser=false; currentUser=null" acceptText="Delete" rejectText="Cancel" @accept="deleteUser(currentUser)">
Are you sure you want to delete {{user.name}}?
</Modal>
<nav class="level"> <nav class="level">
<div class="level-left"> <div class="level-left">
<div class="level-item"> <div class="level-item">
@ -35,6 +38,7 @@
<th>name</th> <th>name</th>
<th>email</th> <th>email</th>
<th>admin</th> <th>admin</th>
<th>edit</th>
</tr> </tr>
</thead> </thead>
<tr v-for="user in users" :key="user.id"> <tr v-for="user in users" :key="user.id">
@ -49,13 +53,15 @@
<td> <td>
<input class="checkbox" type="checkbox" :checked="user.is_admin" /> <input class="checkbox" type="checkbox" :checked="user.is_admin" />
</td> </td>
<td>
<button v-if="!user.is_admin" class="button" @click="onDeleteClicked(user)">Delete</button>
</td>
</tr> </tr>
</table> </table>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import ChildAvatar, { IChildAvatar } from "../components/child_avatar.vue";
import Services from "../../services/index"; import Services from "../../services/index";
import { mapGetters, mapActions } from "vuex"; import { mapGetters, mapActions } from "vuex";
import Modal from "../../shared/components/Modal/Modal.vue"; import Modal from "../../shared/components/Modal/Modal.vue";
@ -63,22 +69,28 @@ import Modal from "../../shared/components/Modal/Modal.vue";
export default { export default {
name: "Home", name: "Home",
components: { components: {
ChildAvatar,
Modal Modal
}, },
methods: { methods: {
createUser(){ createUser(){
alert('created'); alert('created');
}, },
async deleteUser(user){
console.log(user)
await Services.ApiService.deleteUser(user);
this.showDeleteUser=false;
await this.getUsers()
},
onDeleteClicked(user){
this.showDeleteUser = true;
this.currentUser = user;
},
...mapActions(["getUsers"]) ...mapActions(["getUsers"])
}, },
async created() { async created() {
this.loading = true; this.loading = true;
if (this.users === null) await this.getUsers(); if (this.users === null) await this.getUsers();
this.loading = false; this.loading = false;
// this.connections = await Services.ApiService.getConnections();
// this.users = await Services.ApiService.getAllUsers();
// console.dir(connections);
}, },
computed: { computed: {
// async users() { // async users() {
@ -89,6 +101,8 @@ export default {
return { return {
loading: true, loading: true,
showCreateUser: false, showCreateUser: false,
showDeleteUser: false,
currentUser: null,
}; };
} }
}; };

View file

@ -1,5 +1,15 @@
export default class ApiService { export default class ApiService {
static async deleteUser(user: any) {
try{
return (await fetch(`/api/v1/admin/user/${user.id}`, {method: 'DELETE'})).json();
}
catch (e) {
console.error(`deleteUser ERROR: ${e.message}`);
return e;
}
}
static async getUser(userId?: number) { static async getUser(userId?: number) {
try { try {
return (await fetch('/api/v1/client/user/')).json(); return (await fetch('/api/v1/client/user/')).json();

View file

@ -77,6 +77,7 @@ Route.get('/u/images/:fileName', 'CdnController.publicImages');
Route Route
.group(() => { .group(() => {
Route.get('users', 'AdminApiController.getUsers'); Route.get('users', 'AdminApiController.getUsers');
Route.delete('user/:id', 'AdminApiController.deleteUser');
Route.get( Route.get(
'settings/email/test/result', 'AdminApiController.testEmailSettings'); 'settings/email/test/result', 'AdminApiController.testEmailSettings');
}) })