Avoid code duplication for label subscription status on label partial

parent e2865c23
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
const $btn = $(event.currentTarget); const $btn = $(event.currentTarget);
const $span = $btn.find('span'); const $span = $btn.find('span');
const url = $btn.attr('data-url'); const url = $btn.attr('data-url');
const status = $btn.attr('data-status'); const oldStatus = $btn.attr('data-status');
$btn.addClass('disabled'); $btn.addClass('disabled');
$span.toggleClass('hidden'); $span.toggleClass('hidden');
...@@ -25,10 +25,10 @@ ...@@ -25,10 +25,10 @@
}).done(() => { }).done(() => {
let newStatus, newAction; let newStatus, newAction;
if (status === 'subscribed') { if (oldStatus === 'unsubscribed') {
[newStatus, newAction] = ['unsubscribed', 'Subscribe'];
} else {
[newStatus, newAction] = ['subscribed', 'Unsubscribe']; [newStatus, newAction] = ['subscribed', 'Unsubscribe'];
} else {
[newStatus, newAction] = ['unsubscribed', 'Subscribe'];
} }
$span.toggleClass('hidden'); $span.toggleClass('hidden');
......
...@@ -140,7 +140,7 @@ module LabelsHelper ...@@ -140,7 +140,7 @@ module LabelsHelper
end end
end end
def group_label_subscription_status(label, project) def label_subscription_status(label, project)
return 'project-level' if label.subscribed?(current_user, project) return 'project-level' if label.subscribed?(current_user, project)
return 'group-level' if label.subscribed?(current_user) return 'group-level' if label.subscribed?(current_user)
...@@ -148,16 +148,12 @@ module LabelsHelper ...@@ -148,16 +148,12 @@ module LabelsHelper
end end
def group_label_unsubscribe_path(label, project) def group_label_unsubscribe_path(label, project)
case group_label_subscription_status(label, project) case label_subscription_status(label, project)
when 'project-level' then toggle_subscription_namespace_project_label_path(@project.namespace, @project, label) when 'project-level' then toggle_subscription_namespace_project_label_path(@project.namespace, @project, label)
when 'group-level' then toggle_subscription_group_label_path(label.group, label) when 'group-level' then toggle_subscription_group_label_path(label.group, label)
end end
end end
def label_subscription_status(label, project)
label.subscribed?(current_user, project) ? 'subscribed' : 'unsubscribed'
end
def label_subscription_toggle_button_text(label, project) def label_subscription_toggle_button_text(label, project)
label.subscribed?(current_user, project) ? 'Unsubscribe' : 'Subscribe' label.subscribed?(current_user, project) ? 'Unsubscribe' : 'Subscribe'
end end
......
- label_css_id = dom_id(label) - label_css_id = dom_id(label)
- open_issues_count = label.open_issues_count(current_user) - open_issues_count = label.open_issues_count(current_user)
- open_merge_requests_count = label.open_merge_requests_count(current_user) - open_merge_requests_count = label.open_merge_requests_count(current_user)
- status = label_subscription_status(label, @project).inquiry if current_user
- subject = local_assigns[:subject] - subject = local_assigns[:subject]
%li{id: label_css_id, data: { id: label.id } } %li{id: label_css_id, data: { id: label.id } }
...@@ -21,10 +22,9 @@ ...@@ -21,10 +22,9 @@
- if current_user && defined?(@project) - if current_user && defined?(@project)
%li.label-subscription %li.label-subscription
- if label.is_a?(ProjectLabel) - if label.is_a?(ProjectLabel)
%a.js-subscribe-button.label-subscribe-button{ role: 'button', href: '#', data: { status: label_subscription_status(label, @project), url: toggle_subscription_namespace_project_label_path(@project.namespace, @project, label) } } %a.js-subscribe-button.label-subscribe-button{ role: 'button', href: '#', data: { status: status, url: toggle_subscription_namespace_project_label_path(@project.namespace, @project, label) } }
%span= label_subscription_toggle_button_text(label, @project) %span= label_subscription_toggle_button_text(label, @project)
- else - else
- status = group_label_subscription_status(label, @project).inquiry
%a.js-unsubscribe-button.label-subscribe-button{ role: 'button', href: '#', class: ('hidden' if status.unsubscribed?), data: { url: group_label_unsubscribe_path(label, @project) } } %a.js-unsubscribe-button.label-subscribe-button{ role: 'button', href: '#', class: ('hidden' if status.unsubscribed?), data: { url: group_label_unsubscribe_path(label, @project) } }
%span Unsubscribe %span Unsubscribe
%a.js-subscribe-button.label-subscribe-button{ role: 'button', href: '#', class: ('hidden' unless status.unsubscribed?), data: { url: toggle_subscription_namespace_project_label_path(@project.namespace, @project, label) } } %a.js-subscribe-button.label-subscribe-button{ role: 'button', href: '#', class: ('hidden' unless status.unsubscribed?), data: { url: toggle_subscription_namespace_project_label_path(@project.namespace, @project, label) } }
...@@ -47,12 +47,10 @@ ...@@ -47,12 +47,10 @@
- if current_user && defined?(@project) - if current_user && defined?(@project)
.label-subscription.inline .label-subscription.inline
- if label.is_a?(ProjectLabel) - if label.is_a?(ProjectLabel)
%button.js-subscribe-button.label-subscribe-button.btn.btn-default.btn-action{ type: 'button', title: label_subscription_toggle_button_text(label, @project), data: { toggle: 'tooltip', status: label_subscription_status(label, @project), url: toggle_subscription_namespace_project_label_path(@project.namespace, @project, label) } } %button.js-subscribe-button.label-subscribe-button.btn.btn-default.btn-action{ type: 'button', title: label_subscription_toggle_button_text(label, @project), data: { toggle: 'tooltip', status: status, url: toggle_subscription_namespace_project_label_path(@project.namespace, @project, label) } }
%span= label_subscription_toggle_button_text(label, @project) %span= label_subscription_toggle_button_text(label, @project)
= icon('spinner spin', class: 'label-subscribe-button-loading') = icon('spinner spin', class: 'label-subscribe-button-loading')
- else - else
- status = group_label_subscription_status(label, @project).inquiry
%button.js-unsubscribe-button.label-subscribe-button.btn.btn-default.btn-action{ type: 'button', class: ('hidden' if status.unsubscribed?), title: 'Unsubscribe', data: { toggle: 'tooltip', url: group_label_unsubscribe_path(label, @project) } } %button.js-unsubscribe-button.label-subscribe-button.btn.btn-default.btn-action{ type: 'button', class: ('hidden' if status.unsubscribed?), title: 'Unsubscribe', data: { toggle: 'tooltip', url: group_label_unsubscribe_path(label, @project) } }
%span Unsubscribe %span Unsubscribe
= icon('spinner spin', class: 'label-subscribe-button-loading') = icon('spinner spin', class: 'label-subscribe-button-loading')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment