Skip to content

Commit

Permalink
Schedule Criteria: Fix wrong delete button and delete flow (#2774)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgbaybay authored Oct 21, 2024
1 parent d5a6194 commit 2e1280c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
30 changes: 16 additions & 14 deletions ui/src/core/xibo-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2015,13 +2015,14 @@ const configureCriteriaFields = function(dialog) {

// Existing criteria?
const existingCriteria = $fields.data('criteria');
if (existingCriteria && existingCriteria.length >= 0) {
if (existingCriteria && existingCriteria.length > 0) {
// Yes there are existing criteria
// Go through each one and add a field row to the form.
let i = 0;
$.each(existingCriteria, function(index, element) {
i++;
element.isAdd = false;
// Only the first element should have the 'Add' btn functionality
element.isAdd = i === 1;
element.i = i;
const $newField = $(templateScheduleCriteriaFields(element));
$fields.append($newField);
Expand All @@ -2036,31 +2037,32 @@ const configureCriteriaFields = function(dialog) {
// Update metrics and value fields based on the selected type and metric
updateMetricsField($newField, element.type, element.metric, element.value);
});
} else {
// If no existing criterion, add an empty field at top
const $newRow = $(templateScheduleCriteriaFields({
isAdd: true,
}));
const $newTypeSelect = $newRow.find('select[name="criteria_type[]"]');

// Populate type dropdown based on scheduleCriteria
populateTypeDropdown($newTypeSelect);
$fields.append($newRow);
}

// Add a row at the end for configuring a new criterion
const $newRow = $(templateScheduleCriteriaFields({
isAdd: true,
}));
const $newTypeSelect = $newRow.find('select[name="criteria_type[]"]');

// populate type dropdown based on scheduleCriteria
populateTypeDropdown($newTypeSelect);
$fields.append($newRow);

// Buttons we've added should be bound
$fields.on('click', 'button', function(e) {
e.preventDefault();
const $button = $(this);
if ($button.data('isAdd')) {
const newField = $(templateScheduleCriteriaFields({ isAdd: true }));
// Only the first element should have the 'Add' btn functionality
const newField = $(templateScheduleCriteriaFields({ isAdd: false }));
$fields.append(newField);

// Populate the type field for the new row
const $newTypeSelect = newField.find('select[name="criteria_type[]"]');
populateTypeDropdown($newTypeSelect);

$button.data('isAdd', false);
$button.data('isAdd', true);
} else {
$button.closest('.form-group').remove();
}
Expand Down
2 changes: 1 addition & 1 deletion views/schedule-form-templates.twig
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
</label>
</div>
<div class="col-sm-2">
<button class="btn btn-white" data-is-add="{{isAdd}}"><i class="fa {{#if isAdd}}fa-plus{{else}}fa-times{{/if}}"></i></button>
<button class="btn btn-white" data-is-add="{{isAdd}}"><i class="fa {{#if isAdd}}fa-plus{{else}}fa-minus{{/if}}"></i></button>
</div>
</div>
</script>
Expand Down

0 comments on commit 2e1280c

Please sign in to comment.