fix bugs in create plans

This commit is contained in:
Rimian Perkins
2019-10-23 15:55:06 +11:00
parent a94287434d
commit 922dee581c
13 changed files with 164 additions and 80 deletions
@@ -1,6 +1,17 @@
import computed from "ember-addons/ember-computed-decorators";
import DiscourseURL from "discourse/lib/url";
export default Ember.Controller.extend({
@computed("model.plan.isNew")
planFieldDisabled(isNew) {
return !isNew;
},
@computed("model.product.id")
productId(id) {
return id;
},
redirect(product_id) {
DiscourseURL.redirectTo(`/admin/plugins/discourse-patrons/products/${product_id}`);
},
@@ -11,8 +22,19 @@ export default Ember.Controller.extend({
},
createPlan() {
const product_id = this.get('model.plan.product');
this.get('model.plan').save().then(() => this.redirect(product_id));
// TODO: set default group name beforehand
if (this.get("model.plan.metadata.group_name") === undefined) {
this.set(
"model.plan.metadata",
{ group_name: this.get("model.groups.firstObject.name") }
);
}
this.get('model.plan').save().then(() => this.redirect(this.productId));
},
updatePlan() {
this.get('model.plan').update().then(() => this.redirect(this.productId));
}
}
});
@@ -1,34 +1,26 @@
import { popupAjaxError } from "discourse/lib/ajax-error";
export default Ember.Controller.extend({
redirect() {
this.transitionToRoute("adminPlugins.discourse-patrons.products");
},
actions: {
cancelProduct() {
this.redirect();
this.transitionToRoute("adminPlugins.discourse-patrons.products");
},
createProduct() {
// TODO: set default group name beforehand
if (this.get("model.product.metadata.group_name") === undefined) {
this.set(
"model.product.metadata",
{ group_name: this.get("model.groups.firstObject.name") }
);
}
this.get("model.product")
.save()
.then(() => this.redirect())
.then(product => {
this.transitionToRoute("adminPlugins.discourse-patrons.products.show", product.id);
})
.catch(popupAjaxError);
},
updateProduct() {
this.get("model.product")
.update()
.then(() => this.redirect())
.then(() => {
this.transitionToRoute("adminPlugins.discourse-patrons.products");
})
.catch(popupAjaxError);
}
}
@@ -2,6 +2,7 @@ import computed from "ember-addons/ember-computed-decorators";
import { ajax } from "discourse/lib/ajax";
const AdminPlan = Discourse.Model.extend({
isNew: false,
name: "",
interval: "month",
amount: 0,
@@ -13,6 +14,16 @@ const AdminPlan = Discourse.Model.extend({
return moment.unix(created).format();
},
@computed("trial_period_days")
parseTrialPeriodDays(trial_period_days) {
if(trial_period_days) {
return parseInt(0 + trial_period_days);
}
else {
return 0;
}
},
destroy() {
return ajax(`/patrons/admin/plans/${this.id}`, { method: "delete" });
},
@@ -22,12 +33,22 @@ const AdminPlan = Discourse.Model.extend({
nickname: this.nickname,
interval: this.interval,
amount: this.amount,
trial_period_days: this.trial_period_days,
trial_period_days: this.parseTrialPeriodDays,
product: this.product,
metadata: this.metadata,
};
return ajax("/patrons/admin/plans", { method: "post", data });
},
update() {
const data = {
nickname: this.nickname,
trial_period_days: this.parseTrialPeriodDays,
metadata: this.metadata,
};
return ajax(`/patrons/admin/plans/${this.id}`, { method: "patch", data });
}
});
@@ -22,7 +22,9 @@ const AdminProduct = Discourse.Model.extend({
active: this.active
};
return ajax("/patrons/admin/products", { method: "post", data });
return ajax("/patrons/admin/products", { method: "post", data }).then(product =>
AdminProduct.create(product)
);
},
update() {
@@ -8,7 +8,7 @@ export default Discourse.Route.extend({
let plan;
if(id === 'new') {
plan = AdminPlan.create({ product: product.get('id') });
plan = AdminPlan.create({ isNew: true, product: product.get('id') });
}
else {
plan = AdminPlan.find(id);
@@ -9,17 +9,15 @@
<table class="table discourse-patrons-admin">
<thead>
<th>{{i18n 'discourse_patrons.admin.products.product.name'}}</th>
<th>{{i18n 'discourse_patrons.admin.products.product.group'}}</th>
<th>{{i18n 'discourse_patrons.admin.products.product.created_at'}}</th>
<th>{{i18n 'discourse_patrons.admin.products.product.active'}}</th>
<th class="td-right">{{i18n 'discourse_patrons.admin.products.product.active'}}</th>
<th></th>
</thead>
{{#each model as |product|}}
<tr>
<td>{{product.name}}</td>
<td>{{product.metadata.group_name}}</td>
<td>{{format-date product.createdFormatted}}</td>
<td>{{product.active}}</td>
<td class="td-right">{{product.active}}</td>
<td class="td-right">
{{#link-to "adminPlugins.discourse-patrons.products.show" product.id class="btn no-text btn-icon"}}
{{d-icon "far-edit"}}
@@ -22,7 +22,7 @@
</p>
<p>
<label for="amount">{{i18n 'discourse_patrons.admin.plans.plan.amount'}}</label>
{{input type="text" name="name" value=model.plan.amount}}
{{input type="text" name="name" value=model.plan.amount disabled=planFieldDisabled}}
</p>
<p>
<label for="trial">
@@ -51,7 +51,12 @@
<div class="pull-right">
{{d-button label="cancel" action=(action "cancelPlan" model.plan.product) icon="times"}}
{{d-button label="discourse_patrons.admin.plans.operations.create" action="createPlan" icon="plus" class="btn btn-primary"}}
{{#if model.plan.isNew}}
{{d-button label="discourse_patrons.admin.plans.operations.create" action="createPlan" icon="plus" class="btn btn-primary"}}
{{else}}
{{d-button label="discourse_patrons.admin.plans.operations.update" action="updatePlan" icon="check" class="btn btn-primary"}}
{{/if}}
</div>
</section>
@@ -20,53 +20,54 @@
</p>
</form>
<h4>{{i18n 'discourse_patrons.admin.plans.title'}}</h4>
{{#unless model.product.isNew}}
<h4>{{i18n 'discourse_patrons.admin.plans.title'}}</h4>
<p>
<table class="table discourse-patrons-admin">
<thead>
<th>{{i18n 'discourse_patrons.admin.plans.plan.nickname'}}</th>
<th>{{i18n 'discourse_patrons.admin.plans.plan.interval'}}</th>
<th>{{i18n 'discourse_patrons.admin.plans.plan.created_at'}}</th>
<th>{{i18n 'discourse_patrons.admin.plans.plan.group'}}</th>
<th class="td-right">{{i18n 'discourse_patrons.admin.plans.plan.amount'}}</th>
<th class="td-right">
{{#link-to "adminPlugins.discourse-patrons.products.show.plans.show" model.product.id "new" class="btn"}}
{{i18n 'discourse_patrons.admin.plans.operations.add'}}
{{/link-to}}
</th>
</thead>
{{#each model.plans as |plan|}}
<tr>
<td>{{plan.nickname}}</td>
<td>{{plan.interval}}</td>
<td>{{format-date plan.createdFormatted}}</td>
<td>{{plan.metadata.group_name}}</td>
<td class="td-right">{{plan.amount}}</td>
<td class="td-right">
{{#link-to "adminPlugins.discourse-patrons.products.show.plans.show" model.product.id plan.id class="btn no-text btn-icon"}}
{{d-icon "far-edit"}}
{{/link-to}}
{{d-button
action=(route-action "destroyPlan")
actionParam=plan
icon="trash-alt"
class="btn-danger btn no-text btn-icon"}}
</td>
</tr>
{{/each}}
<p>
<table class="table discourse-patrons-admin">
<thead>
<th>{{i18n 'discourse_patrons.admin.plans.plan.nickname'}}</th>
<th>{{i18n 'discourse_patrons.admin.plans.plan.interval'}}</th>
<th>{{i18n 'discourse_patrons.admin.plans.plan.created_at'}}</th>
<th>{{i18n 'discourse_patrons.admin.plans.plan.group'}}</th>
<th class="td-right">{{i18n 'discourse_patrons.admin.plans.plan.amount'}}</th>
<th class="td-right">
{{#link-to "adminPlugins.discourse-patrons.products.show.plans.show" model.product.id "new" class="btn"}}
{{i18n 'discourse_patrons.admin.plans.operations.add'}}
{{/link-to}}
</th>
</thead>
{{#each model.plans as |plan|}}
<tr>
<td>{{plan.nickname}}</td>
<td>{{plan.interval}}</td>
<td>{{format-date plan.createdFormatted}}</td>
<td>{{plan.metadata.group_name}}</td>
<td class="td-right">{{plan.amount}}</td>
<td class="td-right">
{{#link-to "adminPlugins.discourse-patrons.products.show.plans.show" model.product.id plan.id class="btn no-text btn-icon"}}
{{d-icon "far-edit"}}
{{/link-to}}
{{d-button
action=(route-action "destroyPlan")
actionParam=plan
icon="trash-alt"
class="btn-danger btn no-text btn-icon"}}
<td colspan="6">
{{#unless model.plans}}
<hr>
{{i18n 'discourse_patrons.admin.products.product.plan_help'}}
{{/unless}}
</td>
</tr>
{{/each}}
<tr>
<td colspan="5">
{{#unless model.plans}}
<hr>
{{i18n 'discourse_patrons.admin.products.product.plan_help'}}
{{/unless}}
</td>
</tr>
</table>
</p>
</table>
</p>
{{/unless}}
<section>
<hr>