From aedb12ee03cfba17b0bb130c36dc22c205f5f2df Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kasia=20Jarmo=C5=82kowicz?=
Date: Mon, 21 May 2018 21:24:13 +0200
Subject: [PATCH] Create campaign from template
---
app/models/campaign_template.rb | 7 +++++
.../campaign_templates/index.html.erb | 4 +++
client/js/campaign_templates/new/wizard.js | 31 +++++++++++++++++++
3 files changed, 42 insertions(+)
diff --git a/app/models/campaign_template.rb b/app/models/campaign_template.rb
index 099dea01..21d0bcd6 100644
--- a/app/models/campaign_template.rb
+++ b/app/models/campaign_template.rb
@@ -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
diff --git a/app/views/nonprofits/campaign_templates/index.html.erb b/app/views/nonprofits/campaign_templates/index.html.erb
index 70c16c5d..1a1da3f9 100644
--- a/app/views/nonprofits/campaign_templates/index.html.erb
+++ b/app/views/nonprofits/campaign_templates/index.html.erb
@@ -7,6 +7,7 @@
<%= IncludeAsset.js '/client/js/campaign_templates/index/page.js' %>
<% end %>
@@ -52,6 +53,9 @@
Customizable attributes: <%= template.customizable_attributes_list %>
+ Create campaign from template
+
+
Delete template
diff --git a/client/js/campaign_templates/new/wizard.js b/client/js/campaign_templates/new/wizard.js
index ca29497a..130dc20b 100644
--- a/client/js/campaign_templates/new/wizard.js
+++ b/client/js/campaign_templates/new/wizard.js
@@ -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)
+ })
+})