Refactor connection toggle to use API URLs

This commit is contained in:
jeremystretch 2021-09-08 14:25:14 -04:00
parent 2c1745ce28
commit cf8fdacfa3
4 changed files with 5 additions and 5 deletions

Binary file not shown.

Binary file not shown.

View File

@ -8,12 +8,12 @@ import { isTruthy, apiPatch, hasError, getElements } from '../util';
* @param element Connection Toggle Button Element * @param element Connection Toggle Button Element
*/ */
function toggleConnection(element: HTMLButtonElement): void { function toggleConnection(element: HTMLButtonElement): void {
const id = element.getAttribute('data'); const url = element.getAttribute('data-url');
const connected = element.classList.contains('connected'); const connected = element.classList.contains('connected');
const status = connected ? 'planned' : 'connected'; const status = connected ? 'planned' : 'connected';
if (isTruthy(id)) { if (isTruthy(url)) {
apiPatch(`/api/dcim/cables/${id}/`, { status }).then(res => { apiPatch(url, { status }).then(res => {
if (hasError(res)) { if (hasError(res)) {
// If the API responds with an error, show it to the user. // If the API responds with an error, show it to the user.
createToast('danger', 'Error', res.error).show(); createToast('danger', 'Error', res.error).show();

View File

@ -1,10 +1,10 @@
{% if perms.dcim.change_cable %} {% if perms.dcim.change_cable %}
{% if cable.status == 'connected' %} {% if cable.status == 'connected' %}
<button type="button" class="btn btn-warning btn-sm cable-toggle connected" title="Mark Planned" data="{{ cable.pk }}"> <button type="button" class="btn btn-warning btn-sm cable-toggle connected" title="Mark Planned" data-url="{% url 'dcim-api:cable-detail' pk=cable.pk %}">
<i class="mdi mdi-lan-disconnect" aria-hidden="true"></i> <i class="mdi mdi-lan-disconnect" aria-hidden="true"></i>
</button> </button>
{% else %} {% else %}
<button type="button" class="btn btn-info btn-sm cable-toggle" title="Mark Installed" data="{{ cable.pk }}"> <button type="button" class="btn btn-info btn-sm cable-toggle" title="Mark Installed" data-url="{% url 'dcim-api:cable-detail' pk=cable.pk %}">
<i class="mdi mdi-lan-connect" aria-hidden="true"></i> <i class="mdi mdi-lan-connect" aria-hidden="true"></i>
</button> </button>
{% endif %} {% endif %}