-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathProjects.vue
188 lines (184 loc) · 6.18 KB
/
Projects.vue
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
183
184
185
186
187
188
<template>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="component-block">
<label>Component : Modal</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<button class="btn btn-dark" data-toggle="modal" @click="openModal">Add New</button>
<table class="table mt-2 mb-5">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<tr v-for="project, index in projects">
<th scope="row">{{ index + 1 }}</th>
<td>{{ project.name }}</td>
<td>{{ project.description | stripText }}</td>
<td>
<a href="javascript:void(0);" data-id="project.id" @click="editProject(project)">
<i class="fa fa-edit"></i>
</a>
<a href="javascript:void(0);" @click="deleteProject(project.id)">
<i class="fa fa-trash"></i>
</a>
</td>
</tr>
<tr v-if="projects.length == 0" class="text-center">
<td colspan="4">No project found</td>
</tr>
</tbody>
</table>
</div>
</div>
<form class="form-horizontal" role="form" method="POST" @submit.prevent="editMode ? updateProject() : createProject()" @keydown="form.errors.clear($event.target.name)">
<modal @close="closeModal" v-show="showModal">
<template slot="header">
<h5 v-show="!editMode" class="modal-title">Add New Project</h5>
<h5 v-show="editMode" class="modal-title">Update Project</h5>
</template>
<input type="hidden" name="_token" :value="csrf">
<div class="form-group">
<label for="name">Project Name</label>
<input type="text" name="name" class="form-control" autocomplete="off" v-model="form.name">
<span class="help-block" v-if="form.errors.has('name')">
<strong v-text="form.errors.get('name')"></strong>
</span>
</div>
<div class="form-group">
<label for="description">Project Description</label>
<textarea name="description" class="form-control" autocomplete="off" v-model="form.description"></textarea>
<span class="help-block" v-if="form.errors.has('description')">
<strong v-text="form.errors.get('description')"></strong>
</span>
</div>
<template slot="footer">
<button type="button" class="btn btn-danger" data-dismiss="modal" @click="closeModal">Close</button>
<button v-show="editMode" type="submit" class="btn btn-secondary-custom" :disabled="form.errors.any()">Update</button>
<button v-show="!editMode" type="submit" class="btn btn-secondary-custom" :disabled="form.errors.any()">Create</button>
</template>
</modal>
</form>
</div>
</template>
<script>
import 'sweetalert2/dist/sweetalert2.min.css';
import Modal from '../components/Modal.vue';
export default {
components: { Modal },
data() {
return {
showModal: false,
editMode: false,
projects: [],
form: new Form({
name: '',
description: '',
}),
csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content')
}
},
created() {
this.loadProjects();
this.$on('completed', () => {
this.showModal = false;
this.loadProjects();
});
},
filters: {
stripText(text) {
let body = text.replace(/(<([^>]+)>)/ig, '');
return body.length > 25 ? body.substring(0, 25) + '...' : body;
}
},
methods: {
openModal() {
this.editMode = false;
this.form.reset();
this.showModal = true;
},
closeModal() {
this.showModal = false;
this.form.reset();
},
loadProjects() {
axios.get("/projects")
.then( response => {
this.projects = response.data
});
},
createProject() {
this.form.post('/project')
.then( data => {
if ( data.success ) {
this.$emit('completed');
this.$swal({
type: 'success',
title: 'Created!',
text: data.message,
confirmButtonColor: '#3fb984',
});
}
}).catch( error => {
});
},
editProject(project) {
this.editMode = true;
this.form.reset();
this.showModal = true;
this.form.fill(project);
},
updateProject() {
this.form.put('/project/'+this.form.id)
.then( data => {
if ( data.success ) {
this.$emit('completed');
this.$swal({
type: 'success',
title: 'Updated!',
text: data.message,
confirmButtonColor: '#3fb984',
});
}
}).catch( error => {
});
},
deleteProject(id) {
this.$swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3fb984',
cancelButtonColor: '#dc3545',
confirmButtonText: 'Yes, delete it!'
}).then( (result) => {
if (result.value) {
this.form.delete('/project/'+id)
.then( data => {
if ( data.success ) {
this.$emit('completed');
this.$swal({
type: 'success',
title: 'Deleted!',
text: data.message,
confirmButtonColor: '#3fb984',
});
}
}).catch( error => {
});
}
});
}
}
}
</script>