Blazor.AdaptiveCards icon indicating copy to clipboard operation
Blazor.AdaptiveCards copied to clipboard

Question: Cards with different schema's

Open dazinator opened this issue 4 years ago • 0 comments

I have an API on the backend that returns a collection of "Notifications". Each Notification has some common properties like "NotificationId" etc etc. However it also has some Data associated with it which is some JSON. The schema of that JSON data depends upon the notification type.

For example here are 3 notifications, the first two are "Alert" and the third is a "Message" notification:

[
   {
    "NotificationId": 1
    "Type": "Alert",
    "Data": "{\"severity\":\"warning\"}"
   },
   {
    "NotificationId": 2
    "Type": "Alert",
    "Data": "{\"severity\":\"error\"}"
   },
  {
    "NotificationId": 3
    "Type": "Message",
    "Data": "{\"content\":\"hi there\"}"
   },
]

Is it possible for me to render these "Notifications" as a Card Collection, whilst also rendering the "Data" - which is JSON, in the template?

The nearest example I can find is this: https://www.adaptivecardsblazor.com/cards/templateselector.html

The issue though with the above example, is that there seems to be one schema for the card collection as a whole, where as for my case, I would need to vary the schema for each individual item based on its notification type. For example Id like to display a card list for the above notifications that resembles something like this:

  • Card 1 = Alert: "Warning"
  • Card 2 = Alert: "Error"
  • Card 3 = Message: "Hi There"

Also my "Notification" objects all have a Data property but that is just a JSON string. It seems I'd need to turn that into a C# object instance in order to be able to use it in an adaptive card template is that correct?

Perhaps this is possible by somehow nesting adaptive cards? So I'd have an outer card for the "Notification" which always renders the same, but then it conditionally renders another Adaptvie Card inside itself, based on it's "notification type" to pick the schema / template used - and passing its JSON "Data" as a C# object instance to that sub-card. Do you think an approach like that could work?

Many Thanks

P.S I only recently stumbled accross adaptive cards and am investigating whether I can switch to it. Right now, I use scriban on the backend to generate the HTML for each notification and it's JSON data, and the resulting HTML is delivered to the browser and displayed within each card as Raw html, meaning there is no templating to do on the client. This approach has its downsides however.

dazinator avatar Jun 17 '20 12:06 dazinator