Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#2698 persist column visibility state stage 3 completes issue #3

Open
wants to merge 4 commits into
base: issue#2698_persist_column_visibility_state_stage_2_add_table_state_to_preference_sets_rake_task
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/controllers/preference_sets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class PreferenceSetsController < ApplicationController
before_action :skip_authorization, :set_table_name
def table_state
render json: PreferenceSetTableStateService.new(user_id: current_user.id).table_state(
table_name: @table_name
)
end

def table_state_update
render json: PreferenceSetTableStateService.new(user_id: current_user.id).update!(
table_state: params["table_state"],
table_name: @table_name
)

rescue PreferenceSetTableStateService::TableStateUpdateFailed
render json: {error: "Failed to update table state for '#{@table_name}'" }
end


private
def set_table_name
@table_name = params[:table_name]
end
end

13 changes: 13 additions & 0 deletions app/decorators/preference_set_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class PreferenceSetDecorator < ApplicationDecorator
delegate_all

# Define presentation-specific methods here. Helpers are accessed through
# `helpers` (aka `h`). You can override attributes, for example:
#
# def created_at
# helpers.content_tag :span, class: 'time' do
# object.created_at.strftime("%a %m/%d/%y")
# end
# end

end
2 changes: 2 additions & 0 deletions app/helpers/preference_sets_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PreferenceSetsHelper
end
61 changes: 42 additions & 19 deletions app/javascript/src/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,42 @@ $('document').ready(() => {
const casaCasePath = id => `/casa_cases/${id}`
const volunteersTable = $('table#volunteers').DataTable({
autoWidth: false,
stateSave: false,
stateSave: true,
initComplete: function(settings, json) {
this.api().columns().every(function(index) {
var columnVisible = this.visible();
$('#visibleColumns input[data-column="' + index + '"]').prop('checked', columnVisible);
});
},
stateSaveCallback: function (settings, data) {
$.ajax({
url: "/preference_sets/table_state_update/" + settings.nTable.id + '_table',

data: {
table_state: JSON.stringify(data),
},
dataType: "json",
type: "POST",
success: function (response) { console.log( 'from stateSaveCallback', data) }
});

},
stateSaveParams: function(settings, data) {
data.search.search = '';
return data;
},
stateLoadCallback: function (settings, callback) {
$.ajax({
url: '/preference_sets/table_state/' + settings.nTable.id + '_table',
dataType: 'json',
type: 'GET',
success: function(json) {
callback(json);
}
});
},
order: [[6, 'desc']],
columns: [
columns: [
{
name: 'display_name',
render: (data, type, row, meta) => {
Expand All @@ -107,7 +140,6 @@ $('document').ready(() => {
{
name: 'email',
render: (data, type, row, meta) => row.email,
visible: false
},
{
className: 'supervisor-column',
Expand Down Expand Up @@ -171,7 +203,6 @@ $('document').ready(() => {
: 'None ❌'
},
searchable: false,
visible: true
},
{
name: 'contacts_made_in_past_days',
Expand All @@ -182,7 +213,6 @@ $('document').ready(() => {
`
},
searchable: false,
visible: false
},
{
name: 'hours_spent_in_days',
Expand All @@ -201,19 +231,18 @@ $('document').ready(() => {
return row.extra_languages.length > 0 ? `<span class="language-icon" data-toggle="tooltip" title="${languages}">🌎</span>` : ''
},
searchable: false,
visible: true
},
{
name: 'actions',
orderable: false,
render: (data, type, row, meta) => {
return `
<span class="mobile-label">Actions</span>
<a href="${editVolunteerPath(row.id)}" class="main-btn primary-btn btn-hover btn-sm">
<i class="lni lni-pencil-alt mr-5"></i> Edit
<span class="mobile-label">Actions</span>
<a href="${editVolunteerPath(row.id)}" class="btn btn-primary">
Edit
</a>
<a href="${impersonateVolunteerPath(row.id)}" class="main-btn dark-btn btn-hover btn-sm">
<i class="lni lni-user mr-5"></i> Impersonate
<a href="${impersonateVolunteerPath(row.id)}" class="btn btn-secondary">
Impersonate
</a>
`
},
Expand Down Expand Up @@ -258,15 +287,9 @@ $('document').ready(() => {
// columns are visible
volunteersTable.columns().every(function (index) {
const columnVisible = this.visible()

if (columnVisible) {
$('#visibleColumns input[data-column="' + index + '"]').prop('checked', true)
} else {
$('#visibleColumns input[data-column="' + index + '"]').prop('checked', false)
}

$('#visibleColumns input[data-column="' + index + '"]').prop('checked', columnVisible);
return true
})
})

const casaCasesTable = $('table#casa-cases').DataTable({
autoWidth: false,
Expand Down
25 changes: 25 additions & 0 deletions app/services/preference_set_table_state_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class PreferenceSetTableStateService
class TableStateUpdateFailed < StandardError; end
def initialize(user_id:)
@user_id = user_id
end

def table_state(table_name:)
preference_set.table_state[table_name]
end

def update!(table_name:, table_state:)
preference_set.table_state[table_name] = table_state
preference_set.save!

rescue StandardError
raise TableStateUpdateFailed, "Failed to update table state for '#{table_name}'"
end

private
attr_reader :user_id
def preference_set
@preference_set ||= PreferenceSet.find_by!(user_id: user_id)
end

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an empty line at the end.

7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
devise_for :all_casa_admins, path: "all_casa_admins", controllers: {sessions: "all_casa_admins/sessions"}
devise_for :users, controllers: {sessions: "users/sessions", passwords: "users/passwords"}

resources :preference_sets, only: [] do
collection do
post "/table_state_update/:table_name", to: "preference_sets#table_state_update", as: :table_state_update
get "/table_state/:table_name", to: "preference_sets#table_state", as: :table_state
end
end

concern :with_datatable do
post "datatable", on: :collection
end
Expand Down
4 changes: 4 additions & 0 deletions spec/decorators/preference_set_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'

RSpec.describe PreferenceSetDecorator do
end
15 changes: 15 additions & 0 deletions spec/helpers/preference_sets_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the PreferenceSetsHelper. For example:
#
# describe PreferenceSetsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe PreferenceSetsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
7 changes: 7 additions & 0 deletions spec/requests/preference_sets_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rails_helper'

RSpec.describe "PreferenceSets", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{__FILE__}"
end
end
61 changes: 61 additions & 0 deletions spec/services/preference_set_table_state_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "rails_helper"

RSpec.describe PreferenceSetTableStateService do
subject { described_class.new(user_id: user.id) }

let!(:user) { create(:user) }
let(:preference_set) { user.preference_set }
let!(:table_state) { { "volunteers_table" => { "columns" => [{"visible" => true}] }} }
let(:table_state2) { { "columns" => [{"visible" => false}] }}
let(:table_name) { "volunteers_table" }

describe '#update!' do
context 'when the update is successful' do
it 'updates the table state' do
expect {
subject.update!(table_state: table_state2, table_name: table_name)
}.to change {
preference_set.reload.table_state
}.from({}).to({ table_name => table_state2 })
end
end

context 'when the update fails' do
before do
allow_any_instance_of(PreferenceSet).to receive(:save!).and_raise(ActiveRecord::RecordNotSaved)
end

it 'raises an error' do
expect {
subject.update!(table_state: table_state2, table_name: table_name)
}.to raise_error(PreferenceSetTableStateService::TableStateUpdateFailed, "Failed to update table state for '#{table_name}'")
end
end
end

describe '#table_state' do
context 'when the preference set exists' do
before do
table_state = { "columns" => [{"visible" => true}]}
user.preference_set.table_state["volunteers_table"] = table_state
user.preference_set.save!
end

it 'returns the table state' do
binding.pry
expect(subject.table_state(table_name: "volunteers_table")).to eq(table_state["volunteers_table"])
end
end

context 'when there is no data for that table name' do
before do
preference_set.table_state = {}
preference_set.save
end

it 'returns nil' do
expect(subject.table_state(table_name: table_name)).to eq(nil)
end
end
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an empty line.