Skip to content

Commit 7fdb8f8

Browse files
committed
Adding the ability to change states form Dashboard
This enable users to change states through the Machinery Dashboard while still passing through the machinery internals, including guard and callback functions.
1 parent 18e75d3 commit 7fdb8f8

18 files changed

Lines changed: 233 additions & 72 deletions

File tree

lib/machinery/endpoint.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,12 @@ defmodule Machinery.Endpoint do
1111
key: "machinery_sid",
1212
table: :machinery_session
1313

14+
plug Plug.Parsers,
15+
parsers: [:urlencoded, :multipart, :json],
16+
pass: ["*/*"],
17+
json_decoder: Poison,
18+
length: 500_000_000
19+
20+
1421
plug Machinery.Router
1522
end

lib/web/controllers/resource_controller.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ defmodule Machinery.ResourceController do
22
use Machinery.Web, :controller
33
import Ecto.Query
44

5-
@items_per_page 50
5+
@items_per_page 30
6+
7+
def update(conn, %{"id" => id, "state" => state} = _params) do
8+
repo = conn.assigns.repo
9+
model = conn.assigns.model
10+
machinery_module = conn.assigns.module
11+
12+
struct = repo.get!(model, id)
13+
transition = Machinery.transition_to(struct, machinery_module, state)
14+
15+
json(conn, transition)
16+
end
617

718
def index(conn, %{"state" => state, "page" => page} = _params) do
819
repo = conn.assigns.repo
@@ -11,7 +22,6 @@ defmodule Machinery.ResourceController do
1122
resources = get_resources_for_state(repo, model, state, page)
1223
json(conn, resources)
1324
end
14-
1525
def index(conn, _params) do
1626
repo = conn.assigns.repo
1727
model = conn.assigns.model

lib/web/router.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ defmodule Machinery.Router do
2020

2121
scope "/api", Machinery do
2222
pipe_through :api
23-
get "/:state/resources/:page", ResourceController, :index
23+
post "/resources/:id", ResourceController, :update
24+
get "/resources/:state/:page", ResourceController, :index
2425
end
2526
end

lib/web/static/css/app.css

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,14 @@ ul li.resource-item:hover {
4343
background-color: #e6e7f8;
4444
}
4545

46-
div.col.filled {
47-
height: 85vh;
48-
min-height: 700px;
46+
div.col-md-2.filled {
47+
max-height: 50vh;
4948
overflow: hidden;
5049
}
5150

52-
div.col.filled div.holder-limiter {
53-
overflow: hidden;
54-
height: 45%;
55-
border-radius: 5px;
56-
}
57-
58-
div.col.filled div.holder {
51+
div.col-md-2.filled div.holder {
5952
overflow: scroll;
60-
border: 1px solid #ddd;
61-
height: 45%;
53+
height: 87%;
6254
border-radius: 5px;
6355
}
6456

lib/web/static/js/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
const ResourceList = require('./resource_list.js');
2+
const SortableList = require('./sortable_list.js');
23
const listHolders = $('.holder');
34
const lists = {};
45

6+
var el = $('.itemslol');
7+
8+
59
$.each(listHolders, function(_i, holder) {
610
let list = new ResourceList(holder);
711
let state = holder.dataset.state;
812
lists[state] = list;
913

14+
new SortableList(holder)
15+
1016
$(holder).find('.load-more-btn').on('click', function(event) {
1117
let requestedState = holder.dataset.state;
1218
lists[requestedState].loadNextPage();

lib/web/static/js/resource_loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = class ResourceLoader {
1212
if(!this.loading){
1313
let state = list.dataset.state
1414
let nextPage = page + 1;
15-
let url = `${this.url}${state}/resources/${nextPage}`
15+
let url = `${this.url}resources/${state}/${nextPage}`
1616

1717
let request = $.get(url, function(response) {
1818
callback(response)

lib/web/static/js/sortable_list.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const alertTemplate = require('./templates/alert_template');
2+
3+
module.exports = class SortableList {
4+
constructor(holder) {
5+
let listElm = $(holder).find('ul.list-group')[0]
6+
let sortable = new Sortable(listElm, {
7+
group: { name: 'lists', pull: true, put: true},
8+
onAdd: this.changeState.bind(this)
9+
});
10+
}
11+
12+
changeState(event) {
13+
let mountedPath = $("body")[0].dataset.mountedPath
14+
let itemEl = event.item;
15+
let resourceId = itemEl.dataset.id;
16+
let to_state = $(event.to).parents('.holder')[0].dataset.state;
17+
let url = `${mountedPath}/api/resources/${resourceId}`
18+
let data = {state: to_state}
19+
20+
$.post(url, data, function(response) {
21+
if(response[0] == "error") {
22+
this.rollbackTransition(response[1], event);
23+
}
24+
}.bind(this)).fail(function() {
25+
let message = "Error on the request, check logs and try again."
26+
this.rollbackTransition(message, event);
27+
}.bind(this));
28+
}
29+
30+
rollbackTransition(message, event) {
31+
let error_message = `${message} Item moved back to it's original state.`
32+
let template = alertTemplate.render(error_message);
33+
$('main').prepend(template);
34+
this.moveItemBack(event.item, event.from, event.oldIndex);
35+
}
36+
37+
moveItemBack(item, state, index) {
38+
let _index = (index == 0) ? 0 : index - 1
39+
let _item = $(state).find("li.resource-item").eq(_index)
40+
41+
if(index == 0){
42+
$(item).insertBefore(_item);
43+
}
44+
else {
45+
$(item).insertAfter(_item);
46+
}
47+
}
48+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let renderAlert = function(message) {
2+
return `
3+
<div class="alert alert-danger alert-dismissible fade show" role="alert">
4+
${message}
5+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
6+
<span aria-hidden="true">&times;</span>
7+
</button>
8+
</div>
9+
`
10+
11+
}
12+
13+
module.exports = {
14+
render: renderAlert
15+
}

lib/web/static/js/templates/resource_template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let renderResource = function(model, resource) {
2-
return `<li class="list-group-item resource-item" data-toggle="modal" data-target="#modal${resource.id}">
2+
return `<li class="list-group-item resource-item sortable-drag" data-toggle="modal" data-target="#modal${resource.id}" data-id="${resource.id}">
33
${model} - #${resource.id}
44
</li>
55

0 commit comments

Comments
 (0)