diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js new file mode 100644 index 0000000..37335ca --- /dev/null +++ b/app/assets/javascripts/application.js @@ -0,0 +1,47 @@ +document.addEventListener('DOMContentLoaded', function () { + const migrationActions = document.querySelectorAll('.migration-action'); + + migrationActions.forEach(button => { + button.addEventListener('click', function (event) { + const originalText = button.value; + button.value = 'Loading...'; + disableButtons(); + + const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); + + fetch(event.target.form.action, { + method: 'POST', + headers: { + 'X-Requested-With': 'XMLHttpRequest', + 'X-CSRF-Token': csrfToken + } + }) + .then(response => { + if (response.ok) { + window.location.reload(); + } else { + throw new Error('Network response was not ok.'); + } + }) + .catch(error => { + console.error('There has been a problem with your fetch operation:', error); + enableButtons(); + button.value = originalText; + }); + + event.preventDefault(); + }); + }); + + function disableButtons() { + migrationActions.forEach(button => { + button.disabled = true; + }); + } + + function enableButtons() { + migrationActions.forEach(button => { + button.disabled = false; + }); + } +}); diff --git a/app/assets/stylesheets/actual_db_schema/styles.css b/app/assets/stylesheets/styles.css similarity index 93% rename from app/assets/stylesheets/actual_db_schema/styles.css rename to app/assets/stylesheets/styles.css index 270577f..f9bd4aa 100644 --- a/app/assets/stylesheets/actual_db_schema/styles.css +++ b/app/assets/stylesheets/styles.css @@ -75,6 +75,12 @@ table { background-color: #000; } +.button:disabled, .button:hover:disabled { + background-color: transparent; + color: #666; + cursor: not-allowed; +} + .button-container { display: flex; } diff --git a/app/views/actual_db_schema/migrations/index.html.erb b/app/views/actual_db_schema/migrations/index.html.erb index 0680487..28c77e2 100644 --- a/app/views/actual_db_schema/migrations/index.html.erb +++ b/app/views/actual_db_schema/migrations/index.html.erb @@ -2,7 +2,9 @@ Migrations - <%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %> + <%= csrf_meta_tags %> + <%= stylesheet_link_tag 'styles', media: 'all' %> + <%= javascript_include_tag 'application' %>
@@ -45,12 +47,12 @@ <%= button_to '⎌ Rollback', rollback_migration_path(id: migration[:version], database: migration[:database]), method: :post, - class: 'button', + class: 'button migration-action', style: ('display: none;' if migration[:status] == "down") %> <%= button_to '⬆ Migrate', migrate_migration_path(id: migration[:version], database: migration[:database]), method: :post, - class: 'button', + class: 'button migration-action', style: ('display: none;' if migration[:status] == "up" || migration[:phantom]) %>
diff --git a/app/views/actual_db_schema/migrations/show.html.erb b/app/views/actual_db_schema/migrations/show.html.erb index 6e3f2a9..9bf28b7 100644 --- a/app/views/actual_db_schema/migrations/show.html.erb +++ b/app/views/actual_db_schema/migrations/show.html.erb @@ -2,7 +2,9 @@ Migration Details - <%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %> + <%= csrf_meta_tags %> + <%= stylesheet_link_tag 'styles', media: 'all' %> + <%= javascript_include_tag 'application' %>
@@ -41,12 +43,12 @@ <%= button_to '⎌ Rollback', rollback_migration_path(id: migration[:version], database: migration[:database]), method: :post, - class: 'button', + class: 'button migration-action', style: ('display: none;' if migration[:status] == "down") %> <%= button_to '⬆ Migrate', migrate_migration_path(id: migration[:version], database: migration[:database]), method: :post, - class: 'button', + class: 'button migration-action', style: ('display: none;' if migration[:status] == "up" || migration[:phantom]) %>
diff --git a/app/views/actual_db_schema/phantom_migrations/index.html.erb b/app/views/actual_db_schema/phantom_migrations/index.html.erb index 1c4f4df..c89ee9e 100644 --- a/app/views/actual_db_schema/phantom_migrations/index.html.erb +++ b/app/views/actual_db_schema/phantom_migrations/index.html.erb @@ -2,7 +2,9 @@ Phantom Migrations - <%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %> + <%= csrf_meta_tags %> + <%= stylesheet_link_tag 'styles', media: 'all' %> + <%= javascript_include_tag 'application' %>
@@ -10,7 +12,10 @@
<%= link_to 'All Migrations', migrations_path, class: "top-button" %> <% if phantom_migrations.present? %> - <%= button_to '⎌ Rollback all', rollback_all_phantom_migrations_path, method: :post, class: 'button' %> + <%= button_to '⎌ Rollback all', + rollback_all_phantom_migrations_path, + method: :post, + class: 'button migration-action' %> <% end %>
<% if phantom_migrations.present? %> @@ -39,8 +44,13 @@ <%= migration[:database] %>
- <%= link_to '👁 Show', phantom_migration_path(id: migration[:version], database: migration[:database]), class: 'button' %> - <%= button_to '⎌ Rollback', rollback_phantom_migration_path(id: migration[:version], database: migration[:database]), method: :post, class: 'button' %> + <%= link_to '👁 Show', + phantom_migration_path(id: migration[:version], database: migration[:database]), + class: 'button' %> + <%= button_to '⎌ Rollback', + rollback_phantom_migration_path(id: migration[:version], database: migration[:database]), + method: :post, + class: 'button migration-action' %>
diff --git a/app/views/actual_db_schema/phantom_migrations/show.html.erb b/app/views/actual_db_schema/phantom_migrations/show.html.erb index eebb236..7e22ff7 100644 --- a/app/views/actual_db_schema/phantom_migrations/show.html.erb +++ b/app/views/actual_db_schema/phantom_migrations/show.html.erb @@ -2,7 +2,9 @@ Phantom Migration Details - <%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %> + <%= csrf_meta_tags %> + <%= stylesheet_link_tag 'styles', media: 'all' %> + <%= javascript_include_tag 'application' %>
@@ -38,7 +40,10 @@
<%= link_to '← Back', phantom_migrations_path, class: 'button' %> - <%= button_to '⎌ Rollback', rollback_phantom_migration_path(id: params[:id], database: params[:database]), method: :post, class: 'button' %> + <%= button_to '⎌ Rollback', + rollback_phantom_migration_path(id: params[:id], database: params[:database]), + method: :post, + class: 'button migration-action' %>
<% flash.each do |key, message| %> diff --git a/lib/actual_db_schema/engine.rb b/lib/actual_db_schema/engine.rb index a245b27..8e9121a 100644 --- a/lib/actual_db_schema/engine.rb +++ b/lib/actual_db_schema/engine.rb @@ -11,7 +11,7 @@ class Engine < ::Rails::Engine mount ActualDbSchema::Engine => "/rails" end - app.config.assets.precompile += %w[actual_db_schema/styles.css] + app.config.assets.precompile += %w[styles.css application.js] end end end