Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Persist Title and Description on session storage (#183)

Guilherme Oenningdb7c4daab9ca
diff --git a/public/components/IdeaInput.tsx b/public/components/IdeaInput.tsx
index edad97ed0..543c2ea82 100644
--- a/public/components/IdeaInput.tsx
+++ b/public/components/IdeaInput.tsx
@@ -17,6 +17,9 @@ interface IdeaInputState {
focused: boolean;
}
+const CACHE_TITLE_KEY = 'IdeaInput-Title';
+const CACHE_DESCRIPTION_KEY = 'IdeaInput-Description';
+
export class IdeaInput extends React.Component<IdeaInputProps, IdeaInputState> {
private title: HTMLInputElement;
private form: Form;
@@ -32,8 +35,8 @@ export class IdeaInput extends React.Component<IdeaInputProps, IdeaInputState> {
super();
this.user = this.session.getCurrentUser();
this.state = {
- title: '',
- description: '',
+ title: this.session.getCache(CACHE_TITLE_KEY) || '',
+ description: this.session.getCache(CACHE_DESCRIPTION_KEY) || '',
focused: false
};
}
@@ -51,10 +54,21 @@ export class IdeaInput extends React.Component<IdeaInputProps, IdeaInputState> {
}
}
+ private onTitleChanged(title: string) {
+ this.session.setCache(CACHE_TITLE_KEY, title);
+ this.setState({ title });
+ }
+
+ private onDescriptionChanged(description: string) {
+ this.session.setCache(CACHE_DESCRIPTION_KEY, description);
+ this.setState({ description });
+ }
+
private async submit(event: ButtonClickEvent) {
if (this.state.title) {
const result = await this.service.addIdea(this.state.title, this.state.description);
if (result.ok) {
+ this.session.removeCache(CACHE_TITLE_KEY, CACHE_DESCRIPTION_KEY);
this.form.clearFailure();
location.href = `/ideas/${result.data.number}/${result.data.slug}`;
event.preventEnable();
@@ -69,7 +83,11 @@ export class IdeaInput extends React.Component<IdeaInputProps, IdeaInputState> {
const details = (
<div>
<div className="field">
- <Textarea onChange={(e) => this.setState({ description: e.currentTarget.value })} placeholder="Describe your idea" />
+ <Textarea
+ onChange={(e) => this.onDescriptionChanged(e.currentTarget.value)}
+ defaultValue={this.state.description}
+ placeholder="Describe your idea"
+ />
</div>
<Button className={buttonCss} onClick={(e) => this.submit(e)}>
Submit
@@ -85,7 +103,8 @@ export class IdeaInput extends React.Component<IdeaInputProps, IdeaInputState> {
ref={(e) => this.title = e!}
onFocus={() => this.onTitleFocused()}
maxLength={100}
- onKeyUp={(e) => { this.setState({ title: e.currentTarget.value }); }}
+ defaultValue={this.state.title}
+ onChange={(e) => this.onTitleChanged(e.currentTarget.value)}
placeholder={this.props.placeholder}
/>
{this.state.title && details}
diff --git a/public/services/Session.ts b/public/services/Session.ts
index 0b09395ab..54f1ef7c8 100644
--- a/public/services/Session.ts
+++ b/public/services/Session.ts
@@ -12,6 +12,9 @@ export interface Session {
getAppSettings(): AppSettings;
isSingleHostMode(): boolean;
isProduction(): boolean;
+ setCache(key: string, value: string): void;
+ getCache(key: string): string | null;
+ removeCache(...key: string[]): void;
}
@injectable()
@@ -71,4 +74,25 @@ export class BrowserSession implements Session {
public isProduction(): boolean {
return this.getAppSettings().environment.toLowerCase() === 'production';
}
+
+ public setCache(key: string, value: string): void {
+ if (this.w.sessionStorage) {
+ this.w.sessionStorage.setItem(key, value);
+ }
+ }
+
+ public getCache(key: string): string | null {
+ if (this.w.sessionStorage) {
+ return this.w.sessionStorage.getItem(key);
+ }
+ return null;
+ }
+
+ public removeCache(...keys: string[]): void {
+ if (this.w.sessionStorage && keys) {
+ for (const key of keys) {
+ return this.w.sessionStorage.removeItem(key);
+ }
+ }
+ }
}
diff --git a/tests/lib/components.ts b/tests/lib/components.ts
index 80199a8af..1d3c49607 100644
--- a/tests/lib/components.ts
+++ b/tests/lib/components.ts
@@ -1,4 +1,4 @@
-import { WebElementPromise } from 'selenium-webdriver';
+import { WebElementPromise, Key } from 'selenium-webdriver';
export class WebComponent {
constructor(protected element: WebElementPromise, public selector: string) { }
@@ -24,6 +24,9 @@ export class WebComponent {
}
public async getText() {
+ if (await this.element.getTagName() === 'input') {
+ return await this.element.getAttribute('value');
+ }
return await this.element.getText();
}
}
@@ -47,7 +50,15 @@ export class TextInput extends WebComponent {
super(element, selector);
}
- public type(text: string) {
- return this.element.sendKeys(text);
+ public async type(text: string) {
+ await this.element.sendKeys(text);
+ }
+
+ public async clear() {
+ const text = await this.getText();
+ for (const char of text) {
+ await this.element.sendKeys(Key.ARROW_RIGHT);
+ await this.element.sendKeys(Key.BACK_SPACE);
+ }
}
}
diff --git a/tests/testcases/SubmitIdeas.ts b/tests/testcases/SubmitIdeas.ts
index bd2566ed2..e7a4548f0 100644
--- a/tests/testcases/SubmitIdeas.ts
+++ b/tests/testcases/SubmitIdeas.ts
@@ -1,4 +1,4 @@
-import { ensure, elementIsVisible, mailgun } from '../lib';
+import { ensure, elementIsVisible, elementIsNotVisible, mailgun, delay } from '../lib';
import { pages, tenant, browser } from '../context';
import { createTenant } from './setup';
@@ -21,6 +21,36 @@ describe('Submit ideas', () => {
await ensure(pages.home.UserName).textIs('JON SNOW');
});
+ it('User doesn\'t lose what they typed', async () => {
+ // Action
+ await pages.home.navigate();
+ await pages.home.IdeaTitle.type('My Great Idea');
+ await pages.home.IdeaDescription.type('With an awesome description');
+
+ await pages.home.IdeaTitle.clear();
+ await pages.home.IdeaTitle.type('My Great Idea has a new title');
+
+ // Assert
+ await ensure(pages.home.IdeaTitle).textIs('My Great Idea has a new title');
+ await ensure(pages.home.IdeaDescription).textIs('With an awesome description');
+
+ // Action
+ await pages.home.navigate();
+
+ // Assert
+ await ensure(pages.home.IdeaTitle).textIs('My Great Idea has a new title');
+ await ensure(pages.home.IdeaDescription).textIs('With an awesome description');
+
+ // Action
+ await pages.home.IdeaDescription.clear();
+ await pages.home.IdeaTitle.clear();
+ await pages.home.navigate();
+
+ // Assert
+ await ensure(pages.home.IdeaTitle).textIs('');
+ await browser.wait(elementIsNotVisible(() => pages.home.IdeaDescription));
+ });
+
it('Unauthenticated cannot submit ideas', async () => {
// Action
await pages.home.navigate();