Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Commit

Permalink
ro31337#309 Improve select locale menu action (ro31337#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
ro31337 authored Nov 12, 2016
1 parent 1174fe9 commit fba0bfb
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 84 deletions.
18 changes: 13 additions & 5 deletions src/actions/menu/select-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import CompositeResponse from '../../responses/composite-response';
import SelectLocaleResponse from '../../responses/select-locale-response';
import TextResponse from '../../responses/text-response';
import RedirectResponse from '../../responses/redirect-response';
import If from '../../responses/if-response';
import In from '../../conditions/in';
import locales from '../../validations/supported-locales';

/**
* Select locale menu action.
*
Expand Down Expand Up @@ -33,7 +37,7 @@ export default class SelectLocale extends Action {
.add(new TextResponse({ message: 'Select your language:' }))
.add(new OptionsResponse({
rows: [
[{ label: 'English', value: 'en' }, { label: 'Русский', value: 'ru' }],
[{ label: 'English', value: locales[0] }, { label: 'Русский', value: locales[1] }],
],
}));
}
Expand All @@ -45,9 +49,13 @@ export default class SelectLocale extends Action {
* which contains {@link SelectLocaleResponse}, and {@link RedirectResponse}.
*/
post(value) {
return new CompositeResponse()
.add(new SelectLocaleResponse({ locale: value }))
.add(new TextResponse({ message: '👌 OK!' }))
.add(new RedirectResponse({ path: 'select-user-type' }));
return new If({
condition: new In(value, locales),
ok: new CompositeResponse()
.add(new SelectLocaleResponse({ locale: value }))
.add(new TextResponse({ message: '👌 OK!' }))
.add(new RedirectResponse({ path: 'select-user-type' })),
err: this.get(),
});
}
}
28 changes: 28 additions & 0 deletions src/conditions/in.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Condition from './condition';

/**
* In Condition.
* Accepts value and array. Returns true when array includes value.
*
* @author Roman Pushkin ([email protected])
* @date 2016-11-11
* @extends {Condition}
* @version 1.1
* @since 0.1.0
*/
export default class In extends Condition {
constructor(value, arr) {
super({ type: 'in' });
this.value = value;
this.arr = arr;
}

/**
* Entry point for condition.
*
* @returns {boolean} result - `true` if array includes value, `false` otherwise.
*/
call() {
return this.arr.includes(this.value);
}
}
5 changes: 2 additions & 3 deletions src/conditions/not-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Condition from './condition';

/**
* Not In Condition.
* Accepts value and array. Returns true if value is not in array.
* Accepts value and array. Returns true if array doesn't include value.
*
* @author Roman Pushkin ([email protected])
* @date 2016-09-11
Expand All @@ -20,8 +20,7 @@ export default class NotIn extends Condition {
/**
* Entry point for condition.
*
* @returns {boolean} result - returns `true` if actual equals expected, otherwise
* returns `false`.
* @returns {boolean} result - `true` if array doesn't include value, `false` otherwise.
*/
call() {
return !this.arr.includes(this.value);
Expand Down
5 changes: 3 additions & 2 deletions src/response-handlers/if-response-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ export default class IfResponseHandler extends
call(onResult) {
const user = this.user;
const r = this.response;
const api = this.api;

if (r.condition.call()) {
const response = r.ok;
const handler = ResponseHandlerFactory.getHandler({ response, user });
const handler = ResponseHandlerFactory.getHandler({ response, user, api });
handler.call(onResult);
} else if (r.err) {
const response = r.err;
const handler = ResponseHandlerFactory.getHandler({ response, user });
const handler = ResponseHandlerFactory.getHandler({ response, user, api });
handler.call(onResult);
} else {
onResult();
Expand Down
3 changes: 1 addition & 2 deletions src/responses/select-locale-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import UserStateResponse from './user-state-response';
import objectAssign from 'object-assign';
import { mix } from 'mixwith';
import checkNotNull from '../validations/check-not-null';
import checkLocale from '../validations/check-locale';
/**
* Select locale response. Used to return response that contains
* locale (`en`, `ru`, etc..) that should be stored in user state.
Expand All @@ -16,7 +15,7 @@ import checkLocale from '../validations/check-locale';
* @since 0.1.0
*/
export default class SelectLocaleResponse extends
mix(UserStateResponse).with(checkNotNull('locale'), checkLocale) {
mix(UserStateResponse).with(checkNotNull('locale')) {
/**
* Constructor.
*
Expand Down
34 changes: 0 additions & 34 deletions src/validations/check-locale.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/validations/supported-locales.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @typedef SupportedLocales
* @desc Hash set that represents the list of currently supported locales:
* @desc Array set that represents the list of currently supported locales:
* - `en` - English locale
* - `ru` - Russian locale
* @extends {Set}
Expand All @@ -10,8 +10,8 @@
* @since 0.1.0
* @example
* import SupportedLocales from './supported-locales';
* if (!SupportedLocales.has('cn')) {
* if (!SupportedLocales.includes('cn')) {
* console.log('locale "cn" is not supported yet');
* }
*/
export default new Set(['en', 'ru']);
export default ['en', 'ru'];
22 changes: 22 additions & 0 deletions test/conditions/in-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable no-new */
import test from 'ava';
import In from '../../src/conditions/in';

test('can be constructed with default parameters', t => {
const c1 = new In(2, [2, 4, 6]);
t.is(c1.type, 'in');
});

test('should be truthy when element in', t => {
const c1 = new In(1, [1, 4, 6]);
const c2 = new In('one', ['one', 'four', 'six']);
t.truthy(c1.call());
t.truthy(c2.call());
});

test('should be falsy when element not in', t => {
const c1 = new In(1, [2, 4, 6]);
const c2 = new In('one', ['two', 'four', 'six']);
t.falsy(c1.call());
t.falsy(c2.call());
});
26 changes: 17 additions & 9 deletions test/menu/select-locale-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,32 @@ test('can be constructed with default parameters', t => {
t.pass();
});

test('should return composite response on get', t => {
const action = new SelectLocale({ i18n: {}, user });
const response = action.get();
const assertGet = (t, response) => {
t.is(response.type, 'composite');
t.is(response.responses[0].type, 'text');
t.is(response.responses[0].message, 'Select your language:');
t.is(response.responses[1].type, 'options');
t.is(response.responses[1].rows[0][0].value, 'en');
t.is(response.responses[1].rows[0][1].value, 'ru');
};

test('should return composite response on get', t => {
const action = new SelectLocale({ i18n: {}, user });
assertGet(t, action.get());
});

test('should return composite response on post', t => {
const action = new SelectLocale({ i18n: {}, user });
const response = action.post('en');
t.is(response.type, 'composite');
t.is(response.responses[0].type, 'user-state');
t.is(response.responses[1].type, 'text');
t.is(response.responses[1].message, '👌 OK!');
t.is(response.responses[2].type, 'redirect');
t.is(response.responses[2].path, 'select-user-type');
t.is(response.type, 'if');
t.is(response.condition.type, 'in');
t.deepEqual(response.condition.arr, ['en', 'ru']);
t.is(response.condition.value, 'en');
t.is(response.ok.type, 'composite');
t.is(response.ok.responses[0].type, 'user-state');
t.is(response.ok.responses[1].type, 'text');
t.is(response.ok.responses[1].message, '👌 OK!');
t.is(response.ok.responses[2].type, 'redirect');
t.is(response.ok.responses[2].path, 'select-user-type');
assertGet(t, response.err);
});
26 changes: 0 additions & 26 deletions test/validations/check-locale-test.js

This file was deleted.

0 comments on commit fba0bfb

Please sign in to comment.