|
| 1 | +this.ckan.module('canada-user-opt-in-feature', function($){ |
| 2 | + return { |
| 3 | + /* options object can be extended using data-module-* attributes */ |
| 4 | + options: { |
| 5 | + csrf_name: null, |
| 6 | + csrf_value: null, |
| 7 | + page_redirect: null, |
| 8 | + label: null, |
| 9 | + button_label: null, |
| 10 | + user_id: null, |
| 11 | + feature_key: null, |
| 12 | + feature_value: true, |
| 13 | + }, |
| 14 | + initialize: function (){ |
| 15 | + _load_user_opt_in(this); |
| 16 | + } |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +function _load_user_opt_in(CKAN_MODULE){ |
| 21 | + const _ = CKAN_MODULE._; |
| 22 | + const csrfTokenName = CKAN_MODULE.options.csrf_name; |
| 23 | + const csrfTokenValue = CKAN_MODULE.options.csrf_value; |
| 24 | + const pageRedirect = CKAN_MODULE.options.page_redirect; |
| 25 | + const label = CKAN_MODULE.options.label; |
| 26 | + const buttonLabel = CKAN_MODULE.options.button_label; |
| 27 | + const userID = CKAN_MODULE.options.user_id; |
| 28 | + const featureKey = CKAN_MODULE.options.feature_key; |
| 29 | + const featureValue = CKAN_MODULE.options.feature_value; |
| 30 | + |
| 31 | + let postData = {'id': userID}; |
| 32 | + postData[featureKey] = featureValue; |
| 33 | + postData[csrfTokenName] = csrfTokenValue; |
| 34 | + |
| 35 | + let container = $('[data-module="canada-user-opt-in-feature"][data-module-feature_key="' + featureKey + '"]'); |
| 36 | + let htmlContent = '<div class="alert alert-warning"><p>' + label + '</p><p><a role="button" class="btn btn-primary btn-small" href="javascript:void(0);">' + buttonLabel + '</a></p></div>'; |
| 37 | + |
| 38 | + function _render_failure(_consoleMessage, _message, _type){ |
| 39 | + console.warn(_consoleMessage); |
| 40 | + $(container).find('#uoif_failure_message').remove(); |
| 41 | + $(container).append('<div id="uoif_failure_message" class="alert alert-dismissible show alert-' + _type + '"><p>' + _message + '</p></div>'); |
| 42 | + } |
| 43 | + |
| 44 | + if( $(container).children('.alert').length == 0 ){ |
| 45 | + $(container).append(htmlContent); |
| 46 | + let button = $(container).find('a.btn'); |
| 47 | + if( button.length > 0 ){ |
| 48 | + $(button).off('click.optInFeature'); |
| 49 | + $(button).on('click.optInFeature', function(_event){ |
| 50 | + $(button).addClass('disabled'); |
| 51 | + $(button).attr('disabled', 'disabled'); |
| 52 | + $.ajax({ |
| 53 | + 'url': '/api/action/user_patch', |
| 54 | + 'type': 'POST', |
| 55 | + 'dataType': 'JSON', |
| 56 | + 'data': postData, |
| 57 | + 'complete': function(_data){ |
| 58 | + if( _data.responseJSON ){ // we have response JSON |
| 59 | + if( _data.responseJSON.success ){ // successful patch |
| 60 | + if( |
| 61 | + _data.responseJSON.result[featureKey] == featureValue |
| 62 | + ){ |
| 63 | + if( pageRedirect && pageRedirect.startsWith('#') ){ |
| 64 | + window.location.hash = pageRedirect.replace('#', ''); |
| 65 | + window.location.reload(); |
| 66 | + return; |
| 67 | + }else if( pageRedirect ){ |
| 68 | + window.location.href = pageRedirect; |
| 69 | + return; |
| 70 | + } |
| 71 | + window.location.reload(); |
| 72 | + return; |
| 73 | + }else{ // unexpected return dict |
| 74 | + _render_failure('Opt-in Feature — Unexpected response data.', _('Failed to change user settings'), 'danger'); |
| 75 | + console.warn(_data.responseJSON); |
| 76 | + } |
| 77 | + }else{ // validation error |
| 78 | + _render_failure('Opt-in Feature — ValidationError exception was raised.', _('Failed to change user settings'), 'danger'); |
| 79 | + console.warn(_data.responseJSON); |
| 80 | + } |
| 81 | + }else{ // fully flopped ajax request |
| 82 | + _render_failure('Opt-in Feature — Failed to gather a JSON response.', _('Failed to change user settings'), 'danger'); |
| 83 | + } |
| 84 | + } |
| 85 | + }); |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments