JHipster - JhiAlertService - How to decide how long alerts are displayed? - jhipster

I have generated a JHipster project with version 4.9.0.
I am using the JhiAlertService to display various messages to users.
I would like to know how to send in parameters to indicate how long the messages are displayed.

JhiAlertService comes from the ng-jhipster library.
You can view the code of alert.service.ts to see the available parameters.
For customizing the timeout, use the addAlert method and pass it a JhiAlert object containing a timeout parameter:
this.alertService.addAlert({type: 'success', msg: 'A short message', timeout: 1000}, []);
Note that if you are using the i18n option, the msg field is the name of the i18n JSON key, and the params field should contain any information that is interpolated into the message. So for displaying an alert when the "Admin" user is saved successfully, you would use:
this.alertService.addAlert({type: 'success', msg: 'userManagement.updated', params: { param: 'admin' }, timeout: 1000}, []);

Related

Send Shipping Address details with Stripe Custom Checkout

I have a stripe checkout that's using the now unsupported "data-shipping-address="true"" parameter. This works as expected when using the simple stripe checkout with the following code:
<script
src="https://checkout.stripe.com/checkout.js"
class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-amount="1000"
data-currency="gbp"
data-allow-remember-me="true"
data-shipping-address="true"
data-billing-address="true"
data-label="Proceed to payment details"
data-image=""
data-description="TrillShirts">
</script>
but if I call it in a .js file I can't get it to post the data taken from the Shipping Details. Here's the code I have:
var handler = StripeCheckout.configure({
key: 'pk_test_JfqHIgPSCG2oWOsJ54PWS0Nl',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
billingAddress: 'true',
shippingAddress: 'true',
token: function(token) {
console.log(token.id);
console.log(token.email);
// here I try to find the shippingAddress using cosole.log
console.log(token.shippingAddressLine1);
console.log(token.stripeShippingAddressLine1);
// When I eventually have the shipping address, I will insert it in the same way as below
$(".stripeToken").val(token.id);
$(".stripeEmail").val(token.email);
$(".stripe").submit();
}
});
document.getElementById('pay').addEventListener('click', function(e) {
// Open Checkout with further options:
handler.open({
name: 'TrillShirts',
description: 'Childcatcher Tee',
currency: 'gbp',
amount: 2000
});
});
});
Both of the following return undefined in the console, so I can't figure out the format that the Shipping Details come back in.
console.log(token.shippingAddressLine1);
console.log(token.stripeShippingAddressLine1);
Once I know the format and have a successful console log I can use jQuery to insert the data into the form before submit.
Can anyone help?
With Custom Checkout, when shippingAddress is enabled, the shipping details are passed to your token callback function as second argument of the function. This is the args parameter shown in the token callback reference in this doc.
So in your case you would need to modify the signature of your token callback function from function(token) {...} to function(token, args) {...} and then you should be able to get the info from args.
Here is an example: https://jsfiddle.net/rghpes57/3/
in this example the second console.log is the one you want to look at to see how the object looks like.

I'm trying to make sense of session, but cannot get any data out of it

Currently using LUIS in a bot that connects to Slack. Right now I'm using interactive messages and trying to respond to user input correctly. When I click an item from the drop down LUIS receives it as a message. I can get the text with session.message.text, however I need to get the callback_id of the attachment as well as the channel it was sent from.
I've used console.log(session) to get an idea of what session looks like. From there I've seen that session.message.sourceEvent contains the data I need, however I can't use indexOf() or contains() to actual extrapolate the data. I've also tried session.message.sourceEvent.Payload but end up getting "[object [Object]]". I've tried searching for documentation on session formatting but to no avail.
Below is a snippet of what is returned when I run console.log(session.message.sourceEvent).
{ Payload:
action_ts: '1513199773.200354',
is_app_unfurl: false,
subtype: 'bot_message',
team: { id: 'T03QR2PHH', domain: 'americanairlines' },
user: { id: 'U6DT58F2T', name: 'john.cerreta' },
message_ts: '1513199760.000073',
attachment_id: '1',
ts: '1513199760.000073' },
actions: [ [Object] ],
callback_id: 'map_selection1',
original_message:
username: 'Rallybot',
response_url: 'https://hooks.slack.com/actions/T03QR2PHH/287444348935/Y6Yye3ijlC6xfmn8qjMK4ttB',
type: 'message',
{ type: 'interactive_message',
channel: { id: 'G6NN0DT88', name: 'privategroup' },
token: 'removed for security',
{ text: 'Please choose the Rally and Slack team you would like to map below.',
bot_id: 'B7WDX03UM',
attachments: [Array],
trigger_id: '285857445393.3841091595.085028141d2b8190b38f1bf0ca47dd88' },
ApiToken: 'removed for security' }
session.message.sourceEvent is a javascript Object, however indexOf or contains are functions of String or Array types.
Any info you required in the object, you should direct use the code <object>.<key> to invoke that value. You can try session.message.sourceEvent.Payload.action_ts for example.
Also, you can use Object.keys(session.message.sourceEvent) to get all the keys in this object.

displaying custom error messages in stripecheckout.js

I am using stripecheckout.js for handling payments on my site.
I am configuring the object to be send to the stripe pop up as:
stripeHandler = StripeCheckout.configure({
key: $scope.stripeKey,
image: 'img/logo.png',
locale: 'auto',
panelLabel: 'Pay',
color: 'black',
token: handleToken
});
where handleToken is my callback.
Opening the stripe pop up as :
stripeHandler.open({name: //some name,
description: //some description,
amount: //some amount,
currency: //some currency
});
Now I want to display error custom messages related to expiry and cvv fields.
How do I have to pass these messages inside stripe popup.
You can't. Checkout has a number of configuration options, but it is not possible to customize the error messages.
If Checkout supports your language, you can use the data-locale attribute to force a specific language, or set it to auto to use the customer's preferred language automatically.

Missing required field: member

{ errors:
[ { domain: 'global',
reason: 'required',
message: 'Missing required field: member' } ],
code: 400,
message: 'Missing required field: member' }
I get this error when I run the following request:
var request = client.admin.members.insert({
groupKey: "some_group#example.com"
, email: "me#example.com"
});
I was authenticated successfully (I received the access token and so on) but when I execute the request above it callbacks that error.
What member field am I supposed to add?
It works fine in API Explorer using groupKey and email fields.
The documentation at https://developers.google.com/admin-sdk/directory/v1/reference/members/insert for admin.members.insert indicates that it requires a groupKey parameter, but that the body (which the node.js library handles as a separate object) should contain a members object containing the role property. See the API Explorer a the bottom of that page as well.
email is part of the form data. The form data must be passed as object in the second argument:
// create the group insert request
var request = client.admin.members.insert({
groupKey: "some_group#example.com"
}, {
email: "me#example.com"
});

Password confirmation and external Model validation in Sails.js

I've been playing around with Sails for maybe one day. I'm trying to wrap my head around what would be the best way to do extensive validation in Sails.js.
Here is the scenario:
Registration Form:
Username: _______________
E-Mail: _______________
Password: _______________
Confirm: _______________
User inputs:
a correct e-mail
a username that already exists
two passwords that don't match
Desired outcome:
Username: _______________ x Already taken
E-Mail: _______________ ✓
Password: _______________ ✓
Confirm: _______________ x Does not match
Requirements, a few key points:
The user receives all error messages (not just the first one) for every aspect of his input. They are not vague ("username already taken" or "username must be at least 4 letters long" is better than "invalid username")
The built-in model validation can obviously not be responsible for checking a matched password confirmation (SRP)
What I think I need to do:
UserController:
create: function(req, res) {
try {
// use a UserManager-Service to keep the controller nice and thin
UserManager.create(req.params.all(), function(user) {
res.send(user.toJSON());
});
}
catch (e) {
res.send(e);
}
}
UserManager:
create: function(input, cb) {
UserValidator.validate(input); // this can throw a ValidationException which will then be handled by the controller
User.create(input, cb); // this line should only be reached if the UserValidator did not throw an exception
}
User: (model)
attributes: {
username: {
type: 'string',
required: true,
minLength: 3,
unique: true
},
email: {
type: 'email',
required: true,
unique: true
},
password: {
type: 'string',
required: true
}
}
UserValidator:
This is the tricky part. I need to combine input-specific validation (does the password confirmation match?) with the Model validation (is the username taken and is the e-mail address valid?).
If there was a way to instantiate a User-model and perform validation without saving to the database in Sails/Waterline I think this would be quite straight-forward, but there doesn't seem to be that option.
How would you go about solving this problem? Thank you very much for your help!
You can do this in your model:
module.exports = {
types: {
mycustomtype: function (password) {
return password === this.confirm;
}
},
attributes: {,
password:{
type: 'STRING',
required: true,
mycustomtype: true
}
}
}
There are going to be some validations that you can perform immediately on the client-side without needing to round-trip to the server. Things like comparing the password with the confirmation password, as well as verifying a string matches an email regex can be done with client-side javascript.
For other things like checking whether a username exists or not, you could use an ajax call to sails to directly ask it 'does this username exist' and provide real-time validation on the client-side based on the result, or you can wait until the user submits the form and parse the form submission to display those validations. Since checking ahead of time for things like this aren't 100% reliable (i.e. someone could create a user with that name after the check but prior to the form being posted back), some people choose to forgo the pre-check and only handle the error after post.
Waterline has its own built-in validation mechanism called Anchor, which is built on validator.js (previously called node-validator). For a full list of validations available, see here. I would recommend that instead of defining a separate validation layer, you define a method that parses the sails validation messages and formats them in a way that is user-friendly and consistent.
If you want to perform your own validations outside of what Waterline would do for you, you could do those validations inside a lifecycle callback, for instance the beforeCreate(values, callback) lifecycle callback. If you detect errors, you could pass them into the callback as the first parameter, and they would be passed back as an error to the caller of the create collection method.
An alternative to using a lifecycle callback, would be to create your own collection method that handles the create. Something like this:
Users.validateAndCreate(req.params.all(), function (err, user) {
...
});
More information about how to create a collection method like this can be found in my answer to this question: How can I write sails function on to use in Controller?

Resources