Create campaign from template

This commit is contained in:
Kasia Jarmołkowicz 2018-05-21 21:24:13 +02:00 committed by Eric Schultz
parent 2db5e72bcb
commit aedb12ee03
3 changed files with 42 additions and 0 deletions

View file

@ -94,4 +94,11 @@ class CampaignTemplate < ActiveRecord::Base
# self.total_supporters = 1
# self.published = false if self.published.nil?
end
def create_campaign_params
excluded = %w(
id template_name created_at updated_at
)
attributes.except!(*excluded)
end
end

View file

@ -7,6 +7,7 @@
<script>
app.current_nonprofit_user = "<%= current_nonprofit_user? %>"
app.nonprofit_id = <%= @nonprofit.id %>
app.profile_id = '<%= raw current_user.profile.id %>'
</script>
<%= IncludeAsset.js '/client/js/campaign_templates/index/page.js' %>
<% end %>
@ -52,6 +53,9 @@
<small>Customizable attributes: <%= template.customizable_attributes_list %></small>
</p>
<a class="button">Create campaign from template</a>
<!--= on 'click' (create_campaign '<%= raw(template.create_campaign_params.to_json) %>') -->
<a class="button red">Delete template</a>
<!--= on 'click' (delete_template <%= template.id %>) -->
</td>

View file

@ -72,3 +72,34 @@ appl.def('delete_template', function(id) {
})
}
})
appl.def('create_campaign', function(campaign_params) {
appl.def('loading', true)
var url = '/nonprofits/' + app.nonprofit_id + '/campaigns'
var params = new Object
params.campaign = JSON.parse(campaign_params)
params.campaign.profile_id = app.profile_id
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest()
req.open("POST", url)
req.setRequestHeader('X-CSRF-Token', window._csrf)
req.setRequestHeader('Content-Type', 'application/json')
req.send(JSON.stringify(params))
req.onload = function(ev) {
if(req.status === 200) resolve(req)
else reject(req)
}
}).then(function(req) {
appl.def('loading', false)
appl.notify('Redirecting you to your campaign…')
var campaign_id = JSON.parse(req.response).id
appl.redirect(url + '/' + campaign_id)
})
.catch(function(req) {
appl.def('loading', false)
appl.notify(req.responseText)
})
})