If/else in FormatJS / React-intl - reactjs

Is it possible to have an if/else in a FormatJS message?
Example
I have a boolean variable isDay that should determine what message is shown. When true I want to show the wording "Day" and when false I want to show Night.
// message string
message = "Day";
// React component
<FormattedMessage
id="message"
values={{isDay: true}}
/>
I want to be able to do something like:
message = "{if isDay}Day{else}Night{endif}";
I know the above is not the actual syntax, but wondering if something like this is possible with FormatJS?

Found a solution using the ICU Message select syntax.
message = "{isDay, select, true {Day} other {Night}}";

Related

Selecting a node with a dot or fullstop in the name with Puppeteer

We've a form with a lot of elements in it like this, with the only unique identifier being the name attribute which is always of the form company.something
<input class="form-control" type="text" name="company.name" value="">
Both of the following attempts at selecting this text input fail
await page_c.click('input[name=company.name]')
await page_c.type('input[name=company.name]', client.companyName)
await page_c.click('input[name=company\.name]')
await page_c.type('input[name=company\.name]', client.companyName)
with the following error
Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': 'input[name=company.name]' is not a valid selector.
Is there a way to handle elements where the name attribute had a dot or full stop in it with out rewriting the front end please? Thanks.
After searching for the error message I found Cannot use query selector with id's that includes "." double escaping works.
await page_c.click('input[name=company\\.name]')
await page_c.type('input[name=company\\.name]', client.companyName)
As this is in an attribute selector, you don't need to settle for double escaping — quoting the attribute value is the less error-prone solution:
await page_c.click('input[name="company.name"]')
await page_c.type('input[name="company.name"]', client.companyName)

react-apollo mutation component sends empty strings as null

I will reduce the issue to a simple example. I have a table component rendering a table with queried data. I also have a small form that allows users to input data into the table. The issue i am having is that the default value of the input fields are empty strings.
Sample input field:
<td><input type="text" placeholder="Nombre" name="name" onChange={this.handleChange} value={this.state.name}/></td>
Assume that the state is holding an empty string as default value for the input field
this.state={name:' '}
Assume also that i want to send in my form an empty string to the database. I am doing the mutation the apollo way, with graphql:
const ADD_CONTACTS = gql`
mutation createContact(
$name: String,
){
createContact(
name: $name
){
id
name
}
}`
<Mutation mutation={ADD_CONTACTS}>
{createContact=>(
<form onSubmit={async e=> {
e.preventDefault();
let name = this.state.name;
await createContact({variables : {
"name":name
} })
}}
>
<table>
... table markup with input fields
</form>
)}
</Mutation>
Ok, that's the general context. In actuality the form contains 6 fields. But the issue is that when I send an empty string to the query inside the variable, the moment it sends the query to the server it changes the empty string to a null value so that i end up with this error:
GraphQL error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null
I have tested the server using graphiql and the requests themselves can accept empty strings, but it seems to be that the mutation component or apollo, somewhere, is converting my empty string that im passing to the graphql variables into null.
I have managed to make a workaround for this scenario, but it's definitely not the right way to deal with this. If I could somehow get apollo to actually send empty strings instead of null it would solve the issue.
thanks to anyone that has any idea why this i
Try removing async and await.

display errors when the username doesn't fit pattern

I'm using symfony and I want to display errors when the username doesn't fit pattern, how can I do that?
{{ form_widget(form.username, {'attr':{'pattern': '[a-zA-Z]*'} }) }}
Try to add Assert to your property in the entity class.
Check out this for Asserts : http://symfony.com/doc/current/validation.html
And this one for regex : http://symfony.com/doc/current/reference/constraints/Regex.html

Odoo8 - how can I sort status bar and set default as new?

I have created a new module in Odoo for helpdesk and I have 2 problems that I can't seem to fix or find information on so need some help.
I created a status bar (code):
state = fields.Selection({('new','New'), ('open','In Progress'), ('closed','Closed')}, "Status")
_defaults = {
'state': 'new'
}
<header>
<field name="state" widget="statusbar" statusbar_visible="new,open,closed" clickable="True"/>
Even thought I have stated "new, open, closed" it is showing in Odoo as open,new, closed.
I set the state default as new, even though I am not getting any errors, when I click on create it shows state as blank.
Any ideas on how to fix these issues?
When you declared your field you gave it a set of options instead of a list of options. Sets in Python doesn't keep the information about items order, but lists do. For your declared order to be respected you just need to replace the set literal by a list literal:
state = fields.Selection(
[('new','New'), ('open','In Progress'), ('closed','Closed')],
"Status",
)
You can remove statusbar_visible from your view.
As for your second problem (with the default value) Emipro Technologies is correct. You need to declare the default value as an argument on your field:
state = fields.Selection(
[('new','New'), ('open','In Progress'), ('closed','Closed')],
default='new',
string="Status",
)
Your fields declaration seems that it's Odoo-8 code, in V8 _defaults is not there you need to write as below,
state = fields.Selection({('new','New'), ('open','In Progress'), ('closed','Closed')},"Status", default='new')
And there is no more logic to set sequence in status bar but then also try this,
<form string="String" version="7.0">
<header>
<field name="state" widget="statusbar" statusbar_visible="new,open,closed" clickable="True"/>
</header>
</form>

Asserting a checkbox is not checked by default in WebDriver with Junit

I'm using WebDriver with Junit 4.11 and I want to assert that a checkbox is not selected by default, and to do that I'm unsure which constructor method to choose.
The following is from the DOM before the checkbox is selected:
<input type="checkbox" id="c234" name="instantAd" value="true" class="t-checkbox-A">
Then, once the checkbox becomes selected a 'checked' is added, like so:
<input type="checkbox" id="c234" name="instantAd" value="true" checked="" class="t-checkbox-A">
I have tried the following:
WebElement checkBox = chrome.findElement(By.cssSelector("input.t-checkbox-A[name=\"instantAd\"]"));
new WebDriverWait(chrome, 5).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.t-checkbox-A[name=\"instantAd\"]")));
Assert.assertEquals("null",checkBox.getAttribute("checked"));
checkBox.click();
Assert.assertEquals("true",checkBox.getAttribute("checked"));
The first assertion fails. Perhaps this is because the 'checked' attribute isn't visible in the DOM yet, at a guess.
The stacktrace is displaying:
java.lang.AssertionError: expected: java.lang.String but was: null
I've searched many different posts but none offer me the answer Im looking for, and when checking http://junit.sourceforge.net/javadoc/org/junit/Assert.html for info and guidance (as being new to test automation, Im finding it difficult to work out what I need in my constructor.
Any help would be most appreciated.
There is a less preferable way to achieve the goal, than #mark suggested. (For cases when isSelected() does not work).
boolean isFound = false;
try {
isFound = driver.findElement(By.cssSelector("input.t-checkbox-A[name=\"instantAd\"][checked]")).isDisplayed();
} catch (NoSuchElementException e) {
// Do nothing
}
Assert.assertFalse(isFound);
checkBox.click();
try {
isFound = driver.findElement(By.cssSelector("input.t-checkbox-A[name=\"instantAd\"][checked]")).isDisplayed();
} catch (NoSuchElementException e) {
// Do nothing
}
Assert.assertTrue(isFound);
In short, the code tries to verify if the element with the "checked" attribute is displayed. If it is, then isFound is set to true, else isFound remains false
There are disadvantages:
too much code
the test will spend additional 5 seconds before throwing the NoSuchElementException when the element is not found
The "try" clause can be externalized to a separate method.
With the help from #Francis on SQA Stackexchange I managed to solve the issue to assert a checkbox to be deselected by default.
The code that I had written (posted in my question above) contained the below assertion:
Assert.assertEquals("null",checkBox.getAttribute("checked"));
What I needed (again, thanks to Francis for suggesting this method) was the below assertNull:
Assert.assertNull(checkBox.getAttribute("checked"));
Job done.
Many thanks to all that looked into this.

Resources