Refactor campaign/template preview functions and component.

This commit is contained in:
Kailash Nadh 2022-07-02 17:35:31 +05:30
parent 2dcac57cba
commit 5a5caca256
5 changed files with 82 additions and 33 deletions

View file

@ -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: "dummy-campaign@listmonk.app",
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: "dummy-campaign@listmonk.app",
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()))
}
// 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
}
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()))
}
return c.HTML(http.StatusOK, string(msg.Body()))
return c.HTML(http.StatusOK, string(out))
}
// handleCreateTemplate handles template creation.

View file

@ -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);
@ -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();

View file

@ -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>
@ -43,6 +44,10 @@ export default {
// campaign | template.
type: String,
// campaign | tx.
templateType: String,
body: String,
contentType: String,
templateId: {

View file

@ -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>
@ -99,7 +100,7 @@ export default Vue.extend({
methods: {
previewTemplate() {
this.previewItem = this.data;
this.previewItem = this.form;
},
closePreview() {

View file

@ -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>