Skip to content

front-end: Add email notification preferences #1913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/components/api-token-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default Component.extend({

didInsertElement() {
let input = this.element.querySelector('input');
if (input.focus) {
if (input && input.focus) {
input.focus();
}
},
Expand Down
19 changes: 19 additions & 0 deletions app/components/owned-crate-row.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Component from '@ember/component';
import { computed } from '@ember/object';
import { alias } from '@ember/object/computed';

export default Component.extend({
tagName: 'li',

name: alias('ownedCrate.name'),
controlId: computed('ownedCrate.id', function() {
return `${this.ownedCrate.id}-email-notifications`;
}),
emailNotifications: alias('ownedCrate.email_notifications'),

actions: {
toggleEmailNotifications() {
this.set('emailNotifications', !this.get('emailNotifications'));
},
},
});
43 changes: 42 additions & 1 deletion app/controllers/me/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Controller from '@ember/controller';
import { sort, filterBy, notEmpty } from '@ember/object/computed';
import { alias, sort, filterBy, notEmpty } from '@ember/object/computed';
import { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';

export default Controller.extend({
// eslint-disable-next-line ember/avoid-leaking-state-in-ember-objects
Expand All @@ -12,10 +13,50 @@ export default Controller.extend({

isResetting: false,

ownedCrates: alias('model.ownedCrates'),

newTokens: filterBy('model.api_tokens', 'isNew', true),
disableCreate: notEmpty('newTokens'),

emailNotificationsError: false,
emailNotificationsSuccess: false,

setAllEmailNotifications(value) {
this.get('ownedCrates').forEach(c => {
c.set('email_notifications', value);
});
},

actions: {
async saveEmailNotifications() {
try {
await ajax(`/api/v1/me/email_notifications`, {
method: 'PUT',
body: JSON.stringify(
this.get('ownedCrates').map(c => ({
id: parseInt(c.id, 10),
email_notifications: c.email_notifications,
})),
),
});
this.setProperties({
emailNotificationsError: false,
emailNotificationsSuccess: true,
});
} catch (err) {
console.error(err);
this.setProperties({
emailNotificationsError: true,
emailNotificationsSuccess: false,
});
}
},
emailNotificationsSelectAll() {
this.setAllEmailNotifications(true);
},
emailNotificationsSelectNone() {
this.setAllEmailNotifications(false);
},
startNewToken() {
this.store.createRecord('api-token', {
created_at: new Date(Date.now() + 2000),
Expand Down
6 changes: 6 additions & 0 deletions app/models/owned-crate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import DS from 'ember-data';

export default DS.Model.extend({
name: DS.attr('string'),
email_notifications: DS.attr('boolean'),
});
11 changes: 11 additions & 0 deletions app/routes/me/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@ import Route from '@ember/routing/route';
import AuthenticatedRoute from '../../mixins/authenticated-route';

export default Route.extend(AuthenticatedRoute, {
actions: {
willTransition: function() {
this.controller
.setProperties({
emailNotificationsSuccess: false,
emailNotificationsError: false,
})
.clear();
},
},
model() {
return {
user: this.get('session.currentUser'),
ownedCrates: this.get('session.ownedCrates'),
api_tokens: this.store.findAll('api-token'),
};
},
Expand Down
5 changes: 5 additions & 0 deletions app/services/session.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { A } from '@ember/array';
import Service, { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';

Expand All @@ -7,6 +8,7 @@ export default Service.extend({
isLoggedIn: false,
currentUser: null,
currentUserDetected: false,
ownedCrates: A(),

store: service(),
router: service(),
Expand Down Expand Up @@ -66,6 +68,9 @@ export default Service.extend({
fetchUser() {
return ajax('/api/v1/me').then(response => {
this.set('currentUser', this.store.push(this.store.normalize('user', response.user)));
this.ownedCrates.pushObjects(
response.owned_crates.map(c => this.store.push(this.store.normalize('owned-crate', c))),
);
});
},

Expand Down
120 changes: 120 additions & 0 deletions app/styles/me.scss
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,126 @@
}
}

#me-email-notifications {
border-bottom: 5px solid $gray-border;
padding-bottom: 20px;
margin-bottom: 20px;
@include display-flex;
@include flex-direction(column);

.row {
@include display-flex;
@include flex-direction(row);

.error, .success {
border-top-width: 0px;
font-weight: bold;

padding: 0px 10px 10px 20px;
}

.error {
color: rgb(216, 0, 41);
}

.success {
color: green;
}
}

.button-container {
margin-right: 1rem;
}

ul {
padding: 0;
@include flex-grow(1);

li {
background-color: #fff;
display: block;
position: relative;
border: 1px solid #d5d3cb;
}

label {
padding: 20px 30px;
width: 100%;
display: block;
text-align: left;
font-weight: bold;
cursor: pointer;
position: relative;
z-index: 2;
transition: color 200ms ease-in;
overflow: hidden;

&:before {
width: 100%;
height: 10px;
border-radius: 50%;
content: '';
background-color: $main-bg-dark;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) scale3d(1, 1, 1);
opacity: 0;
z-index: -1;
}

&:after {
width: 32px;
height: 32px;
content: '';
border: 2px solid #d5d3cb;
border-radius: 50%;
z-index: 2;
position: absolute;
right: 30px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
}
}

input:checked ~ label {
&:before {
transform: translate(-50%, -50%) scale3d(56, 56, 1);
opacity: 1;
}

&:after {
background-color: #cfc487;
border-color: #cfc487;
background-image: url("data:image/svg+xml,%3Csvg width='32' height='32' viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M5.414 11L4 12.414l5.414 5.414L20.828 6.414 19.414 5l-10 10z' fill='%23383838' fill-rule='nonzero'/%3E%3C/svg%3E ");
background-repeat: no-repeat;
background-position: 3px 4px;
}
}

input {
width: 32px;
height: 32px;
order: 1;
z-index: 2;
position: absolute;
right: 30px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
visibility: hidden;
}
}

.right {
@include flex(2);
@include display-flex;
@include justify-content(flex-end);
@include align-self(center);
}
}

#me-api {
@media only screen and (max-width: 350px) {
.api { display: none; }
Expand Down
11 changes: 11 additions & 0 deletions app/templates/components/owned-crate-row.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{input
type="checkbox"
name=controlId
id=controlId
checked=emailNotifications
change=(action "toggleEmailNotifications")
class="form-control"
}}
<label for="{{ controlId }}">
{{ name }}
</label>
43 changes: 43 additions & 0 deletions app/templates/me/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,49 @@
{{email-input type='email' value=model.user.email user=model.user}}
</div>

<form id='me-email-notifications' {{ action 'saveEmailNotifications' on='submit' }} >
<h2>Email Notification Preferences</h2>

<p>
To aid detection of unauthorized crate changes, we email you each time a new version of a crate you own is pushed.
By receiving and reading these emails, you help protect the Rust ecosystem. You may also choose to turn these
emails off for any of your crates listed below.
</p>

<div class='row'>
<div class='button-container'>
<button type='button' class='yellow-button small' {{action 'emailNotificationsSelectAll'}}>Select All</button>
</div>
<div class='button-container'>
<button type='button' class='yellow-button small' {{action 'emailNotificationsSelectNone'}}>Deselect All</button>
</div>
</div>

<div class='row'>
<ul>
{{#each ownedCrates as |ownedCrate|}}
{{owned-crate-row ownedCrate=ownedCrate}}
{{/each}}
</ul>
</div>

<div class='row'>
{{#if emailNotificationsError}}
<div class='error'>
An error occurred while saving your email preferences.
</div>
{{/if}}
{{#if emailNotificationsSuccess}}
<div class='success'>
Your email notification preferences have been updated!
</div>
{{/if}}
<div class='right'>
<button type='submit' class='yellow-button'>Update</button>
</div>
</div>
</form>

<div id='me-api'>
<div class='me-subheading'>
<h2>API Access</h2>
Expand Down