Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 56072b2

Browse files
committed
feat: add discount code generator example
1 parent 06f5b64 commit 56072b2

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,54 @@
11
import * as cdk from 'aws-cdk-lib'
2+
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'
3+
import { Action, Buttonize, ButtonizeApp, Display, Input } from 'buttonize/cdk'
24
import { Construct } from 'constructs'
5+
import * as path from 'path'
36

47
export class ExampleStack extends cdk.Stack {
58
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
69
super(scope, id, props)
10+
11+
Buttonize.init(this, {
12+
apiKey: 'btnz_mybuttonizekey1234567',
13+
externalId: 'CHANGE-ME-some-random-external-id'
14+
})
15+
16+
const discountGenerator = new NodejsFunction(this, 'DiscountGenerator', {
17+
entry: path.join(__dirname, '../', 'discountGenerator.ts')
18+
})
19+
20+
new ButtonizeApp(this, 'DemoApp', {
21+
name: 'Discount code generator',
22+
description:
23+
'Select the discount amount and you will get the discount code on the next page.'
24+
})
25+
.page('InputPage', {
26+
body: [
27+
Display.heading('Generate discount code for customer'),
28+
Input.select({
29+
id: 'discount',
30+
label: 'Discount value',
31+
options: [
32+
{ label: '30%', value: 30 },
33+
{ label: '60%', value: 60 }
34+
]
35+
}),
36+
Display.button({
37+
label: 'Generate discount',
38+
onClick: Action.aws.lambda.invoke(
39+
discountGenerator,
40+
{ Payload: { discountValue: '{{discount}}' } },
41+
{ id: 'discountGenerator' }
42+
),
43+
onClickFinished: Action.buttonize.app.changePage('DonePage')
44+
})
45+
]
46+
})
47+
.page('DonePage', {
48+
body: [
49+
Display.heading('Discount generated'),
50+
Display.text('Discount code: {{InputPage.discountGenerator.code}}')
51+
]
52+
})
753
}
854
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const handler = async (event: { discountValue: number }) => {
2+
console.log(`Generating discount of value ${event.discountValue}`)
3+
4+
return {
5+
discountValue: event.discountValue,
6+
code: `${Math.random()}`.split('.')[1]
7+
}
8+
}

0 commit comments

Comments
 (0)