sp-dev-fx-webparts/samples/ace-strategy-pattern
dependabot[bot] a3c05566b7
Bump follow-redirects in /samples/ace-strategy-pattern
Bumps [follow-redirects](https://github.com/follow-redirects/follow-redirects) from 1.15.3 to 1.15.4.
- [Release notes](https://github.com/follow-redirects/follow-redirects/releases)
- [Commits](https://github.com/follow-redirects/follow-redirects/compare/v1.15.3...v1.15.4)

---
updated-dependencies:
- dependency-name: follow-redirects
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-01-11 02:44:52 +00:00
..
config action handling in aces 2022-05-27 14:52:31 +02:00
doc-resources action handling in aces 2022-05-27 14:52:31 +02:00
src action handling in aces 2022-05-27 14:52:31 +02:00
tests action handling in aces 2022-05-27 14:52:31 +02:00
.gitignore action handling in aces 2022-05-27 14:52:31 +02:00
.npmignore action handling in aces 2022-05-27 14:52:31 +02:00
.yo-rc.json action handling in aces 2022-05-27 14:52:31 +02:00
README.md Updated contributors for readmes 2023-03-15 00:11:27 -04:00
gulpfile.js action handling in aces 2022-05-27 14:52:31 +02:00
package-lock.json Bump follow-redirects in /samples/ace-strategy-pattern 2024-01-11 02:44:52 +00:00
package.json Bump postcss and @microsoft/sp-build-web 2023-10-03 19:01:25 +00:00
tsconfig.json action handling in aces 2022-05-27 14:52:31 +02:00
tslint.json action handling in aces 2022-05-27 14:52:31 +02:00

README.md

ace-strategy-pattern

Summary

This sample shows how to avoid conditions hell in quick view actions.

With more complex cards it can get extremely difficult to handle all the actions as adding more and more conditions to target action id impacts code readability.

In this sample we are exposing organization news in adaptive card. To start we introduce Next and Previous action to navigate between loaded news. We also want to add possibility to like and comment displayed news. Commenting is actually a two-action operation as first- we have to show the input and then submit value provided in the input. Finally we add support for posting this news in a Teams channel - this requires calls to get Teams, then Channels for selected Team and finally post the news in selected channel. This is how it would look like.

Note we are already abstracting all the logic in a NewsManager, which hides away significant chunk of the code.

With few simple tricks we can refactor to this

With a better isolation and testability of our actions.

Compatibility

⚠️ Important
Every SPFx version is only compatible with specific version(s) of Node.js. In order to be able to build this sample, please ensure that the version of Node on your workstation matches one of the versions listed in this section. This sample will not work on a different version of Node.
Refer to https://aka.ms/spfx-matrix for more information on SPFx compatibility.

SPFx 1.13.0 Node.js v14 | v12 Compatible with SharePoint Online Does not work with SharePoint 2019 Does not work with SharePoint 2016 (Feature Pack 2) Local Workbench Unsupported Hosted Workbench Compatible Compatible with Remote Containers

Applies to

Prerequisites

None

Contributors

Version history

Version Date Comments
1.0 May 27, 2022 Initial commit

Disclaimer

THIS CODE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.


Minimal Path to Awesome

  • Clone this repository (or download this solution as a .ZIP file then unzip it)
  • From your command line, change your current directory to the directory containing this sample (ace-strategy-pattern, located under samples)
  • in the command line run:
    • npm install
    • gulp serve
  • Add Graph Auto Batching

To run tests

  • Clone this repository (or download this solution as a .ZIP file then unzip it)
  • From your command line, change your current directory to the directory containing this sample (ace-strategy-pattern, located under samples)
  • in the command line run:
    • npm install
    • npx jest

Features

To understand this sample we need to understand the root problem. First of all, I hope we can all agree, multiple if statements have negative impact on code readability and extensibility. At some point if You need to extend such method You cannot be sure where to put new if statement and it's difficult to controls state changes. Additionally - as it's very difficult to correctly mock objects from SPFx, we hit our test coverage, making actions almost impossible to test, or making our tests performing heavy mocking on SPFx objects (such as QuickView).

To address this we need an interface that can abstract our operations. To perform any action we effectively need only action object, current state and setState method. We can pass all of this as a method arguments, which we do in src\manager\viewManager\viewActions\IViewActionHandler.ts.

To avoid SPFx objects reference let's create new interface which will represent a contract we have with SPFx Quick view: src\manager\viewManager\IView.ts

With that in place we can implement a specific ActionHandlers such as src\manager\viewManager\viewActions\NavigateAction.ts and src\manager\viewManager\viewActions\PostInTeamsAction.ts

Finally we bring it together in src\manager\viewManager\viewActions\ViewActionExecutor.ts where we register actions we want to support in our view. Everything is handled by src\manager\viewManager\NewsQuickViewManager.ts

Note how now, we can test in isolation sharing news in Teams without any dependency on SPFx tests\manager\viewManager\viewActions\PostInTeamsAction.test.ts

Hope You'll enjoy it.

Notice that better pictures and documentation will increase the sample usage and the value you are providing for others. Thanks for your submissions advance.

Share your web part with others through Microsoft 365 Patterns and Practices program to get visibility and exposure. More details on the community, open-source projects and other activities from http://aka.ms/m365pnp.

References