Skip to content

Commit ce8eb46

Browse files
WEBDEV-7354 Add Recaptcha to review form
* Draft recaptcha implementation * Tidy up hidden inputs and error handling * Clarify tests and comment re: hidden action input * Update to prod recaptcha manager * Tidy up following CR * Disable submit button if recaptcha not set up * Add error styling and improve error handling * Experiment with error handling for bad site-key * Update error handling * Adjust error handling and testing * Remove test logging * Update default hostname * Add recaptcha bypass option * Tidy up bypass implementation * Add /demo to deploy comment * Tidy up following CR * Adjust default key and error messaging
1 parent 6b5543a commit ce8eb46

10 files changed

Lines changed: 429 additions & 47 deletions

.github/workflows/pr-preview.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ on:
1414

1515
concurrency: preview-${{ github.ref }}
1616

17+
env:
18+
PREVIEW_BRANCH: gh-pages
1719
jobs:
1820
deploy-preview:
1921
runs-on: ubuntu-latest
2022
steps:
2123
- name: Checkout
2224
uses: actions/checkout@v4
2325
- uses: actions/setup-node@v4
26+
with:
27+
node-version: latest
28+
2429

2530
- name: Install and Build
2631
run: |
@@ -30,6 +35,29 @@ jobs:
3035
# Reference: https://github.com/rossjrw/pr-preview-action
3136
- name: Deploy preview
3237
uses: rossjrw/pr-preview-action@v1
38+
id: preview-step
3339
with:
3440
source-dir: ./ghpages/
3541
umbrella-dir: pr
42+
preview-branch: ${{ env.PREVIEW_BRANCH }}
43+
comment: false
44+
45+
- uses: marocchino/sticky-pull-request-comment@v2
46+
if: steps.preview-step.outputs.deployment-action == 'deploy'
47+
with:
48+
header: pr-preview
49+
message: |
50+
[PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }}
51+
:---:
52+
| <p></p> :rocket: View preview at <br> ${{ steps.preview-step.outputs.preview-url }}demo/ <br><br>
53+
| <h6>Built to branch [`${{ env.PREVIEW_BRANCH }}`](${{ github.server_url }}/${{ github.repository }}/tree/${{ env.PREVIEW_BRANCH }}) at ${{ steps.preview-step.outputs.action-start-time }}. <br> Preview will be ready when the [GitHub Pages deployment](${{ github.server_url }}/${{ github.repository }}/deployments) is complete. <br><br> </h6>
54+
55+
- uses: marocchino/sticky-pull-request-comment@v2
56+
if: steps.preview-step.outputs.deployment-action == 'remove'
57+
with:
58+
header: pr-preview
59+
message: |
60+
[PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }}
61+
:---:
62+
Preview removed because the pull request was closed.
63+
${{ steps.preview-step.outputs.action-start-time }}

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ npm install
4646
```bash
4747
npm run test
4848
```
49+
### Local Demo
50+
```bash
51+
npm run start
52+
```
53+
**Note:** If you'd like ReCaptcha to work in your dev environment, you'll want to temporarily replace the `defaultSiteKey` in `app-root.ts` with the current IA site key. If you don't know what that is, ask in the UX team channel.
4954

5055
### Watch mode
5156
```bash

demo/app-root.ts

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,83 @@
1-
import { css, html, LitElement } from 'lit';
2-
import { customElement } from 'lit/decorators.js';
1+
import { css, html, LitElement, nothing } from 'lit';
2+
import { customElement, state } from 'lit/decorators.js';
33

44
import { Review } from '@internetarchive/metadata-service';
55
import '../src/review-form';
6-
7-
const mockOldReview = new Review({
8-
stars: 3,
9-
reviewtitle: 'What a cool book!',
10-
reviewbody: 'I loved it.',
11-
reviewer: 'foo-bar',
12-
reviewdate: '2025-03-03 18:13:36',
13-
createdate: '2025-02-25 14:28:19',
14-
});
6+
import {
7+
RecaptchaManager,
8+
RecaptchaManagerInterface,
9+
} from '@internetarchive/recaptcha-manager';
1510

1611
@customElement('app-root')
1712
export class AppRoot extends LitElement {
13+
private mockOldReview = new Review({
14+
stars: 3,
15+
reviewtitle: 'What a cool book!',
16+
reviewbody: 'I loved it.',
17+
reviewer: 'foo-bar',
18+
reviewdate: '2025-03-03 18:13:36',
19+
createdate: '2025-02-25 14:28:19',
20+
});
21+
22+
private goodRecaptchaManager: RecaptchaManagerInterface =
23+
new RecaptchaManager({
24+
defaultSiteKey: 'demo-key',
25+
});
26+
27+
private badRecaptchaManager: RecaptchaManagerInterface = new RecaptchaManager(
28+
{
29+
defaultSiteKey: 'i-am-a-bad-key-that-will-fail',
30+
},
31+
);
32+
33+
private errors: string[] = [
34+
"Sorry, we couldn't submit your review.",
35+
'Write a better one.',
36+
];
37+
38+
@state()
39+
private recaptchaManager?: RecaptchaManagerInterface;
40+
41+
@state()
42+
private bypassRecaptcha: boolean = false;
43+
44+
@state()
45+
private showErrors: boolean = false;
46+
1847
render() {
19-
return html`
48+
return html`${!this.recaptchaManager
49+
? html`<button
50+
@click=${() =>
51+
(this.recaptchaManager = this.goodRecaptchaManager)}
52+
>
53+
Turn on ReCaptcha (good site key)</button
54+
><button
55+
@click=${() => (this.recaptchaManager = this.badRecaptchaManager)}
56+
>
57+
Turn on ReCaptcha (bad site key)
58+
</button> `
59+
: nothing}
60+
<button @click=${() => (this.bypassRecaptcha = !this.bypassRecaptcha)}>
61+
${!this.bypassRecaptcha ? 'Bypass' : 'Enable'} ReCaptcha
62+
</button>
63+
<button @click=${() => (this.showErrors = !this.showErrors)}>
64+
${this.showErrors ? 'Hide' : 'Show'} Pre-filled Errors
65+
</button>
2066
<div class="container">
2167
<ia-review-form
2268
.identifier=${'goody'}
23-
.oldReview=${mockOldReview}
69+
.oldReview=${this.mockOldReview}
70+
.recaptchaManager=${this.recaptchaManager}
71+
.prefilledErrors=${this.showErrors ? this.errors : []}
72+
?bypassRecaptcha=${this.bypassRecaptcha}
2473
></ia-review-form>
25-
</div>
26-
`;
74+
</div>`;
2775
}
2876

2977
static styles = css`
3078
.container {
3179
max-width: 750px;
32-
margin: 0 auto;
80+
margin: 10px auto;
3381
}
3482
`;
3583
}

package-lock.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
},
6868
"dependencies": {
6969
"@internetarchive/metadata-service": "^1.0.3",
70+
"@internetarchive/recaptcha-manager": "^1.0.0",
7071
"@lit/localize": "^0.12.2",
7172
"lit": "^2.8.0"
7273
}

0 commit comments

Comments
 (0)