Skip to content

Commit 67981dc

Browse files
committed
Added most of the media upload tutorial
1 parent 6b37bdc commit 67981dc

File tree

3 files changed

+205
-1
lines changed

3 files changed

+205
-1
lines changed

docs/spfx/viva/get-started/actions/media-upload/MediaUploadTutorial.md

Lines changed: 205 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,208 @@ At this point, if you do `gulp serve`, then you will see the `MediaUpload` card:
4747

4848
![See the MediaUpload card icon in the webpart toolbox](./img/mediaUploadTutorialACE.PNG)
4949

50-
## Add media upload action to your Adaptive Card Extension
50+
## Add media upload action to your Adaptive Card Extension
51+
52+
At this point we have out of the box Adaptive Card Extension code. Now it's time to flare things up with selecting media from the Card and Quick views.
53+
54+
In the Card View, we will provide a button which will perform the following actions:
55+
56+
- Upload an image file
57+
58+
### Update the labels that will show up on the card
59+
60+
Before we start adding the actions, let us first update the strings that you will see on the card.
61+
62+
For this, locate and open the following file in your project: **./src/adaptiveCardExtensions/mediaUpload/loc/en-us.js**
63+
64+
Replace the content of this file with:
65+
66+
```javascript
67+
define([], function() {
68+
return {
69+
"PropertyPaneDescription": "Tutorial on media upload action in ACE.",
70+
"TitleFieldLabel": "MediaUpload",
71+
"Title": "Media Upload",
72+
"SubTitle": "Media Upload Actions",
73+
"PrimaryText": "Media Upload Demo",
74+
"Description": "Demonstrating Media Upload Capabilities",
75+
"UploadPNG": "Upload PNG file"
76+
}
77+
});
78+
```
79+
80+
Next, locate and open the following file in your project: **./src/adaptiveCardExtensions/mediaUpload/loc/mystring.d.ts**
81+
82+
Add the following:
83+
84+
```typescript
85+
UploadPNG: string;
86+
```
87+
88+
to the `IMediaUploadAdaptiveCardExtensionStrings` interface.
89+
90+
### Add actions on the Card View
91+
92+
As mentioned earlier, on the Card View, we will add a button, which will allow the user to upload a png file when clicking the Card View.
93+
94+
Locate and open the following file in your project: **./src/adaptiveCardExtensions/mediaUpload/cardView/CardView.ts**
95+
96+
Here, replace the definition of `cardButtons` function with the following:
97+
98+
```typescript
99+
public get cardButtons(): [ICardButton] | [ICardButton, ICardButton] | undefined {
100+
return [
101+
{
102+
title: strings.UploadPNG,
103+
action: {
104+
type: 'VivaAction.SelectMedia'
105+
parameters: {
106+
mediaType: MediaType.Image
107+
}
108+
}
109+
}
110+
];
111+
}
112+
```
113+
114+
With this change, we have configured a button with label `Upload PNG file` and on click action is `VivaAction.SelectMedia`, which load the file uploader modal.
115+
116+
Next, replace the content of `onCardSelection` function with the following:
117+
118+
```typescript
119+
public get onCardSelection(): IQuickViewCardAction | IExternalLinkCardAction | undefined {
120+
return {
121+
type: 'QuickView',
122+
parameters: {
123+
view: QUICK_VIEW_REGISTRY_ID
124+
}
125+
};
126+
}
127+
```
128+
129+
This change implies that when a user clicks the Card View, then it should open a Quick View for them.
130+
131+
With the changes made so far, your Card View would look like:
132+
133+
![Card appearance after introducing the strings and changes in the card-view](./img/mediaUploadTutorialCardView.PNG)
134+
135+
### Add actions on the Quick View
136+
137+
In the Quick View, we will introduce buttons for 3 actions:
138+
139+
- Upload a png file
140+
- Upload a jpg file
141+
- Upload both a png and jpg file
142+
143+
We will first define the template of the Quick View. For this, locate and open the following file in your project: **./src/adaptiveCardExtensions/mediaUpload/quickView/template/QuickViewTemplate.json**
144+
145+
Replace the content of this file with the following:
146+
```json
147+
{
148+
"schema": "http://adaptivecards.io/schemas/adaptive-card.json",
149+
"type": "AdaptiveCard",
150+
"version": "1.2",
151+
"body": [
152+
{
153+
"type": "TextBlock",
154+
"text": "Time to load up some files!"
155+
}
156+
],
157+
"actions": [
158+
{
159+
"title": "Upload a png",
160+
"type": "VivaAction.SelectMedia",
161+
"parameters": {
162+
"mediaType": "MediaType.Image",
163+
"allowMultipleCapture": false,
164+
"supportedFileFormats": "png"
165+
}
166+
},
167+
{
168+
"title": "Upload a jpg",
169+
"type": "VivaAction.SelectMedia",
170+
"parameters": {
171+
"mediaType": "MediaType.Image",
172+
"allowMultipleCapture": false,
173+
"supportedFileFormats": "jpg"
174+
}
175+
},
176+
{
177+
"title": "Upload a png and jpg",
178+
"type": "VivaAction.SelectMedia",
179+
"parameters": {
180+
"mediaType": "MediaType.Image",
181+
"allowMultipleCapture": true,
182+
"supportedFileFormats": "png jpg"
183+
}
184+
}
185+
]
186+
}
187+
```
188+
189+
After adding these actions, your Quick View would look like:
190+
191+
![Card appearance after introducing changes in the quick-view](./img/mediaUploadTutorialQuickView.PNG)
192+
193+
### Set up the state for our Adaptive Card Extension
194+
195+
So far we have created our Card View and Quick View. If you do a `gulp serve` at this point, then you will be able to perform the actions that were described above.
196+
197+
But now, let us take it a notch higher.
198+
199+
We now wish to show all files uploaded on the Quick View when the respective actions are executed.
200+
201+
We'll need to make a few quick changes will first introduce new states. First locate and open the following file in your project: **./src/adaptiveCardExtensions/mediaUpload/MediaUploadAdaptiveCardExtension.ts**
202+
203+
Here, add the following `states` to the `IMediaUploadAdaptiveCardExtensionState` interface:
204+
205+
```typescript
206+
filesUploaded: string[];
207+
```
208+
209+
Next, in the `onInit` function, change `this.state={}` to
210+
211+
```typescript
212+
this.state = {
213+
filesUploaded: []
214+
};
215+
```
216+
217+
We will now make similar changes in Quick View as well.
218+
219+
Locate and open the following file in your project: **./src/adaptiveCardExtensions/mediaUpload/quickView/QuickView.ts**
220+
221+
Add the following properties to the `IQuickViewData` interface:
222+
223+
```typescript
224+
filesUploaded: string[];
225+
```
226+
227+
and then add the following two lines in the returned object of `data` getter:
228+
229+
```typescript
230+
filesUploaded: this.state.filesUploaded
231+
```
232+
233+
### Implement the onAction function
234+
235+
So far we have created defined our media upload action and wired in our states. Now we can finally implement the `onAction` function, which gives the ability to the Third Party Developer to decide what they wish to do with the media information uploaded.
236+
237+
For this, open the QuickView.ts file (**./src/adaptiveCardExtensions/mediaUpload/quickView/QuickView.ts**) and import the `ISelectMediaActionArguments` interface, as follows:
238+
239+
```typescript
240+
import {ISelectMediaActionArguments} from '@microsoft/sp-adaptive-card-extension-base';
241+
```
242+
243+
Finally, introduce the following `onAction` function in the QuickView class:
244+
245+
```typescript
246+
public onAction(action: ISelectMediaActionArguments): void {
247+
if (action.type === 'VivaAction.SelectMedia') {
248+
this.setState({
249+
filesUploaded: action.media
250+
});
251+
}
252+
}
253+
```
254+
Loading
Loading

0 commit comments

Comments
 (0)