Skip to content

Commit 5a15e13

Browse files
committed
PC-58 updated add parameters page
1 parent 2f25645 commit 5a15e13

7 files changed

Lines changed: 184 additions & 63 deletions

File tree

app/admin/items.rb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -283,26 +283,29 @@ def apply_tmp_params(item, tmp)
283283

284284
when "Select"
285285
sub_hash = {}
286-
(1..10).each do |i|
287-
desc = params["option_description_#{i}"]
288-
val = params["option_value_#{i}"]
286+
287+
# Отримаємо масив select_options з параметрів
288+
select_options = params[:select_options] || []
289+
290+
select_options.each do |pair|
291+
desc = pair["description"]
292+
val = pair["value"]
289293
next if desc.blank? || val.blank?
290-
294+
291295
sub_hash[desc] = val
292296
end
293-
294-
value_label = params[:value_label].to_s.strip
295-
296-
if param_name.blank? || value_label.blank? || sub_hash.empty?
297-
flash[:error] = "Select Name, Value Label and at least one option are required"
297+
298+
value_label = params[:value_label]
299+
if value_label.blank?
300+
flash[:error] = "Value Label is required for Select parameter"
298301
return redirect_back(fallback_location: edit_admin_item_path(@item))
299302
end
300-
303+
301304
store[:select][param_name] = {
302305
"options" => sub_hash,
303306
"value_label" => value_label
304307
}
305-
308+
306309
else
307310
flash[:error] = "Unknown parameter type"
308311
return redirect_back(fallback_location: edit_admin_item_path(@item))
@@ -313,6 +316,9 @@ def apply_tmp_params(item, tmp)
313316
Rails.logger.info "📤 Session after update: #{session[:tmp_params][item_key]}"
314317

315318
flash[:notice] = "Parameter '#{param_name}' added (stored in session)."
319+
Rails.logger.info "🧠 Final saved SELECT: #{store[:select][param_name].inspect}"
320+
Rails.logger.info "📤 Full session after update: #{session[:tmp_params].inspect}"
321+
316322
redirect_to @item.persisted? ? edit_admin_item_path(@item) : new_resource_path
317323
end
318324

@@ -403,33 +409,31 @@ def apply_tmp_params(item, tmp)
403409
session[:tmp_params]["new"]["calculation_formula"] = params[:calculation_formula]
404410
else
405411
@item = Item.find(params[:id])
406-
412+
407413
session[:tmp_params] ||= {}
408414
session[:tmp_params][@item.id.to_s] ||= {}
409415
session[:tmp_params][@item.id.to_s]["calculation_formula"] = params[:calculation_formula]
410-
416+
411417
@item.calculation_formula = params[:calculation_formula]
412418
end
413-
419+
414420
flash[:notice] = "Formula saved (in session)!"
415421
redirect_to params[:id] == "new" ? new_resource_path : edit_admin_item_path(params[:id])
416422
end
417-
418423

419424
member_action :clear_session, method: :post do
420425
item_key = params[:id].to_s
421426
if session[:tmp_params].present?
422427
Rails.logger.info "⚠️ TRYING TO DELETE session[:tmp_params][#{item_key}]"
423428
Rails.logger.info "🔍 BEFORE DELETE: #{session[:tmp_params][item_key].inspect}"
424-
429+
425430
session[:tmp_params].delete(item_key)
426-
431+
427432
Rails.logger.info "🧹 DELETED! AFTER: #{session[:tmp_params].inspect}"
428433
else
429434
Rails.logger.warn "⚠️ tmp_params session is empty, nothing to delete"
430435
end
431436

432437
head :ok
433438
end
434-
435439
end

app/assets/javascripts/active_admin.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ document.addEventListener("DOMContentLoaded", function () {
111111
})
112112
.then((response) => {
113113
if (response.ok) {
114-
window.location.href = "/admin/items/new";
114+
window.location.href = "/admin/items/new";
115115
} else {
116116
alert("❌ Session clear failed");
117117
}
@@ -141,3 +141,56 @@ document.addEventListener("DOMContentLoaded", function () {
141141
}
142142
});
143143

144+
145+
document.addEventListener("DOMContentLoaded", () => {
146+
const wrapper = document.getElementById("select-options-wrapper");
147+
const addBtn = document.getElementById("add-option-button");
148+
149+
const createOptionRow = () => {
150+
const row = document.createElement("div");
151+
row.className = "select-option-row";
152+
153+
row.innerHTML = `
154+
<div class="select-field">
155+
<label>Description</label>
156+
<input type="text" name="select_options[][description]" />
157+
</div>
158+
<div class="select-field">
159+
<label>Value</label>
160+
<input type="text" name="select_options[][value]" />
161+
</div>
162+
<button type="button" class="delete-option-button button light">Delete</button>
163+
`;
164+
165+
// кнопка Delete
166+
row.querySelector(".delete-option-button").addEventListener("click", () => {
167+
if (wrapper.querySelectorAll(".select-option-row").length > 2) {
168+
row.remove();
169+
} else {
170+
alert("At least 2 options are required.");
171+
}
172+
});
173+
174+
return row;
175+
};
176+
177+
// Додати новий рядок
178+
addBtn.addEventListener("click", () => {
179+
const newRow = createOptionRow();
180+
wrapper.appendChild(newRow);
181+
});
182+
183+
// Додати обробники на існуючі 2 рядки
184+
wrapper.querySelectorAll(".select-option-row").forEach((row) => {
185+
const delBtn = row.querySelector(".delete-option-button");
186+
delBtn.addEventListener("click", () => {
187+
if (wrapper.querySelectorAll(".select-option-row").length > 2) {
188+
row.remove();
189+
} else {
190+
alert("At least 2 options are required.");
191+
}
192+
});
193+
});
194+
});
195+
196+

app/assets/stylesheets/active_admin.scss

Lines changed: 79 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,32 @@
1616
//
1717
// .status_tag { background: #6090DB; }
1818
.parameter-table {
19-
width: 100%;
20-
border-collapse: collapse;
21-
22-
th, td {
23-
padding: 8px 12px;
24-
border: 1px solid #ddd;
25-
text-align: left;
26-
}
27-
28-
th:nth-child(1), td:nth-child(1) {
29-
width: 40%;
30-
}
31-
32-
th:nth-child(2), td:nth-child(2) {
33-
width: 40%;
34-
}
35-
36-
th:nth-child(3), td:nth-child(3) {
37-
width: 20%;
38-
}
19+
width: 100%;
20+
border-collapse: collapse;
21+
22+
th,
23+
td {
24+
padding: 8px 12px;
25+
border: 1px solid #ddd;
26+
text-align: left;
27+
}
28+
29+
th:nth-child(1),
30+
td:nth-child(1) {
31+
width: 40%;
3932
}
4033

34+
th:nth-child(2),
35+
td:nth-child(2) {
36+
width: 40%;
37+
}
38+
39+
th:nth-child(3),
40+
td:nth-child(3) {
41+
width: 20%;
42+
}
43+
}
44+
4145
.parameter-panel {
4246
padding: 20px;
4347
border: 1px solid #ccc;
@@ -69,7 +73,7 @@
6973

7074
input {
7175
width: 100%;
72-
max-width: 400px;
76+
max-width: 503px;
7377
}
7478
}
7579

@@ -90,32 +94,78 @@
9094
}
9195
}
9296
}
97+
9398
.select-option-row {
9499
display: flex;
100+
flex-wrap: wrap;
95101
align-items: flex-end;
96102
gap: 16px;
97-
margin-bottom: 12px;
103+
margin-bottom: 16px;
104+
border-radius: 8px;
98105

99106
.select-field {
100107
display: flex;
101108
flex-direction: column;
102-
flex: 1;
109+
flex: 1 1 250px;
110+
min-width: 200px;
103111

104112
label {
105-
margin-bottom: 4px;
106-
font-weight: 600;
113+
margin-bottom: 6px;
114+
color: #444;
107115
}
108116

109117
input[type="text"] {
110-
width: 100%;
111-
max-width: 280px;
118+
padding: 8px 10px;
119+
border-radius: 4px;
120+
border: 1px solid #ccc;
121+
background-color: #fff;
122+
transition: border-color 0.2s;
123+
124+
&:focus {
125+
border-color: #888;
126+
outline: none;
127+
}
112128
}
113129
}
114130
}
115131

116-
.add-param-link-wrapper{
132+
.delete-option-button {
133+
padding: 8px 14px;
134+
font-weight: bold;
135+
border-radius: 6px;
136+
border: 1px solid #ccc;
137+
background: linear-gradient(#ffffff, #f7f7f7) !important;
138+
text-shadow: none !important;
139+
background: #fff;
140+
color: #444;
141+
cursor: pointer;
142+
transition: background 0.2s, color 0.2s;
143+
144+
&:hover {
145+
background-color: #eee;
146+
color: #000;
147+
}
148+
}
149+
150+
.add-param-link-wrapper {
117151
margin-bottom: 10px;
118152
}
119153

154+
.cancel-button {
155+
margin-left: 10px;
156+
background: linear-gradient(#ffffff, #f7f7f7) !important;
157+
border: 1px solid #ccc !important;
158+
color: #444 !important;
159+
padding: 8px 16px !important;
160+
text-shadow: none !important;
161+
border-radius: 8px !important;
162+
}
163+
164+
.cancel-button:hover {
165+
background: linear-gradient(#f0f0f0, #e6e6e6) !important;
166+
border-color: #aaa !important;
167+
color: #222 !important;
168+
}
169+
120170

121-
@import "active_admin/forms/select";
171+
@import "active_admin/forms/select";

app/models/item.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Item < ApplicationRecord
55
validates :name, uniqueness: { scope: :category_id, message: "Item name must be unique within category" }
66
validate :category_must_be_active
77
validate :fixed_parameters_values_must_be_numeric
8-
validate :pricing_options_values_must_be_numeric
8+
# validate :pricing_options_values_must_be_numeric
99
validates :calculation_formula, presence: true, if: :requires_calculation_formula?
1010

1111
def self.ransackable_attributes(_auth_object = nil)

app/views/admin/items/_parameters.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
<% end %>
8484
</h4>
8585

86-
<p>Value Label: <%= value_label %></p>
86+
<p>Value label: <%= value_label %></p>
8787

8888
<table class="parameter-table">
8989
<thead>

app/views/admin/items/new_formula.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<%= hidden_field_tag :calculation_formula, '', id: 'formulaInput' %>
2929
<div class="form-actions" style="margin-top: 15px;">
3030
<%= submit_tag "Save Formula", class: "button" %>
31+
<% back_path = @item.persisted? ? edit_admin_item_path(@item) : new_resource_path %>
32+
<%= link_to "Cancel", back_path, class: "button cancel-button", style: "margin-left: 10px;" %>
3133
</div>
3234
<% end %>
3335
</div>

app/views/admin/items/new_parameter.html.erb

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,35 @@
4343
<%= text_field_tag :value_label, nil, class: "value-label-input" %>
4444
</div>
4545

46-
<% 10.times do |i| %>
47-
<div class="select-option-row">
48-
<div class="select-field">
49-
<%= label_tag "option_description_#{i + 1}", "Description #{i + 1}" %>
50-
<%= text_field_tag "option_description_#{i + 1}" %>
46+
<div id="select-options-wrapper">
47+
<% 2.times do %>
48+
<div class="select-option-row">
49+
<div class="select-field">
50+
<label>Description</label>
51+
<input type="text" name="select_options[][description]" />
52+
</div>
53+
<div class="select-field">
54+
<label>Value</label>
55+
<input type="text" name="select_options[][value]" />
56+
</div>
57+
<button type="button" class="delete-option-button button light">Delete</button>
5158
</div>
52-
<div class="select-field">
53-
<%= label_tag "option_value_#{i + 1}", "Value #{i + 1}" %>
54-
<%= text_field_tag "option_value_#{i + 1}" %>
55-
</div>
56-
</div>
57-
<% end %>
59+
<% end %>
60+
</div>
61+
62+
<div class="form-actions">
63+
<button type="button" id="add-option-button" class="button">Add Option</button>
64+
</div>
65+
5866
</div>
5967

6068
<div class="form-actions">
61-
<% tmp = session[:tmp_params] && session[:tmp_params]["new"] %>
62-
<%= submit_tag "Add Parameter" %>
69+
<% item_key = resource.persisted? ? resource.id.to_s : "new" %>
70+
<% back_path = resource.persisted? ? edit_admin_item_path(resource) : new_resource_path %>
71+
72+
<%= submit_tag "Add Parameter", class: "button" %>
73+
<%= link_to "Cancel", back_path, class: "button cancel-button" %>
6374
</div>
75+
6476
<% end %>
6577
</div>

0 commit comments

Comments
 (0)