Open platform to collect and prioritize feedback
enhancement: cache pending comments to prevent loses (#377)
Guilherme Oenning61d98939647c
diff --git a/Makefile b/Makefileindex d61daa23a..a9f723b9f 100644--- a/Makefile+++ b/Makefile@@ -15,9 +15,7 @@ lint-fix:# Testingtest-ui:- rm -rf ./output/public- npx tsc -p ./tsconfig.json- TZ='GMT' npx jest ./output/public+ TZ='GMT' npx jest ./publictest-server:godotenv -f .test.env go test ./..diff --git a/package-lock.json b/package-lock.jsonindex d80654e6d..7b66c4629 100644--- a/package-lock.json+++ b/package-lock.json@@ -73,6 +73,28 @@"integrity": "sha512-e74sM9W/4qqWB6D4TWV9FQk0WoHtX1X4FJpbjxucMSVJHtFjbQOH3H6yp+xno4br0AKG0wz/kPtaN599GUOvAg==","dev": true},+ "@types/jsdom": {+ "version": "11.0.4",+ "resolved": "https://registry.npmjs.org/@types/jsdom/-/jsdom-11.0.4.tgz",+ "integrity": "sha512-lthMj4kw7Fzs3LjBhQ0+1faAfDrN9GFJZO5Nf/xO7fppFfxqnnQdNR28n0xMGXsx8fTHOPliE1NTkAW1bVLpYw==",+ "dev": true,+ "requires": {+ "@types/node": "*",+ "@types/tough-cookie": "*",+ "parse5": "^3.0.2"+ },+ "dependencies": {+ "parse5": {+ "version": "3.0.3",+ "resolved": "https://registry.npmjs.org/parse5/-/parse5-3.0.3.tgz",+ "integrity": "sha512-rgO9Zg5LLLkfJF9E6CCmXlSE4UVceloys8JrFqCcHloC3usd/kJCyPDwH2SOlzix2j3xaP9sUX3e8+kvkuleAA==",+ "dev": true,+ "requires": {+ "@types/node": "*"+ }+ }+ }+ },"@types/markdown-it": {"version": "0.0.4","resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-0.0.4.tgz",@@ -119,6 +141,12 @@"integrity": "sha512-yrqQvb1EZhH+ONMzUmsEnBjzitortVv0lynRe5US2+FofdoMWUE4wf7v4peCd62fqXq8COCVTbpS1/jIg5EbuQ==","dev": true},+ "@types/tough-cookie": {+ "version": "2.3.2",+ "resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-2.3.2.tgz",+ "integrity": "sha512-vOVmaruQG5EatOU/jM6yU2uCp3Lz6mK1P5Ztu4iJjfM4SVHU9XYktPUQtKlIXuahqXHdEyUarMrBEwg5Cwu+bA==",+ "dev": true+ },"abab": {"version": "1.0.4","resolved": "https://registry.npmjs.org/abab/-/abab-1.0.4.tgz",diff --git a/package.json b/package.jsonindex 85ec60df6..c94abaefe 100644--- a/package.json+++ b/package.json@@ -14,6 +14,7 @@},"devDependencies": {"@types/jest": "22.2.3",+ "@types/jsdom": "^11.0.4","@types/markdown-it": "0.0.4","@types/node": "9.6.7","@types/react": "16.3.13",@@ -46,6 +47,7 @@"transform": {"^.+\\.(tsx|ts)?$": "ts-jest"},+ "setupTestFrameworkScriptFile": "<rootDir>/public/jest.setup.ts","testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(ts|tsx|js|jsx)$","moduleFileExtensions": ["ts",@@ -56,7 +58,7 @@"node"],"moduleNameMapper": {- "^.*\\.(png|scss)$": "<rootDir>/public/assets/jest-sass-stub.ts",+ "^.*\\.(png|scss)$": "<rootDir>/public/jest.assets.ts","@fider/(.*)": "<rootDir>/public/$1"}}diff --git a/public/assets/jest-sass-stub.ts b/public/jest.assets.tssimilarity index 100%rename from public/assets/jest-sass-stub.tsrename to public/jest.assets.tsdiff --git a/public/jest.setup.ts b/public/jest.setup.tsnew file mode 100644index 000000000..570b6224f--- /dev/null+++ b/public/jest.setup.ts@@ -0,0 +1,43 @@+import { JSDOM } from "jsdom";++const jsdom = new JSDOM("<!doctype html><html><body></body></html>");+const { window } = jsdom;++function copyProps(src: any, target: any) {+ const props = Object.getOwnPropertyNames(src)+ .filter(prop => typeof target[prop] === "undefined")+ .reduce(+ (result, prop) => ({+ ...result,+ [prop]: Object.getOwnPropertyDescriptor(src, prop)+ }),+ {}+ );+ Object.defineProperties(target, props);+}++(global as any).window = window;+(global as any).document = window.document;+(global as any).navigator = {+ userAgent: "node.js"+};++let storage: { [key: string]: string | undefined } = {};+(window as any).sessionStorage = {+ getItem: (key: string) => {+ const value = storage[key];+ return typeof value === "undefined" ? null : value;+ },+ setItem: (key: string, value: string) => {+ storage[key] = value;+ },+ removeItem: (key: string) => {+ return delete storage[key];+ }+};++beforeEach(() => {+ storage = {};+});++copyProps(window, global);diff --git a/public/pages/Home/components/IdeasContainer.tsx b/public/pages/Home/components/IdeasContainer.tsxindex 66baa3369..e716659cc 100644--- a/public/pages/Home/components/IdeasContainer.tsx+++ b/public/pages/Home/components/IdeasContainer.tsx@@ -23,7 +23,7 @@ interface IdeasContainerState {}export class IdeasContainer extends React.Component<IdeasContainerProps, IdeasContainerState> {- private timer: any;+ private timer?: number;constructor(props: IdeasContainerProps) {super(props);@@ -69,9 +69,9 @@ export class IdeasContainer extends React.Component<IdeasContainerProps, IdeasCo}private async searchIdeas(query: string, filter: string, tags: string[], delay: number = 0) {- clearTimeout(this.timer);+ window.clearTimeout(this.timer);this.setState({ loading: true });- this.timer = setTimeout(() => {+ this.timer = window.setTimeout(() => {actions.searchIdeas(query, filter, tags).then(response => {if (this.state.loading) {this.setState({ loading: false, ideas: response.data });diff --git a/public/pages/ShowIdea/components/CommentInput.tsx b/public/pages/ShowIdea/components/CommentInput.tsxindex a119a184a..0ad84bdac 100644--- a/public/pages/ShowIdea/components/CommentInput.tsx+++ b/public/pages/ShowIdea/components/CommentInput.tsx@@ -5,7 +5,7 @@ import { Idea, CurrentUser } from "@fider/models";import { Gravatar, UserName, Button, Textarea, DisplayError, SignInControl } from "@fider/components/common";import { SignInModal } from "@fider/components";-import { page, actions, Failure } from "@fider/services";+import { cache, page, actions, Failure } from "@fider/services";interface CommentInputProps {user?: CurrentUser;@@ -18,6 +18,8 @@ interface CommentInputState {showSignIn: boolean;}+const CACHE_TITLE_KEY = "CommentInput-Comment-";+export class CommentInput extends React.Component<CommentInputProps, CommentInputState> {private input!: HTMLTextAreaElement;@@ -25,11 +27,20 @@ export class CommentInput extends React.Component<CommentInputProps, CommentInpusuper(props);this.state = {- content: "",+ content: (!!this.props.user && cache.get(this.getCacheKey())) || "",showSignIn: false};}+ private getCacheKey(): string {+ return `${CACHE_TITLE_KEY}${this.props.idea.id}`;+ }++ private commentChanged = (e: React.ChangeEvent<HTMLTextAreaElement>) => {+ cache.set(this.getCacheKey(), e.currentTarget.value);+ this.setState({ content: e.currentTarget.value });+ };+private onTextFocused() {if (!this.props.user) {this.input.blur();@@ -44,6 +55,7 @@ export class CommentInput extends React.Component<CommentInputProps, CommentInpuconst result = await actions.createComment(this.props.idea.number, this.state.content);if (result.ok) {+ cache.remove(this.getCacheKey());location.reload();} else {this.setState({@@ -63,9 +75,8 @@ export class CommentInput extends React.Component<CommentInputProps, CommentInpu<DisplayError error={this.state.error} /><div className="field"><Textarea- onChange={e => {- this.setState({ content: e.currentTarget.value });- }}+ onChange={this.commentChanged}+ defaultValue={this.state.content}onFocus={() => this.onTextFocused()}inputRef={e => (this.input = e!)}rows={1}diff --git a/public/pages/ShowIdea/components/IdeaSearch.tsx b/public/pages/ShowIdea/components/IdeaSearch.tsxindex 9c1c891b2..903278442 100644--- a/public/pages/ShowIdea/components/IdeaSearch.tsx+++ b/public/pages/ShowIdea/components/IdeaSearch.tsx@@ -13,7 +13,7 @@ interface IdeaSearchState {}export class IdeaSearch extends React.Component<IdeaSearchProps, IdeaSearchState> {- private timer: number = 0;+ private timer?: number;constructor(props: IdeaSearchProps) {super(props);@@ -32,8 +32,8 @@ export class IdeaSearch extends React.Component<IdeaSearchProps, IdeaSearchState};private search = (searchQuery: string) => {- clearTimeout(this.timer);- this.timer = setTimeout(() => {+ window.clearTimeout(this.timer);+ this.timer = window.setTimeout(() => {actions.searchIdeas(searchQuery, "", []).then(res => {const ideas =this.props.exclude && this.props.exclude.length > 0diff --git a/public/services/cache.spec.ts b/public/services/cache.spec.tsnew file mode 100644index 000000000..f92a1b310--- /dev/null+++ b/public/services/cache.spec.ts@@ -0,0 +1,16 @@+import { cache } from "./cache";++test("cache starts empty", () => {+ const value = cache.get("my-key");+ expect(value).toBeNull();+});++test("can set, remove and get from cache", () => {+ cache.set("my-key", "Hello World");+ const value = cache.get("my-key");+ expect(value).toBe("Hello World");++ cache.remove("my-key");+ const newValue = cache.get("my-key");+ expect(newValue).toBeNull();+});diff --git a/public/services/utils.spec.tsx b/public/services/utils.spec.tssimilarity index 100%rename from public/services/utils.spec.tsxrename to public/services/utils.spec.ts