-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add feedback form to browser action
- Loading branch information
Showing
19 changed files
with
201 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// WIP | ||
// Will return to this file after https://github.com/lfilho/sample-webextension/issues/38 | ||
// | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
/* | ||
import fs from 'fs'; | ||
import { jest } from '@jest/globals'; | ||
import { submitFeedbackListener } from '../../../src/browser_action/feedback_sender.js'; | ||
jest.mock('../../../src/browser_action/feedback_sender.js'); | ||
const browserActionPath = `${process.cwd()}/src/browser_action/index.html`; | ||
const browserActionHtml = fs.readFileSync(browserActionPath).toString(); | ||
describe.skip('Browser Action', () => { | ||
let $, submitButton, messagesContainer; | ||
beforeEach(() => { | ||
// document.documentElement.innerHTML = browserActionHtml; | ||
$ = document.querySelector.bind(document); | ||
submitButton = $('#submit'); | ||
messagesContainer = $('#messages'); | ||
}); | ||
afterEach(() => { | ||
// restore the original func after test | ||
jest.resetModules(); | ||
}); | ||
it('button exists', (done) => { | ||
expect(submitButton).toBeTruthy(); | ||
submitButton.click(); | ||
setTimeout(() => { | ||
expect(messagesContainer.textContent).toMatch(/Thank you!/); | ||
done(); | ||
}, 5000); | ||
}); | ||
}); | ||
*/ | ||
describe.skip('Browser Action', () => { | ||
it.skip(); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { post } from '../shared/util/fake_fetch.js'; | ||
import { hasMinimalLength } from '../shared/util/validation.js'; | ||
import Logger from '../shared/util/logger.js'; | ||
import { InputTooShortError } from '../shared/model/error.js'; | ||
import { FEEDBACK_ENDPOINT } from '../shared/config.js'; | ||
|
||
const COMMENT_FIELD_SELECTOR = '#comment-field'; | ||
const MESSAGES_CONTAINER_SELECTOR = '#messages'; | ||
|
||
const $ = document.querySelector.bind(document); | ||
|
||
export async function submitFeedbackListener(event) { | ||
event.preventDefault(); | ||
|
||
try { | ||
const feedback = $(COMMENT_FIELD_SELECTOR).value; | ||
|
||
validate(feedback); | ||
|
||
showWaitingMessage(); | ||
|
||
const payload = { feedback }; | ||
const response = await post(FEEDBACK_ENDPOINT, payload); | ||
|
||
showSuccessMessage(); | ||
Logger.debug(`Feedback sent! Server returned status: ${response.status}`); | ||
} catch (error) { | ||
showErrorMessage(error.message); | ||
Logger.error(error); | ||
} | ||
} | ||
|
||
function showMessage(text, className = '') { | ||
const messagesContainer = $(MESSAGES_CONTAINER_SELECTOR); | ||
messagesContainer.className = className; | ||
messagesContainer.innerText = text; | ||
} | ||
|
||
function showSuccessMessage() { | ||
showMessage('✅ Feedback sent! Thank you!', 'success'); | ||
} | ||
|
||
function showErrorMessage(message) { | ||
showMessage(`❌ ${message}`, 'error'); | ||
} | ||
|
||
function showWaitingMessage() { | ||
showMessage('Sending...'); | ||
} | ||
|
||
function validate(feedback) { | ||
const MIN_FEEDBACK_LENGTH = 3; | ||
if (!hasMinimalLength(feedback, MIN_FEEDBACK_LENGTH)) { | ||
throw new InputTooShortError( | ||
`Feedback must have more than ${MIN_FEEDBACK_LENGTH} letters.` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link href="style.css" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<header> | ||
<img | ||
src="../icons/duck-with-shades_130.png" | ||
alt="Best Tracker Blocker extension logo" | ||
/> | ||
<h1 id="myHeading">Best Tracker Blocker</h1> | ||
</header> | ||
|
||
<form id="feedback-form"> | ||
<label for="comment-field"> | ||
Thanks for sharing your feedback with us! And don't worry: we're not | ||
sending any data apart from what you type below — you know us! 😇 | ||
</label> | ||
|
||
<textarea id="comment-field"></textarea> | ||
|
||
<input id="submit" type="button" value="Submit feedback 💬" /> | ||
</form> | ||
|
||
<footer id="messages"></footer> | ||
|
||
<script type="module" src="./index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { submitFeedbackListener } from './feedback_sender.js'; | ||
|
||
const SUBMIT_BUTTON_SELECTOR = '#submit'; | ||
const $ = document.querySelector.bind(document); | ||
$(SUBMIT_BUTTON_SELECTOR).addEventListener('click', submitFeedbackListener); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
body { | ||
padding: 2rem; | ||
max-width: 25rem; | ||
background: repeat url(../icons/duck-pattern.jpg); | ||
font-family: Arial, Helvetica, sans-serif; | ||
} | ||
|
||
h1 { | ||
font-style: italic; | ||
} | ||
|
||
header, | ||
#feedback-form { | ||
display: grid; | ||
grid-gap: 1rem; | ||
justify-items: center; | ||
} | ||
|
||
textarea { | ||
width: 100%; | ||
height: 6rem; | ||
} | ||
|
||
input { | ||
font-size: 1rem; | ||
padding: 0.5rem; | ||
} | ||
|
||
#messages { | ||
text-align: center; | ||
padding: 1rem; | ||
} | ||
#messages.error { | ||
color: darkred; | ||
} | ||
|
||
#messages.success { | ||
color: darkgreen; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const METRIC_ENDPOINT = 'https://fake--endpoint.com/metrics'; | ||
const FEEDBACK_ENDPOINT = 'https://fake--endpoint.com/feedback'; | ||
|
||
export { METRIC_ENDPOINT, FEEDBACK_ENDPOINT }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Most robust validation lib in the world, I know! | ||
export function isEmpty(string) { | ||
return !string || string.length === 0; | ||
} | ||
|
||
export function hasMinimalLength(string, minimalLength) { | ||
return !isEmpty(string) && string.length > minimalLength; | ||
} |