Skip to content

Commit

Permalink
Refactor campaign/template preview functions and component.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Jul 9, 2022
1 parent 2dcac57 commit 5a5caca
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 31 deletions.
82 changes: 53 additions & 29 deletions cmd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,58 +61,82 @@ func handleGetTemplates(c echo.Context) error {
// handlePreviewTemplate renders the HTML preview of a template.
func handlePreviewTemplate(c echo.Context) error {
var (
app = c.Get("app").(*App)

app = c.Get("app").(*App)
id, _ = strconv.Atoi(c.Param("id"))
body = c.FormValue("body")
typ = c.FormValue("typ")
)

if typ == "" {
typ = models.TemplateTypeCampaign
tpl := models.Template{
Type: c.FormValue("template_type"),
Body: c.FormValue("body"),
}

if body != "" {
if typ == models.TemplateTypeCampaign && !regexpTplTag.MatchString(body) {
// Body is posted.
if tpl.Body != "" {
if tpl.Type == "" {
tpl.Type = models.TemplateTypeCampaign
}

if tpl.Type == models.TemplateTypeCampaign && !regexpTplTag.MatchString(tpl.Body) {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("templates.placeholderHelp", "placeholder", tplTag))
}
} else {
// There is no body. Fetch the template.
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}

tpl, err := app.core.GetTemplate(id, false)
t, err := app.core.GetTemplate(id, false)
if err != nil {
return err
}

body = tpl.Body
tpl = t
}

// Compile the template.
camp := models.Campaign{
UUID: dummyUUID,
Name: app.i18n.T("templates.dummyName"),
Subject: app.i18n.T("templates.dummySubject"),
FromEmail: "[email protected]",
TemplateBody: body,
Body: dummyTpl,
}
// Compile the campaign template.
var out []byte
if tpl.Type == models.TemplateTypeCampaign {
camp := models.Campaign{
UUID: dummyUUID,
Name: app.i18n.T("templates.dummyName"),
Subject: app.i18n.T("templates.dummySubject"),
FromEmail: "[email protected]",
TemplateBody: tpl.Body,
Body: dummyTpl,
}

if err := camp.CompileTemplate(app.manager.TemplateFuncs(&camp)); err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("templates.errorCompiling", "error", err.Error()))
}
if err := camp.CompileTemplate(app.manager.TemplateFuncs(&camp)); err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("templates.errorCompiling", "error", err.Error()))
}

// Render the message body.
msg, err := app.manager.NewCampaignMessage(&camp, dummySubscriber)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("templates.errorRendering", "error", err.Error()))
// Render the message body.
msg, err := app.manager.NewCampaignMessage(&camp, dummySubscriber)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("templates.errorRendering", "error", err.Error()))
}
out = msg.Body()
} else {
// Compile transactional template.
if err := tpl.Compile(app.manager.GenericTemplateFuncs()); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

m := models.TxMessage{
Subject: tpl.Subject,
}

// Render the message.
if err := m.Render(dummySubscriber, &tpl); err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("globals.messages.errorFetching", "name"))
}
out = m.Body
}

return c.HTML(http.StatusOK, string(msg.Body()))
return c.HTML(http.StatusOK, string(out))
}

// handleCreateTemplate handles template creation.
Expand Down
20 changes: 19 additions & 1 deletion frontend/cypress/integration/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Templates', () => {
});


it('Previews templates', () => {
it('Previews campaign templates', () => {
// Edited one sould have a bare body.
cy.get('tbody [data-cy=btn-preview').eq(0).click();
cy.wait(500);
Expand All @@ -61,6 +61,24 @@ describe('Templates', () => {
cy.get('.modal-card-foot button').click();
});

it('Previews tx templates', () => {
// Edited one sould have a bare body.
cy.get('tbody tr:nth-child(2) [data-cy=btn-preview').click();
cy.wait(500);
cy.get('.modal-card-body iframe').iframe(() => {
cy.get('strong').first().contains('Order number');
});
cy.get('.modal-card-foot button').click();

// Cloned one should have the full template.
cy.get('tbody tr:nth-child(4) [data-cy=btn-preview').click();
cy.wait(500);
cy.get('.modal-card-body iframe').iframe(() => {
cy.get('strong').first().contains('Order number');
});
cy.get('.modal-card-foot button').click();
});

it('Sets default', () => {
cy.get('tbody td.actions').eq(2).find('[data-cy=btn-set-default]').click();
cy.get('.modal button.is-primary').click();
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/components/CampaignPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<form v-if="body" method="post" :action="previewURL" target="iframe" ref="form">
<input type="hidden" name="template_id" :value="templateId" />
<input type="hidden" name="content_type" :value="contentType" />
<input type="hidden" name="template_type" :value="templateType" />
<input type="hidden" name="body" :value="body" />
</form>

Expand Down Expand Up @@ -43,6 +44,10 @@ export default {
// campaign | template.
type: String,
// campaign | tx.
templateType: String,
body: String,
contentType: String,
templateId: {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/views/TemplateForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<campaign-preview v-if="previewItem"
type='template'
:title="previewItem.name"
:templateType="previewItem.type"
:body="form.body"
@close="closePreview"></campaign-preview>
</section>
Expand Down Expand Up @@ -99,7 +100,7 @@ export default Vue.extend({
methods: {
previewTemplate() {
this.previewItem = this.data;
this.previewItem = this.form;
},
closePreview() {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/views/Templates.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<campaign-preview v-if="previewItem"
type='template'
:id="previewItem.id"
:templateType="previewItem.type"
:title="previewItem.name"
@close="closePreview"></campaign-preview>
</section>
Expand Down

0 comments on commit 5a5caca

Please sign in to comment.