In this blog post I am explaining how we implement a messages page for admins to keep track of all types of system messages sent to users in the Open Event Frontend. The page shows the types of messages sent out to various users at one place and as well as additional details. It offers configuration options to control which messages get sent out as emails or notifications or both. And, the page shows when and what message should be sent via notification or mail.
To create the messages page we’ll run the following command
ember generate route admin/messages |
This will create
This command will also add this.route(‘messages’); to router.js. As admin is the parent route for messages, messages will be nested inside admin in router.js
this.route(‘admin’, function(){ this.route(‘messages’); }); |
Let’s now understand the content of each of above files.
In admin/messages.js we have used titletoken helper to set the title of the tab. Here we have created the message model and added attribute like recipient, trigger, emailMessage, notificationMessage, options and sentAt. We have returned this model from the js file to template.
import Ember from ’ember’; const { Route } = Ember; export default Route.extend({ titleToken() { return this.l10n.t(‘Messages’); }, model() { return [{ recipient: [ { name: ‘Organizer’ }, { name: ‘Speaker’ } ], trigger : ‘Title1’, emailMessage : { subject : ‘Email subject1’, message : ‘Hi, the schedule for session1 has been changed’ }, notificationMessage: { subject : ‘Notification subject1’, message : ‘Hi, the schedule for session1 has been changed’ }, option: { mail : true, notification : false, userControl : true }, sentAt: new Date() }]; } }); |
In template we have created a table and added classes like stackable and compact. Stackable class makes the table responsive and stacks all the rows for devices with smaller screen size. Compact class helps to show more number of rows at a time.
Then in the template we iterate through the model using a loop. Here we have used other semantic-ui elements like ui ordered list , ui header, ui-checkbox inside the table. For options column we have three attributes denoting how the admin wants to send the message to the user. Here we have grouped three fields using the class grouped fields .In each field we have used semantic’s ui-checkbox .In check-box we are mutating values on click by using mut helper.
<div class=“grouped fields”> <div class=“field”> {{ui-checkbox checked=message.option.mail label=(t ‘Mail’) onChange=(action (mut message.option.mail))}} </div> <div class=“field”> {{ui-checkbox checked=message.option.notification label=(t ‘Notification’) onChange=(action (mut message.option.notification))}} </div> <div class=“field”> |
We are getting date object from js and to convert it into human readable format we have used moment like {{moment-format message.sentAt ‘dddd, DD MMMM YYYY’}}
import { test } from ’ember-qunit’; import moduleFor from ‘open-event-frontend/tests/helpers/unit-helper’; moduleFor(‘route:admin/messages’, ‘Unit | Route | admin/messages’, []); test(‘it exists’, function(assert) { let route = this.subject(); assert.ok(route); }); |
Using this we can test the existence of our route. These tests are run using the command ember t.
Our message page is ready now. The admin can have a check at all the messages sent to users.
Additional Resources