Skip to content

Commit

Permalink
Merge pull request #11 from valgaze/add_data
Browse files Browse the repository at this point in the history
add addData to SpeedyCard
  • Loading branch information
valgaze authored Oct 13, 2021
2 parents 3ca5300 + 613ee54 commit 937b530
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 52 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default [{
.setInput(`What's on your mind?`)
.setUrl('https://www.youtube.com/watch?v=3GwjfUFyY6M', 'Take a moment to celebrate')
.setTable([[`Bot's Date`, new Date().toDateString()], ["Bot's Uptime", `${String(process.uptime())}s`]])
.setData({mySpecialData: {a:1, b:2}})
bot.sendCard(myCard.render(), 'Your client does not currently support Adaptive Cards')
},
helpText: 'Sends an Adaptive Card with an input field to the user'
Expand Down
2 changes: 1 addition & 1 deletion docs/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ const cardJson = new SpeedyCard().setTitle('System is 👍', {st})
.setImage('https://i.imgur.com/SW78JRd.jpg')
.setInput(`What's on your mind?`)
.setUrl('https://www.youtube.com/watch?v=3GwjfUFyY6M', 'Take a moment to celebrate')
.setData({mySpecialData: {a:1, b:2}})

bot.sendCard(cardJson.render(), 'Your client does not currently support Adaptive Cards')

```

</details>
Expand Down
97 changes: 46 additions & 51 deletions src/cards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import {bad} from './index'
export interface BaseConfig {
title?: string;
titleConfig?: Partial<TextBlock>;
Expand Down Expand Up @@ -52,6 +52,17 @@ export interface inputConfig {
placeholder?: string;
}

export interface Fact {
title: string;
value: string
}
export interface FactSet {
type: 'FactSet',
facts: Fact[]
}
export interface AttachmentData {
[key: string]: any
}

/**
* SpeedyCard
Expand Down Expand Up @@ -91,6 +102,8 @@ export class SpeedyCard {
public url = ''
public urlLabel = 'Go'
public tableData: string[][] = []
public attachedData: AttachmentData = {}
public needsSubmit = false

public json:EasyCardSpec = {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
Expand Down Expand Up @@ -166,8 +179,15 @@ export class SpeedyCard {
return this
}

setData(payload:AttachmentData) {
if (payload) {
this.attachedData = payload
this.needsSubmit = true
}
return this
}

render() {
let needsSubmit = false
if (this.title) {
const payload:TextBlock = {
type: 'TextBlock',
Expand All @@ -194,52 +214,18 @@ export class SpeedyCard {
}

if (this.tableData && this.tableData.length) {
const payload = {
"type": "ColumnSet",
"columns": [],
"spacing": "Padding",
"horizontalAlignment": "Center"
}

const columnsData: { type: string, width: number, items: any[] }[] = [
{
"type": "Column",
"width": 35,
"items": []
},
{
"type": "Column",
"width": 65,
"items": []
}
]

const buildLabel = (label: string) => {
return {
"type": "TextBlock",
"text": label,
"weight": "Bolder",
"color": "Light",
"spacing": "Small"
}
}
const buildValue = (value: string) => {
return {
"type": "TextBlock",
"text": value,
"color": "Light",
"weight": "Lighter",
"spacing": "Small"
}
const payload: FactSet = {
"type": "FactSet",
"facts": []
}

this.tableData.forEach(([label, value], i) => {
columnsData[0].items.push(buildLabel(label))
columnsData[1].items.push(buildValue(value))
const fact:Fact = {
title: label,
value
}
payload.facts.push(fact)
})

// @ts-ignore
payload.columns = columnsData

this.json.body.push(payload)
}
Expand All @@ -256,7 +242,7 @@ export class SpeedyCard {
}

if (this.choices.length) {
needsSubmit = true
this.needsSubmit = true
const payload: ChoiceBlock = {
type: 'Input.ChoiceSet',
id: 'choiceSelect',
Expand All @@ -270,7 +256,7 @@ export class SpeedyCard {
}

if (this.inputPlaceholder) {
needsSubmit = true
this.needsSubmit = true
const payload = {
"type": "Input.Text",
placeholder: this.inputPlaceholder,
Expand All @@ -280,15 +266,24 @@ export class SpeedyCard {
}


if (needsSubmit) {
const payload = {
if (this.needsSubmit) {
interface SubmitPayload {
type: string;
title: string;
data?: unknown
}
const payload:SubmitPayload = {
type: "Action.Submit",
title: this.buttonLabel,
"data": {
"cardType": "inputForm"
}
}
if (this.attachedData) {
payload.data = this.attachedData
}
this.json.actions = [payload]
} else {
if (this.attachedData && Object.keys(this.attachedData).length) {
bad(`attachedData ignore, you must call at least either .setInput() or .setChoices to pass through data with an adaptive card`)
}
}

if (this.url) {
Expand Down
89 changes: 89 additions & 0 deletions test/card.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import test from "tape";
import {SpeedyCard} from './../src'

test("setup", function (t) {
t.end();
});

test("Sanity test", (t) => {
const expected = {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [{
"type": "TextBlock",
"text": "System is 👍",
"weight": "Bolder",
"size": "Large",
"wrap": true
}]
}
const cardPayload = new SpeedyCard().setTitle('System is 👍')


const actual = cardPayload.render()
t.deepEqual(actual, expected);
t.end();
});

test("Kitchen sink", (t) => {
const expected = {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [{
"type": "TextBlock",
"text": "System is 👍",
"weight": "Bolder",
"size": "Large",
"wrap": true
}, {
"type": "TextBlock",
"text": "If you see this card, everything is working",
"size": "Small",
"isSubtle": true,
"wrap": true,
"weight": "Lighter"
}, {
"type": "FactSet",
"facts": [{
"title": "Bot's Uptime",
"value": "12.492006583s"
}]
}, {
"type": "Image",
"url": "https://i.imgur.com/SW78JRd.jpg",
"horizontalAlignment": "Center",
"size": "Large"
}, {
"type": "Input.Text",
"placeholder": "What's on your mind?",
"id": "inputData"
}],
"actions": [{
"type": "Action.Submit",
"title": "Submit",
"data": {
"mySpecialData": {
"a": 1,
"b": 2
}
}
}]
}

const cardPayload = new SpeedyCard().setTitle('System is 👍')
.setSubtitle('If you see this card, everything is working')
.setImage('https://i.imgur.com/SW78JRd.jpg')
.setInput(`What's on your mind?`)
.setTable([["Bot's Uptime", `12.492006583s`]])
.setData({mySpecialData: { a:1,b:2}})

const actual = cardPayload.render()
t.deepEqual(actual, expected);
t.end();
});

test("teardown", function (t) {
t.end();
});

0 comments on commit 937b530

Please sign in to comment.