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;
}
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 addTurnServer({ request, response }) {}
@ -21,17 +33,20 @@ class AdminApiController {
try {
if (EmailUtils.sendTestEmail(auth.user)) {
return {
code: 0, data: {}
code: 0,
data: {}
}
}
return {
code: 500, message: 'Something went wrong'
code: 500,
message: 'Something went wrong'
}
} catch (e) {
response.code(500);
return {
code: 500, message: e.message
code: 500,
message: e.message
}
}
}

View file

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

View file

@ -134,7 +134,7 @@ module.exports = {
csrf: {
enable: true,
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 }
}
}

View file

@ -12,6 +12,7 @@
"migrate": "npx adonis migration:run -f",
"build": "npm run migrate && npm run build:css && npm run build:applications",
"start": "npm run migrate && node server.js",
"start:dev": "npx adonis serve --dev",
"clean": "bash clean-hot-update.sh",
"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()">
test
</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">
<div class="level-left">
<div class="level-item">
@ -35,6 +38,7 @@
<th>name</th>
<th>email</th>
<th>admin</th>
<th>edit</th>
</tr>
</thead>
<tr v-for="user in users" :key="user.id">
@ -49,13 +53,15 @@
<td>
<input class="checkbox" type="checkbox" :checked="user.is_admin" />
</td>
<td>
<button v-if="!user.is_admin" class="button" @click="onDeleteClicked(user)">Delete</button>
</td>
</tr>
</table>
</div>
</template>
<script lang="ts">
import ChildAvatar, { IChildAvatar } from "../components/child_avatar.vue";
import Services from "../../services/index";
import { mapGetters, mapActions } from "vuex";
import Modal from "../../shared/components/Modal/Modal.vue";
@ -63,22 +69,28 @@ import Modal from "../../shared/components/Modal/Modal.vue";
export default {
name: "Home",
components: {
ChildAvatar,
Modal
},
methods: {
createUser(){
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"])
},
async created() {
this.loading = true;
if (this.users === null) await this.getUsers();
this.loading = false;
// this.connections = await Services.ApiService.getConnections();
// this.users = await Services.ApiService.getAllUsers();
// console.dir(connections);
},
computed: {
// async users() {
@ -89,6 +101,8 @@ export default {
return {
loading: true,
showCreateUser: false,
showDeleteUser: false,
currentUser: null,
};
}
};

View file

@ -1,5 +1,15 @@
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) {
try {
return (await fetch('/api/v1/client/user/')).json();

View file

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