Skip to content

Commit

Permalink
reimplement 'new document' modal in vue (#446)
Browse files Browse the repository at this point in the history
Co-authored-by: Erin Thomas <aronthomas@MacBook-Pro.local>
Co-authored-by: Isaac Kimaiyo <40719885+Kimaiyo077@users.noreply.github.com>
Co-authored-by: Andrew <andrew.tc.pham@gmail.com>
  • Loading branch information
4 people authored Apr 17, 2020
1 parent 1e9eec9 commit d2e147e
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 182 deletions.
172 changes: 172 additions & 0 deletions static/vue.js/components/documents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
Vue.use(VeeValidate);
Vue.component('ValidationProvider', VeeValidate.ValidationProvider);
Vue.component('v-select', VueSelect.VueSelect);
Vue.component('modal', {
template: '#modal-template',
});

// start app
new Vue({
delimiters: ['[[', ']]'],
el: '#documents',
data: {
showModal: false,
programs_list: [],
name: '',
url: '',
description: '',
parent_id: '',
program_id: '',
isEdit: false,
modalHeader: 'Add new document',
filtered_program: false,
filtered_program_id: 0
},
beforeMount: function () {

this.makeRequest('GET', '/workflow/level1_program/')
.then(response => {
if (response.data) {
this.programs_list = response.data.map(el => {
el['program_id'] = el['id'];
delete el['id'];
return el;
});
}
})
.catch(e => {
toastr.error('There was a problem loading data from the database');
});

$('#documentationtable').DataTable();

},

methods: {
/**
* open or close the document form modal
* @param { object } item - document item
*/
toggleModal: function (item = null) {
this.showModal = !this.showModal;
if (item) {
this.isEdit = true;
this.modalHeader = `Edit ${item.name}`;
this.name = item.name;
this.description = item.description;
this.program_id = item.program_id;
} else {
this.isEdit = false;
this.name = '';
this.description = '';
this.program_id = '';
this.modalHeader = 'Add new document';
}
},

/**
* Format date
* @param {string} date - date to be formatted
*/
formatDate: function (date) {
return moment(date, 'YYYY-MM-DDThh:mm:ssZ').format('YYYY-MM-DD');
},

/**
* process form data
* @param { boolean } saveNew - true to keep the modal open for additional posts
*/
processForm: function (saveNew = false) {
this.$validator.validateAll().then(result => {
if (result) {
if (saveNew) {
this.postData(saveNew);
} else {
this.postData();
}
}
});
},

/**
* Create new document
* @param { boolean } saveNew - true if a user wants to make multiple documents
*/
async postData(saveNew) {
let csrfmiddlewaretoken = document.querySelectorAll("input[name=csrfmiddlewaretoken]")[0].value
let data = {
name: this.name,
url: this.url,
program: this.program_id.toString(),
csrfmiddlewaretoken
}
console.log('heyy',data)
// return;
try {
const response = await this.makeRequest(
'POST',
"/workflow/documentation/add",
data
);
if (response) {
toastr.success('Documentation was added successfully', `${response}`);
response.data['program_id'] = response.data['program'];
delete response.data['program'];
this.setFilter(this.filtered_program_id);
if (!saveNew) {
this.toggleModal();
}
// resetting the form
this.name = '';
this.url = '';
this.program_id = '';
this.$validator.reset();

const urlWithoutQueryString = window.location.href.split('?')[0];
if (saveNew) {
window.location.replace(`${urlWithoutQueryString}?quick-modal=true`);
} else {
setTimeout(() => {
window.location.replace(urlWithoutQueryString);
}, 2000);
}
}
} catch (error) {
toastr.error('There was a problem saving');
}
},

// Set workflow level 1 filter
setFilter: function (id) {
this.filtered_program = true;
this.filtered_program_id = id;
},

/**
* make requests for CRUD operations using axios
* @param { string } method - request method
* @param { string } url - request url
* @param { string } data - request payload
* @return { Promise } - axios response Promise
*/
makeRequest(method, url, data = null) {
axios.defaults.xsrfHeaderName = 'X-CSRFToken';
axios.defaults.xsrfCookieName = 'csrftoken';
return axios({ method, url, data });
},

blur(field) {
const provider = this.$refs[field];
return provider.validate();
}
},

computed: {
/**
* Check if document form is valid
*/
isFormValid() {
return this.name && this.program_id && this.url;
}
}
});
112 changes: 56 additions & 56 deletions templates/workflow/documentation_list.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{% extends "base.html" %}
{% extends "base.html" %}
{% block content %}
<script>
$(document).ready(() => {
$('#documentationtable').DataTable();
});
</script>

<div class="container">
{% include 'ui-components/vue_modal.html'%}
<div class="container" id="documents">
{% block breadcrumbs %}
<ul class="breadcrumb">
<li><a href="{% url 'index' %}">My Dashboard</a></li>
Expand All @@ -26,62 +22,68 @@ <h4 class="page-title">Document List</h4>
<div class="sub-nav-item">
<div class="btn-group" role="group" aria-label="...">
<div class="btn-group" role="group">
<button
type="button"
class="btn btn-sm btn-default dropdown-toggle"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
<button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
{{ user.activity_user.organization.level_1_label }}
<span class="fa {% if program_id == 0 %} fa-angle-down {% else %} fa-filter {% endif %}"></span>
</button>
<ul class="dropdown-menu">
<li class="{% if program_id == 0 %} active {% endif %}"><a href="/workflow/documentation_list/0/{{project_agreement_id}}/">--All--</a></li>
<li class="{% if program_id == 0 %} active {% endif %}"><a
href="/workflow/documentation_list/0/{{project_agreement_id}}/">--All--</a></li>
{% for program in get_programs %}
{% if program.name %}
<li class="{% if program_id == program.id %} active {% endif %}">
<a href="/workflow/documentation_list/{{program.id}}/{{project_agreement_id}}/">{{ program.name | truncatechars:30 }}</a>
<a
href="/workflow/documentation_list/{{program.id}}/{{project_agreement_id}}/">{{ program.name | truncatechars:30 }}</a>
</li>
{% endif%}
{% endfor %}
</ul>
</div>
<div class="btn-group" role="group">
<button
type="button"
class="btn btn-sm btn-default dropdown-toggle"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
{{ user.activity_user.organization.level_2_label }}
<span class="fa {% if project_agreement_id == 0 %} fa-angle-down {% else %} fa-filter {% endif %}"></span>
</button>
<ul class="dropdown-menu">
<li class="{% if project_agreement_id == 0 %} active {% endif %}"><a href="/workflow/documentation_list/{{program_id}}/0/">--All--</a></li>
{% for project in get_projects %}
<li class="{% if project_agreement_id == project.id %} active {% endif %}">
<a href="/workflow/documentation_list/{{program_id}}/{{project.id}}/">{{ project.project_name | truncatechars:30 }}</a>
</li>
{% endfor %}
</ul>
<button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
{{ user.activity_user.organization.level_2_label }}
<span class="fa {% if project_agreement_id == 0 %} fa-angle-down {% else %} fa-filter {% endif %}"></span>
</button>
<ul class="dropdown-menu">
<li class="{% if project_agreement_id == 0 %} active {% endif %}"><a
href="/workflow/documentation_list/{{program_id}}/0/">--All--</a></li>
{% for project in get_projects %}
<li class="{% if project_agreement_id == project.id %} active {% endif %}">
<a
href="/workflow/documentation_list/{{program_id}}/{{project.id}}/">{{ project.project_name | truncatechars:30 }}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
<div class="sub-nav-item">
<div class="btn-group" role="group" aria-label="...">
<button
class="btn btn-sm btn-primary"
data-toggle="modal"
data-target="#addDocumentModal"
>
<a
role="button"
class="btn btn-sm btn-primary"
id="show-modal"
@click="toggleModal()">
<i class="fa fa-plus"></i> Document
</button>
</a>
</div>
</div>
<!-- Objective create modal -->

</div>

</div>
<modal v-if="showModal" @close="showModal = false">
<h4 slot="header">[[modalHeader]]</h4>
<div slot="body">
{% include 'workflow/modals/add_document_form.html' %}
</div>
<div slot="footer">
{% include 'ui-components/quick_entry_modal_footer.html' %}
</div>
</modal>

<!-- Table -->
<table class="table" id="documentationtable">
Expand All @@ -97,15 +99,12 @@ <h4 class="page-title">Document List</h4>
{% for document in get_documentation %}
<tr>
<td>
<a href="{% url 'documentation_update' pk=document.id %}"
data-toggle="tooltip"
<a href="{% url 'documentation_update' pk=document.id %}" data-toggle="tooltip"
title="{{ document.name }}">{{ document.name | truncatechars:50 }}</a>
</td>
<td>{{ document.program.name }}</td>
<td>
<a href="{{ document.url }}" target="_blank"
data-toggle="tooltip"
title="{{ document.url }}">
<a href="{{ document.url }}" target="_blank" data-toggle="tooltip" title="{{ document.url }}">
{{ document.url | truncatechars:50 }}
</a>
</td>
Expand All @@ -115,13 +114,8 @@ <h4 class="page-title">Document List</h4>
<button type="button" class="btn btn-default btn-sm disabled">
Actions
</button>
<button
type="button"
class="btn btn-default btn-sm dropdown-toggle"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
Expand All @@ -132,11 +126,17 @@ <h4 class="page-title">Document List</h4>
</div>
</td>
</tr>

{% endfor %}
</tbody>
</table>

<!-- add document modal -->
{% include 'workflow/modals/add_document_modal.html' %}
{% endblock content %}


<!-- add document modal -->

{% endblock content %}

{% block extra_js_in_body %}
<script type="text/javascript" src="{{ STATIC_URL }}vue.js/components/documents.js"></script>
{% endblock %}
Loading

0 comments on commit d2e147e

Please sign in to comment.