Elm adding item in a list on update - list

I m trying to understand Elm lang, and I m having some problem dealing with the update part of my little app.
Actually, I want that when I click on a button, it adds a new User in a List, something really simple that doesn't have any sense, but I need to understand.
Right now, I m having the following code :
main =
Html.beginnerProgram { model = model, view = view, update = update }
type Msg = AddUser
type alias User =
{
username : String,
firstname: String,
lastname: String
}
model: List User
model =
[]
update: Msg -> User -> List User -> List User
update msg user userList =
case msg of
AddUser ->
user::userList
And I m having the following error :
-- TYPE MISMATCH ------------------------------------------------------ main.elm
The argument to function `beginnerProgram` is causing a mismatch.
5| Html.beginnerProgram { model = model, view = view, update = update }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Function `beginnerProgram` is expecting the argument to be:
{ ..., update : Msg -> List User -> List User }
But it is:
{ ..., update : Msg -> User -> List User -> List User }
Hint: Problem in the `update` field. I always figure out field types in
alphabetical order. If a field seems fine, I assume it is "correct" in
subsequent checks. So the problem may actually be a weird interaction with
previous fields.
In fact what I m currently understanding from the update function is :
Take a Msg in the first function that returns a function accepting a User
This function (accepting the user) returns a function accepting a List of Users (to make the operation user::userList)
This last function returns a List of User and I m done
But I can't get the point where I am wrong in this situation.
Can you help me ?

Html.beginnerProgram expects the update function to be of type Msg-> Model -> Model (your Model is named "User" which is no problem). The User you want to add belongs to the "AddUser" union type.
type Msg = AddUser User
This means you pass the new User along with the Msg (so you can use different messages passing different things).
To get the new User you can pattern match it in "case"
case msg of
AddUser newUser ->
newUser :: userList

Related

How to add a dropdown list with parameters that user chooses and append to API request?

In a power query custom connector I can make the user input some text that gets appended to the url to an API request.
// Example in the navigator:
(optional time_range as text) as table => GetSomething(time_range)
// And the actual function
GetSomething = (optional time_range as text) =>
let
_time_range = if time_range <> null then time_range else "medium_term",
options = [Headers = [#"Content-Type" = "application/Json"], Query = [limit = "50", time_range=_time_range]],
source = Web.Contents("https://example.com/v1/me/", options),
json = Json.Document(source),
listOfItems = json[items]
in
listOfItems;
My goal would be to make the user select from a dropdown list with those three values (short, medium, long), instead of making him write and possible compromise the request.
Do you know how to achieve this? I'm searching at the moment. If I find I'll post here.
So, I found the answer in this blogpost:
https://blog.crossjoin.co.uk/2014/11/27/specifying-allowed-values-sample-values-and-descriptions-for-function-parameters-in-power-querypart-1/
// just add a type for the parameters and enter AllowedValues
GetSomethingParamType = type text
meta [Documentation.Description = "Please enter a time range",
Documentation.AllowedValues = {"short_term","medium_term","long_term"}],
// the type for the function
GetSomethingType = type function(
_time_range as GetSomethingParamType)
as list,
// the type replacement
GetSomethingV2 = Value.ReplaceType(GetSomething,
GetSomethingType)

Search custom property in SharePoint User Profile

I created a custom property in the user profile.
I want to search through all the user profiles and output those profiles in which the custom property contains a certain string
For example:
User1- Custom Property value is 1,2,3
User2- Custom Property value in 2,4,5
User3- Custom Property value is 4,6,8
I want to output all the profiles in which Custom Property contains 2
(using c# code)
So the output should have User1 and User2
Can someone suggest the best way to implement this?
I did find some links in the internet for searching user profiles use KeyWord search but and not sure if those methods could be used to search through Custom Properties.
Example: https://www.collaboris.com/how-to-use-search-keywordquery-to-query-user-profiles-in-sharepoint/
I am using SharePoint 2013
We ended up promoting the Custom Property that we added to the User profile to a Managed property.
Also it seems like we can do wildcard searches on managed properties so we do People searches like "CustomProperty:*,2,*" so that it would return all the user profiles which have the number 2 in the custom property of their user profile
Interestingly the wildcard works only on the end for OOTB properties like FirstName so we cant do things like FirstName:oh and expect it would return John Doe's profile
But we can certainly do this - FirstName:joh* and that would return all the people whose first name starts with Joh (which would include John Doe)
But it seems like the wildcard works both at the beginning and the end for the custom managed properties (which helped a great deal for us)
On how to return the results of the search using c# we used this-
private DataTable GetPeople(SPSite spSite, string queryText)
{
var keywordQuery = new KeywordQuery(spSite)
{
QueryText = queryText,
KeywordInclusion = KeywordInclusion.AllKeywords,
SourceId = System.Guid.Parse("b09a7990-05ea-4af9-81ef-edfab16c4e31")
};
keywordQuery.RowLimit = this.MaxProfilesToDisplay;
keywordQuery.SelectProperties.Add("AccountName");
keywordQuery.SelectProperties.Add("PictureURL");
SearchExecutor e = new SearchExecutor();
ResultTableCollection rt = e.ExecuteQuery(keywordQuery);
var tab = rt.Filter("TableType", KnownTableTypes.RelevantResults);
var result = tab.FirstOrDefault();
DataTable DT = result.Table;
return DT;
}
and we would invoke this like
DataTable dt = GetPeople(SPContext.Current.Site, "CustomProperty:*,2,*" );

Ember.js: How to get an array of model IDs from a corresponding array of model attributes

For a Tag model that I have in Ember-Data, I have 4 records in my store:
Tags:
id tag_name
1 Writing
2 Reading-Comprehension
3 Biology
4 Chemistry
In my code I have an array of tag_names, and I want to get a corresponding array of tag IDs. I'm having 2 problems:
My server is being queried even though I have these tags in my store. When I call store.find('tag', {tag_name: tag_name}), I didn't expect to need a call to the server. Here is all the code I'm using to attempt to create an array of IDs.
var self = this;
var tagsArray = ["Writing", "Reading-Comprehension", "Chemistry"];
var tagIdArr = []
tagsArray.forEach(function(tag_name) {
return self.store.find('tag', { tag_name: tag_name }).then(function(tag) {
tagIdArr.pushObject(tag.get('content').get('0').get('id'));
})
})
return tagIdArr;
When I console.log the output of the above code gives me an empty array object with length 0. Clicking on the caret next to the empty array shows three key-value pairs with the correct data. But the array is empty. I'm sure there is a simple explanation for this behavior, but I'm not sure why this is. I've used code similar to the above in other places successfully.
Find hits the server, but peek does not.
var tagsArray = ["Writing", "Reading-Comprehension", "Chemistry"];
return this.store.peekAll('tag').filter(function(tag){
return tagsArray.indexOf(tag) !== -1;
}).mapBy('id');
See: http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_reorganized-find-methods

Persisting domain model w/ list of enums not working in GORM/Grails

My model needs to have multiple enums of the same type:
class Broker {
static constraints = {
brokerTypes(nullable:false)
}
List<BrokerType> brokerTypes
}
The model is being instantiated with the params from the request, which has in it a list of BrokerTypes:
def save(){
def brokerInstance = new Broker(newParams)
System.out.println(brokerInstance.getBrokerTypes().toString());
if (!brokerInstance.save(flush: true)) {
render(view: "create", model: [brokerInstance: brokerInstance])
return
}
redirect(action: "show", id: brokerInstance.id)
}
The println prints out the list of BrokerTypes as expected, so i know that it exists in the instance. Later, the model is retrieved as follows:
def brokerInstance = Broker.findByLatAndLon(lat,lon)
System.out.println(brokerInstance.getBrokerTypes().toString());
This time the println prints out 'null'
So i imagine the problem is that GORM doesn't know how to store this list of enums, and instead when the brokerInstance.save() is called, its saving the brokerTypes field as null.
Do i need to create a mapping somehow to get GORM to recognize the list? A hack alternative would be to instead of storing the list of enums, to store a list of strings or something and then map back to the enum when needed, but this doesnt seem clean
You will have to use a hasMany clause so that grails/gorm initializes a one to many relationship
You should add the following snippet to your domain class.
static hasMany = [brokerTypes : BrokerType]

Sharepoint ResolvePrincipals function

I'm trying to add a user, programmtically, to a field of Type="User" in a SharepointList. Since I do not know the user's unique ID within the site in advance, I'm using the ResolvePrincipals function to add the user to the SPUserCollection as follows:
Dim managerDN() As String = {"some.user#email.com"}
Dim principalInfo() As PrincipalInfo = people.ResolvePrincipals(managerDN, SPPrincipalType.User, True)
Console.WriteLine(principalInfo(0).UserInfoID)
Problem is when I look at the UserInfoID, which is what I'm looking for, I get back -1. I assumed that the ResolvePrincipals function would add the user to the site user collection automatically (According to MSDN documentation) and create a unique, positive UserInfoID in the process. I'm not sure if I have the right idea or not
I always use this code:
SPUser user = web.EnsureUser("login");
SPFieldUserValue value = new SPFieldUserValue(web, user.ID, user.LoginName);
If you only have an email address, you could use this:
SPUser user = web.AllUsers.GetByEmail("email");
if (user != null)
{
SPFieldUserValue value = new SPFieldUserValue(web, user.ID, user.LoginName);
}
I'm not really sure if GetByEmail returns null or throws an error if the user cannot be found, so be sure to check that first !