Hi all!
I'm hoping you can help me improve the performance of a 'like' button that I have added to a gallery. Essentially, the idea is that any user can 'like' items in the gallery.
I have a datasource SharePoint List of 'Ideas' and a separate SharePoint list of 'Likes' so I can keep track of total likes for each idea. Collections for the Ideas and Likes (and a separate collection of the user's own likes) are built when the screen is loaded and the gallery is fed by these Collections.
In practical terms:
- user clicks the 'like' icon (which is initially an empty heart image, not an icon)
- the button then changes to a full heart image
- the datasource table of 'Likes' is patched to add a record that is associated with that gallery record and with that particular user
- the gallery also shows a running total of Likes for each Idea
Three separate things are happening when the user clicks the Like button:
1. image source changes (toggles between empty heart and full heart image)
2. record is patched to Likes (or removed from Likes if that user has previously liked that Idea)
3. running total text label is updated
At the moment, this seems quite slow so this detracts from the user experience. There's a noticeable lag between clicking the 'Like' image and then seeing the image change and the running total get updated.
Here's the code I'm using for the OnSelect property of the 'like' image in the gallery:
If (
CountRows(
Filter(
colUserLikes,
Idea.Id = ThisItem.ID
)
) = 0,
Patch(
Portal_Likes,
Defaults(Portal_Likes),
{
Idea: {
Id: ThisItem.ID,
Value: ThisItem.ID
},
User: {
Claims: "i:0#.f|membership|" & varUserEmail,
Department: "",
DisplayName: varUserName,
Email: varUserEmail,
JobTitle: " ",
Picture: " "
}
}
),
RemoveIf(
Portal_Likes,
Idea.Id = ThisItem.ID And User.DisplayName = varUserName
)
);
Refresh(Portal_Likes);
ClearCollect(
colLikes,
Filter(
'Portal_Likes',
ManualID <> 0
)
);
ClearCollect(
colUserLikes,
Filter(
'colLikes',
User.DisplayName = varUserName
)
)
And here's the code I'm using for the Image property of the 'like' image in the gallery:
If (
CountRows(
Filter(
colUserLikes,
Idea.Id = ThisItem.ID
)
) = 0,
'empty_heart',
'full_heart'
)
And here's the code I'm using in the Text property of the text label showing a running total:
If (
CountRows(
Filter(
colLikes,
Idea.Id = ThisItem.ID
)
) = 1,
CountRows(
Filter(
colLikes,
Idea.Id = ThisItem.ID
)
) & " like",
CountRows(
Filter(
colLikes,
Idea.Id = ThisItem.ID
)
) & " likes"
)
Any help you can provide to optimise this code would be much appreciated! I expect I could do better with referencing variables or collections and reducing the calculations. Happy to use Named Formulas if this would provide a performance benefit for any aspect.
Thank you!