From fe4db30e6ddd3cd86e7fc6efa364d53801143375 Mon Sep 17 00:00:00 2001 From: Paul Mineev Date: Sun, 5 Jun 2022 15:20:20 -0700 Subject: [PATCH] show subscription buttons in simple view, add ability to hide rss button --- frontend/app/__stubs__/settings.ts | 3 - frontend/app/common/__mocks__/settings.ts | 19 -- frontend/app/common/settings.ts | 52 ++--- frontend/app/common/static-store.ts | 3 - .../comment-form/comment-form.spec.tsx | 157 ++++++++++++++ .../comment-form/comment-form.test.tsx | 195 ------------------ .../components/comment-form/comment-form.tsx | 136 ++++++------ frontend/app/components/comment/comment.tsx | 2 - frontend/app/components/root/root.tsx | 2 - frontend/app/components/textarea-autosize.tsx | 2 +- frontend/app/remark.tsx | 16 +- frontend/app/store/theme/reducers.ts | 4 +- frontend/app/utils/isUserAnonymous.ts | 2 +- .../parse-booleans-from-dictionary.test.ts | 57 ----- .../utils/parse-booleans-from-dictionary.ts | 15 -- frontend/app/utils/parse-query.ts | 7 +- frontend/jest.config.js | 1 - frontend/templates/demo.ejs | 2 +- site/src/docs/configuration/frontend/index.md | 30 +-- 19 files changed, 278 insertions(+), 427 deletions(-) delete mode 100644 frontend/app/__stubs__/settings.ts delete mode 100644 frontend/app/common/__mocks__/settings.ts create mode 100644 frontend/app/components/comment-form/comment-form.spec.tsx delete mode 100644 frontend/app/components/comment-form/comment-form.test.tsx delete mode 100644 frontend/app/utils/parse-booleans-from-dictionary.test.ts delete mode 100644 frontend/app/utils/parse-booleans-from-dictionary.ts diff --git a/frontend/app/__stubs__/settings.ts b/frontend/app/__stubs__/settings.ts deleted file mode 100644 index a8d0373b..00000000 --- a/frontend/app/__stubs__/settings.ts +++ /dev/null @@ -1,3 +0,0 @@ -jest.mock('common/settings', () => ({ - siteId: 'remark', -})); diff --git a/frontend/app/common/__mocks__/settings.ts b/frontend/app/common/__mocks__/settings.ts deleted file mode 100644 index 2be1bb6e..00000000 --- a/frontend/app/common/__mocks__/settings.ts +++ /dev/null @@ -1,19 +0,0 @@ -const settingsMock: typeof import('common/settings') = { - ...jest.requireActual('common/settings'), - siteId: 'remark', - pageTitle: 'remark test', - url: 'https://remark42.com/test', - maxShownComments: 20, - token: 'abcd', - theme: 'light', - querySettings: { - site_id: 'remark', - page_title: 'remark test', - url: 'https://remark42.com/test', - max_shown_comments: 20, - token: 'abcd', - theme: 'light', - }, -}; - -module.exports = settingsMock; diff --git a/frontend/app/common/settings.ts b/frontend/app/common/settings.ts index 851c83ee..e274964e 100644 --- a/frontend/app/common/settings.ts +++ b/frontend/app/common/settings.ts @@ -1,38 +1,28 @@ import { parseQuery } from 'utils/parse-query'; - -import type { Theme } from './types'; import { THEMES, MAX_SHOWN_ROOT_COMMENTS } from './constants'; -export interface QuerySettingsType { - site_id?: string; - page_title?: string; - url?: string; - max_shown_comments?: number; - theme: Theme; - /* used in delete users data page */ - token?: string; - show_email_subscription?: boolean; +function parseNumber(value: unknown) { + if (typeof value !== 'string') { + return undefined; + } + + const parsed = +value; + + return isNaN(parsed) ? undefined : parsed; } -export const querySettings: Partial = parseQuery(); - -if (querySettings.max_shown_comments) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - querySettings.max_shown_comments = parseInt(querySettings.max_shown_comments as any as string, 10); -} else { - querySettings.max_shown_comments = MAX_SHOWN_ROOT_COMMENTS; +function includes(coll: ReadonlyArray, el: U): el is T { + return coll.includes(el as T); } -if (!querySettings.theme || THEMES.indexOf(querySettings.theme) === -1) { - querySettings.theme = THEMES[0]; -} - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -querySettings.show_email_subscription = (querySettings.show_email_subscription as any) !== 'false'; - -export const siteId = querySettings.site_id!; -export const pageTitle = querySettings.page_title; -export const url = querySettings.url; -export const maxShownComments = querySettings.max_shown_comments; -export const token = querySettings.token!; -export const theme = querySettings.theme; +export const rawParams = parseQuery(); +export const maxShownComments = parseNumber(rawParams.max_shown_comments) ?? MAX_SHOWN_ROOT_COMMENTS; +export const isEmailSubscription = rawParams.show_email_subscription !== 'false'; +export const isRssSubscription = + rawParams.show_rss_subscription === undefined || rawParams.show_rss_subscription !== 'false'; +export const theme = (rawParams.theme = includes(THEMES, rawParams.theme) ? rawParams.theme : THEMES[0]); +export const siteId = rawParams.site_id || 'remark'; +export const pageTitle = rawParams.page_title; +export const url = rawParams.url; +export const token = rawParams.token; +export const locale = rawParams.locale || 'en'; diff --git a/frontend/app/common/static-store.ts b/frontend/app/common/static-store.ts index e93fc9cc..4558c37f 100644 --- a/frontend/app/common/static-store.ts +++ b/frontend/app/common/static-store.ts @@ -1,9 +1,7 @@ import { Config } from './types'; -import { QuerySettingsType, querySettings } from './settings'; interface StaticStoreType { config: Config; - query: QuerySettingsType; /** used in fetcher, fer example to set comment edit timeout */ serverClientTimeDiff?: number; } @@ -32,5 +30,4 @@ export const StaticStore: StaticStoreType = { telegram_bot_username: '', emoji_enabled: false, }, - query: querySettings as QuerySettingsType, }; diff --git a/frontend/app/components/comment-form/comment-form.spec.tsx b/frontend/app/components/comment-form/comment-form.spec.tsx new file mode 100644 index 00000000..60dcef71 --- /dev/null +++ b/frontend/app/components/comment-form/comment-form.spec.tsx @@ -0,0 +1,157 @@ +import '@testing-library/jest-dom'; +import { fireEvent, screen, waitFor } from '@testing-library/preact'; +import { useIntl } from 'react-intl'; + +import { render } from 'tests/utils'; +import { StaticStore } from 'common/static-store'; +import { LS_SAVED_COMMENT_VALUE } from 'common/constants'; +import * as localStorageModule from 'common/local-storage'; + +import { CommentForm, CommentFormProps, messages } from './comment-form'; + +const user: CommentFormProps['user'] = { + name: 'username', + id: 'id_1', + picture: '', + ip: '', + admin: false, + block: false, + verified: false, +}; + +function setup( + overrideProps: Partial = {}, + overrideConfig: Partial = {} +) { + Object.assign(StaticStore.config, overrideConfig); + + const props = { + mode: 'main', + theme: 'light', + onSubmit: () => Promise.resolve(), + getPreview: () => Promise.resolve(''), + user: null, + id: '1', + ...overrideProps, + } as CommentFormProps; + const CommentFormWithIntl = () => ; + + return render(); +} +describe('', () => { + afterEach(() => { + // reset textarea id in order to have `textarea_1` for every test + CommentForm.textareaId = 0; + }); + + describe('with initial comment value', () => { + afterEach(() => { + localStorage.clear(); + }); + it('should has empty value', () => { + const value = 'text'; + + localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 1: value })); + setup(); + expect(screen.getByTestId('textarea_1')).toHaveValue(value); + }); + + it('should get initial value from localStorage', () => { + const value = 'text'; + + localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 1: value })); + setup(); + expect(screen.getByTestId('textarea_1')).toHaveValue(value); + }); + it('should get initial value from props instead localStorage', () => { + const value = 'text from props'; + + localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 1: 'text from localStorage' })); + + setup({ value }); + expect(screen.getByTestId('textarea_1')).toHaveValue(value); + }); + }); + + describe('update initial value', () => { + afterEach(() => { + localStorage.clear(); + }); + it('should update value', () => { + setup(); + + fireEvent.input(screen.getByTestId('textarea_1'), { target: { value: '1' } }); + expect(localStorage.getItem(LS_SAVED_COMMENT_VALUE)).toBe('{"1":"1"}'); + + fireEvent.input(screen.getByTestId('textarea_1'), { target: { value: '11' } }); + expect(localStorage.getItem(LS_SAVED_COMMENT_VALUE)).toBe('{"1":"11"}'); + }); + + it('should clear value after send', async () => { + localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 1: 'asd' })); + const updateJsonItemSpy = jest.spyOn(localStorageModule, 'updateJsonItem'); + + setup(); + fireEvent.submit(screen.getByTestId('textarea_1')); + await waitFor(() => { + expect(updateJsonItemSpy).toHaveBeenCalled(); + }); + expect(localStorage.getItem(LS_SAVED_COMMENT_VALUE)).toBe('{}'); + }); + }); + + it(`doesn't render preview button and markdown toolbar in simple mode`, () => { + setup({ user }, { simple_view: true }); + expect(screen.queryByTestId('markdown-toolbar')).not.toBeInTheDocument(); + expect(screen.queryByText('Preview')).not.toBeInTheDocument(); + }); + + it.each` + expected | value + ${'99'} | ${'That was Wintermute, manipulating the lock the way it had manipulated the drone micro and the chassis of a gutted game console. It was chambered for .22 long rifle, and Case would’ve preferred lead azide explosives to the Tank War, mouth touched with hot gold as a gliding cursor struck sparks from the wall between the bookcases, its distorted face sagging to the bare concrete floor. Splayed in his elastic g-web, Case watched the other passengers as he made his way down Shiga from the sushi stall he cradled it in his jacket pocket. Images formed and reformed: a flickering montage of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the Japanese night like live wire voodoo and he’d cry for it, cry in his jacket pocket. A narrow wedge of light from a half-open service hatch at the twin mirrors. Still it was a square of faint light. The alarm still oscillated, louder here, the rear wall dulling the roar of the arcade showed him broken lengths of damp chipboard and the robot gardener. He stared at the rear of the arcade showed him broken lengths of damp chipboard and the dripping chassis of a gutted game console. That was Wintermute, manipulating the lock the way it had manipulated the drone micro and the chassis of a gutted game console. It was chambered for .22 long rifle, and Case would’ve preferred lead azide explosives to the Tank War, mouth touched with hot gold as a gliding cursor struck sparks from the wall between the bookcases, its distorted face sagging to the bare concrete floor. Splayed in his elastic g-web, Case watched the other passengers as he made his way down Shiga from the sushi stall he cradled it in his jacket pocket. Images formed and reformed: a flickering montage of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the Japanese night like live wire voodoo and he’d cry for it, cry in his jacket.'} + ${'0'} | ${'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestib'} + ${'-425'} | ${'All the speed he took, all the turns he’d taken and the amplified breathing of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the dark. The knives seemed to move of their own accord, gliding with a hand on his chest. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the Flatline as a construct, a hardwired ROM cassette replicating a dead man’s skills, obsessions, kneejerk responses. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the bright void beyond the chain link. Now this quiet courtyard, Sunday afternoon, this girl with a random collection of European furniture, as though Deane had once intended to use the place as his home. Now this quiet courtyard, Sunday afternoon, this girl with a ritual lack of urgency through the arcs and passes of their dance, point passing point, as the men waited for an opening. They floated in the shade beneath a bridge or overpass. A graphic representation of data abstracted from the banks of every computer in the coffin for Armitage’s call. All the speed he took, all the turns he’d taken and the amplified breathing of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the dark. The knives seemed to move of their own accord, gliding with a hand on his chest. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the Flatline as a construct, a hardwired ROM cassette replicating a dead man’s skills, obsessions, kneejerk responses. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the bright void beyond the chain link. Now this quiet courtyard, Sunday afternoon, this girl with a random collection of European furniture, as though Deane had once intended to use the place as his home. Now this quiet courtyard, Sunday afternoon, this girl with a ritual lack of urgency through the arcs and passes of their dance, point passing point, as the men waited for an opening. They floated in the shade beneath a bridge or overpass. A graphic representation of data abstracted from the banks of every computer in the coffin for Armitage’s call.'} + `('renders counter of rest symbols', async ({ value, expected }) => { + setup({ value }, { max_comment_size: 2000 }); + expect(screen.getByText(expected)).toBeInTheDocument(); + }); + + describe('when authorized', () => { + describe('with simple view', () => { + it('renders email subscription button', () => { + setup({ user }, { simple_view: true, email_notifications: true }); + expect(screen.getByText(/Subscribe by/)).toBeVisible(); + expect(screen.getByTitle('Subscribe by Email')).toBeVisible(); + }); + it('renders rss subscription button', () => { + setup({ user }, { simple_view: true }); + expect(screen.getByText(/Subscribe by/)).toBeVisible(); + expect(screen.getByTitle('Subscribe by RSS')).toBeVisible(); + }); + }); + it('renders without email subscription button when email_notifications disabled', () => { + setup({ user }, { email_notifications: false }); + expect(screen.queryByText('Subscribe by RSS')).not.toBeInTheDocument(); + }); + }); + + describe('when unauthorized', () => { + it(`doesn't email subscription button`, () => { + setup(); + expect(screen.queryByText(/Subscribe by/)).not.toBeInTheDocument(); + expect(screen.queryByTitle('Subscribe bey Email')).not.toBeInTheDocument(); + }); + + it(`doesn't render rss subscription button`, () => { + setup(); + expect(screen.queryByText(/Subscribe by/)).not.toBeInTheDocument(); + expect(screen.queryByText('Subscribe by RSS')).not.toBeInTheDocument(); + }); + + it('should show error message of image upload try by anonymous user', () => { + setup({ user: { ...user, id: 'anonymous_1' } }); + fireEvent.drop(screen.getByTestId('commentform_1')); + expect(screen.getByText(messages.anonymousUploadingDisabled.defaultMessage)).toBeInTheDocument(); + }); + }); +}); diff --git a/frontend/app/components/comment-form/comment-form.test.tsx b/frontend/app/components/comment-form/comment-form.test.tsx deleted file mode 100644 index 04c11fd7..00000000 --- a/frontend/app/components/comment-form/comment-form.test.tsx +++ /dev/null @@ -1,195 +0,0 @@ -import { shallow } from 'enzyme'; - -import { user, anonymousUser } from '__stubs__/user'; -import { StaticStore } from 'common/static-store'; -import { LS_SAVED_COMMENT_VALUE } from 'common/constants'; -import * as localStorageModule from 'common/local-storage'; -import { TextareaAutosize } from 'components/textarea-autosize'; - -import { CommentForm, CommentFormProps, messages } from './comment-form'; -import { SubscribeByEmail } from './__subscribe-by-email'; -import { IntlShape } from 'react-intl'; - -function createEvent(type: string, value: T): E { - const event = new Event(type); - - Object.defineProperty(event, 'target', { value }); - - return event as E; -} - -const DEFAULT_PROPS: Readonly> = { - mode: 'main', - theme: 'light', - onSubmit: () => Promise.resolve(), - getPreview: () => Promise.resolve(''), - user: null, - id: '1', -}; - -const intl = { - formatMessage(message: { defaultMessage: string }) { - return message.defaultMessage || ''; - }, -} as IntlShape; - -describe('', () => { - it('should shallow without control panel, preview button, and rss links in "simple view" mode', () => { - const props = { ...DEFAULT_PROPS, simpleView: true, intl }; - const wrapper = shallow(); - - expect(wrapper.exists('.comment-form__control-panel')).toEqual(false); - expect(wrapper.exists('.comment-form__button_type_preview')).toEqual(false); - expect(wrapper.exists('.comment-form__rss')).toEqual(false); - }); - - it('should be shallowed with email subscription button', () => { - StaticStore.config.email_notifications = true; - - const props = { ...DEFAULT_PROPS, user, intl }; - const wrapper = shallow(); - - expect(wrapper.exists(SubscribeByEmail)).toEqual(true); - }); - - it('should be rendered without email subscription button when email_notifications disabled', () => { - StaticStore.config.email_notifications = false; - - const props = { ...DEFAULT_PROPS, user, intl }; - const wrapper = shallow(); - - expect(wrapper.exists(SubscribeByEmail)).toEqual(false); - }); - - describe('initial value of comment', () => { - afterEach(() => { - localStorage.clear(); - }); - it('should has empty value', () => { - localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 2: 'text' })); - - const props = { ...DEFAULT_PROPS, user, intl }; - const wrapper = shallow(); - - expect(wrapper.state('text')).toBe(''); - expect(wrapper.find(TextareaAutosize).prop('value')).toBe(''); - }); - - it('should get initial value from localStorage', () => { - const COMMENT_VALUE = 'text'; - - localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 1: COMMENT_VALUE })); - - const props = { ...DEFAULT_PROPS, user, intl }; - const wrapper = shallow(); - - expect(wrapper.state('text')).toBe(COMMENT_VALUE); - expect(wrapper.find(TextareaAutosize).prop('value')).toBe(COMMENT_VALUE); - }); - - it('should get initial value from props instead localStorage', () => { - const COMMENT_VALUE = 'text from props'; - - localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 1: 'text from localStorage' })); - - const props = { ...DEFAULT_PROPS, user, intl, value: COMMENT_VALUE }; - const wrapper = shallow(); - - expect(wrapper.state('text')).toBe(COMMENT_VALUE); - expect(wrapper.find(TextareaAutosize).prop('value')).toBe(COMMENT_VALUE); - }); - }); - - describe('update value of comment in localStorage', () => { - afterEach(() => { - localStorage.clear(); - }); - it('should update value', () => { - const props = { ...DEFAULT_PROPS, user, intl }; - - const wrapper = shallow(); - const instance = wrapper.instance(); - - instance.onInput(createEvent('input', { value: '1' })); - expect(localStorage.getItem(LS_SAVED_COMMENT_VALUE)).toBe('{"1":"1"}'); - - instance.onInput(createEvent('input', { value: '11' })); - expect(localStorage.getItem(LS_SAVED_COMMENT_VALUE)).toBe('{"1":"11"}'); - }); - - it('should clear value after send', async () => { - localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ '1': 'asd' })); - const updateJsonItemSpy = jest.spyOn(localStorageModule, 'updateJsonItem'); - const props = { ...DEFAULT_PROPS, user, intl }; - - const wrapper = shallow(); - const instance = wrapper.instance(); - - await instance.send(createEvent('send', { preventDefault: () => undefined })); - expect(updateJsonItemSpy).toHaveBeenCalled(); - expect(localStorage.getItem(LS_SAVED_COMMENT_VALUE)).toBe(JSON.stringify({})); - }); - }); - - it('should show error message of image upload try by anonymous user', () => { - const props = { ...DEFAULT_PROPS, user: anonymousUser, intl }; - const wrapper = shallow(); - const instance = wrapper.instance(); - - instance.onDrop(new Event('drag') as DragEvent); - expect(wrapper.exists('.comment-form__error')).toEqual(true); - expect(wrapper.find('.comment-form__error').text()).toEqual(messages.anonymousUploadingDisabled.defaultMessage); - }); - - it('should show error message of image upload try by unauthorized user', () => { - const props = { ...DEFAULT_PROPS, intl }; - const wrapper = shallow(); - const instance = wrapper.instance(); - - instance.onDrop(new Event('drag') as DragEvent); - expect(wrapper.exists('.comment-form__error')).toEqual(true); - expect(wrapper.find('.comment-form__error').text()).toEqual(messages.unauthorizedUploadingDisabled.defaultMessage); - }); - - it('should show rest letters counter', async () => { - expect.assertions(3); - - const originalConfig = { ...StaticStore.config }; - StaticStore.config.max_comment_size = 2000; - const props = { ...DEFAULT_PROPS, intl }; - const wrapper = shallow(); - const instance = wrapper.instance(); - const text = - 'That was Wintermute, manipulating the lock the way it had manipulated the drone micro and the chassis of a gutted game console. It was chambered for .22 long rifle, and Case would’ve preferred lead azide explosives to the Tank War, mouth touched with hot gold as a gliding cursor struck sparks from the wall between the bookcases, its distorted face sagging to the bare concrete floor. Splayed in his elastic g-web, Case watched the other passengers as he made his way down Shiga from the sushi stall he cradled it in his jacket pocket. Images formed and reformed: a flickering montage of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the Japanese night like live wire voodoo and he’d cry for it, cry in his jacket pocket. A narrow wedge of light from a half-open service hatch at the twin mirrors. Still it was a square of faint light. The alarm still oscillated, louder here, the rear wall dulling the roar of the arcade showed him broken lengths of damp chipboard and the robot gardener. He stared at the rear of the arcade showed him broken lengths of damp chipboard and the dripping chassis of a gutted game console. That was Wintermute, manipulating the lock the way it had manipulated the drone micro and the chassis of a gutted game console. It was chambered for .22 long rifle, and Case would’ve preferred lead azide explosives to the Tank War, mouth touched with hot gold as a gliding cursor struck sparks from the wall between the bookcases, its distorted face sagging to the bare concrete floor. Splayed in his elastic g-web, Case watched the other passengers as he made his way down Shiga from the sushi stall he cradled it in his jacket pocket. Images formed and reformed: a flickering montage of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the Japanese night like live wire voodoo and he’d cry for it, cry in his jacket.'; - - instance.setState({ text }); - await wrapper.update(); - - expect(instance.state.text).toBe(text); - expect(wrapper.find('.comment-form__counter').exists()).toBe(true); - expect(wrapper.find('.comment-form__counter').text()).toBe('99'); - - StaticStore.config = originalConfig; - }); - - it('should show zero in rest letters counter', async () => { - expect.assertions(2); - - const originalConfig = { ...StaticStore.config }; - StaticStore.config.max_comment_size = 2000; - const props = { ...DEFAULT_PROPS, intl }; - const wrapper = shallow(); - const instance = wrapper.instance(); - const text = - 'All the speed he took, all the turns he’d taken and the amplified breathing of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the dark. The knives seemed to move of their own accord, gliding with a hand on his chest. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the Flatline as a construct, a hardwired ROM cassette replicating a dead man’s skills, obsessions, kneejerk responses. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the bright void beyond the chain link. Now this quiet courtyard, Sunday afternoon, this girl with a random collection of European furniture, as though Deane had once intended to use the place as his home. Now this quiet courtyard, Sunday afternoon, this girl with a ritual lack of urgency through the arcs and passes of their dance, point passing point, as the men waited for an opening. They floated in the shade beneath a bridge or overpass. A graphic representation of data abstracted from the banks of every computer in the coffin for Armitage’s call. All the speed he took, all the turns he’d taken and the amplified breathing of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the dark. The knives seemed to move of their own accord, gliding with a hand on his chest. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the Flatline as a construct, a hardwired ROM cassette replicating a dead man’s skills, obsessions, kneejerk responses. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the bright void beyond the chain link. Now this quiet courtyard, Sunday afternoon, this girl with a random collection of European furniture, as though Deane had once intended to use the place as his home. Now this quiet courtyard, Sunday afternoon, this girl with a ritual lack of urgency through the arcs and passes of their dance, point passing point, as the men waited for an opening. They floated in the shade beneath a bridge or overpass. A graphic representation of data abstracted from the banks of every computer in the coffin for Armitage’s call.'; - - instance.onInput(createEvent('input', { value: text })); - - await wrapper.update(); - - expect(instance.state.text).toBe(text.substr(0, StaticStore.config.max_comment_size)); - expect(wrapper.find('.comment-form__counter').text()).toBe('0'); - - StaticStore.config = originalConfig; - }); -}); diff --git a/frontend/app/components/comment-form/comment-form.tsx b/frontend/app/components/comment-form/comment-form.tsx index b447e48f..6158033c 100644 --- a/frontend/app/components/comment-form/comment-form.tsx +++ b/frontend/app/components/comment-form/comment-form.tsx @@ -4,7 +4,7 @@ import b, { Mix } from 'bem-react-helper'; import { User, Theme, Image, ApiError } from 'common/types'; import { StaticStore } from 'common/static-store'; -import { pageTitle } from 'common/settings'; +import * as settings from 'common/settings'; import { extractErrorMessageFromResponse } from 'utils/errorUtils'; import { isUserAnonymous } from 'utils/isUserAnonymous'; import { sleep } from 'utils/sleep'; @@ -21,8 +21,6 @@ import { SubscribeByRSS } from './__subscribe-by-rss'; import { MarkdownToolbar } from './markdown-toolbar'; import { TextExpander } from './text-expander'; -let textareaId = 0; - export type CommentFormProps = { id: string; user: User | null; @@ -31,14 +29,13 @@ export type CommentFormProps = { mix?: Mix; mode?: 'main' | 'edit' | 'reply'; theme: Theme; - simpleView?: boolean; autofocus?: boolean; onSubmit(text: string, pageTitle: string): Promise; getPreview(text: string): Promise; /** action on cancel. optional as root input has no cancel option */ - onCancel?: () => void; - uploadImage?: (image: File) => Promise; + onCancel?(): void; + uploadImage?(image: File): Promise; intl: IntlShape; }; @@ -101,38 +98,24 @@ export const messages = defineMessages({ export class CommentForm extends Component { /** reference to textarea element */ textareaRef = createRef(); - textareaId: string; + static textareaId = 0; + + state = { + preview: null, + isErrorShown: false, + errorMessage: null, + errorLock: false, + isDisabled: false, + text: '', + buttonText: null, + }; constructor(props: CommentFormProps) { super(props); - textareaId = textareaId + 1; - this.textareaId = `textarea_${textareaId}`; const savedComments = getJsonItem>(LS_SAVED_COMMENT_VALUE); - let text = savedComments?.[props.id] ?? ''; - - if (props.value) { - text = props.value; - } - - this.state = { - preview: null, - isErrorShown: false, - errorMessage: null, - errorLock: false, - isDisabled: false, - text, - buttonText: null, - }; - - this.getPreview = this.getPreview.bind(this); - this.onKeyDown = this.onKeyDown.bind(this); - this.onDragOver = this.onDragOver.bind(this); - this.onDrop = this.onDrop.bind(this); - this.appendError = this.appendError.bind(this); - this.uploadImage = this.uploadImage.bind(this); - this.uploadImages = this.uploadImages.bind(this); - this.onPaste = this.onPaste.bind(this); + this.state.text = props.value ?? savedComments?.[props.id] ?? ''; + CommentForm.textareaId += 1; } componentWillReceiveProps(nextProps: CommentFormProps) { @@ -161,12 +144,12 @@ export class CommentForm extends Component { ); } - onKeyDown(e: KeyboardEvent) { + onKeyDown = (e: KeyboardEvent) => { // send on cmd+enter / ctrl+enter if (e.keyCode === 13 && (e.metaKey || e.ctrlKey)) { this.send(e); } - } + }; onInput = (e: Event) => { const { value } = e.target as HTMLInputElement; @@ -190,14 +173,14 @@ export class CommentForm extends Component { }); }; - async onPaste(e: ClipboardEvent) { + onPaste = async (e: ClipboardEvent) => { if (!(e.clipboardData && e.clipboardData.files.length > 0)) { return; } e.preventDefault(); const files = Array.from(e.clipboardData.files); await this.uploadImages(files); - } + }; send = async (e: Event) => { const { text } = this.state; @@ -212,7 +195,7 @@ export class CommentForm extends Component { this.setState({ isDisabled: true, isErrorShown: false, text }); try { - await this.props.onSubmit(text, pageTitle || document.title); + await this.props.onSubmit(text, settings.pageTitle || document.title); } catch (e) { this.setState({ isDisabled: false, @@ -233,7 +216,7 @@ export class CommentForm extends Component { this.setState({ isDisabled: false, preview: null, text: '' }); }; - getPreview() { + getPreview = () => { const text = this.textareaRef.current?.value ?? this.state.text; if (!text || !text.trim()) return; @@ -246,10 +229,10 @@ export class CommentForm extends Component { .catch(() => { this.setState({ isErrorShown: true, errorMessage: null }); }); - } + }; /** appends error to input's error block */ - appendError(...errors: string[]) { + appendError = (...errors: string[]) => { if (!this.state.errorMessage) { this.setState({ errorMessage: errors.join('\n'), @@ -261,9 +244,9 @@ export class CommentForm extends Component { errorMessage: `${this.state.errorMessage}\n${errors.join('\n')}`, isErrorShown: true, }); - } + }; - onDragOver(e: DragEvent) { + onDragOver = (e: DragEvent) => { if (!this.props.user) e.preventDefault(); if (!this.props.uploadImage) return; if (StaticStore.config.max_image_size === 0) return; @@ -273,9 +256,9 @@ export class CommentForm extends Component { if (Array.from(items).filter((i) => i.kind === 'file' && ImageMimeRegex.test(i.type)).length === 0) return; e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; - } + }; - onDrop(e: DragEvent) { + onDrop = (e: DragEvent) => { const isAnonymous = this.props.user && isUserAnonymous(this.props.user); if (!this.props.user || isAnonymous) { const message = isAnonymous ? messages.anonymousUploadingDisabled : messages.unauthorizedUploadingDisabled; @@ -296,7 +279,7 @@ export class CommentForm extends Component { e.preventDefault(); this.uploadImages(data); - } + }; /** returns selection range of a textarea */ getSelection(): [number, number] { @@ -323,7 +306,7 @@ export class CommentForm extends Component { } /** wrapper with error handling for props.uploadImage */ - uploadImage(file: File): Promise { + uploadImage = (file: File): Promise => { const intl = this.props.intl; return this.props.uploadImage!(file).catch((e: ApiError | string) => { return new Error( @@ -333,10 +316,10 @@ export class CommentForm extends Component { }) ); }); - } + }; /** performs upload process */ - async uploadImages(files: File[]) { + uploadImages = async (files: File[]) => { const intl = this.props.intl; if (!this.props.uploadImage) return; if (!this.textareaRef.current) return; @@ -419,7 +402,7 @@ export class CommentForm extends Component { } this.setState({ errorLock: false, isDisabled: false, buttonText: null }); - } + }; renderMarkdownTip = () => (
@@ -437,8 +420,32 @@ export class CommentForm extends Component {
); + renderSubscribeButtons = () => { + const isEmailNotifications = StaticStore.config.email_notifications; + const isEmailSubscription = isEmailNotifications && settings.isEmailSubscription; + const { isRssSubscription } = settings; + + if (!isRssSubscription && !isEmailSubscription) { + return null; + } + + return ( + <> + {' '} + {isRssSubscription && } + {isRssSubscription && isEmailSubscription && ( + <> + {' '} + {' '} + + )} + {isEmailSubscription && } + + ); + }; + render() { - const { theme, mode, simpleView, mix, uploadImage, autofocus, user, intl } = this.props; + const { theme, mode, mix, uploadImage, autofocus, user, intl } = this.props; const { isDisabled, isErrorShown, preview, text, buttonText } = this.state; const charactersLeft = StaticStore.config.max_comment_size - text.length; const errorMessage = this.props.errorMessage || this.state.errorMessage; @@ -447,15 +454,18 @@ export class CommentForm extends Component { edit: , reply: , }; + const textareaId = `textarea_${CommentForm.textareaId}`; const label = buttonText || Labels[mode || 'main']; const placeholderMessage = intl.formatMessage(messages.placeholder); + const isSimpleView = StaticStore.config.simple_view; + return (
{ aria-label={intl.formatMessage(messages.newComment)} onDragOver={this.onDragOver} onDrop={this.onDrop} + data-testid={`commentform_${this.props.id}`} > - {!simpleView && ( -
+ {!isSimpleView && ( +
)}
{ {user ? ( <>
- {!simpleView && ( + {!isSimpleView && (
- {!simpleView && mode === 'main' && ( + {mode === 'main' && (
{this.renderMarkdownTip()} - {' '} - - {StaticStore.config.email_notifications && StaticStore.query.show_email_subscription && ( - <> - {' '} - - - )} + {this.renderSubscribeButtons()}
)} diff --git a/frontend/app/components/comment/comment.tsx b/frontend/app/components/comment/comment.tsx index ecf9bff7..02f553be 100644 --- a/frontend/app/components/comment/comment.tsx +++ b/frontend/app/components/comment/comment.tsx @@ -525,7 +525,6 @@ export class Comment extends Component { getPreview={this.props.getPreview!} autofocus={true} uploadImage={uploadImageHandler} - simpleView={StaticStore.config.simple_view} /> )} @@ -544,7 +543,6 @@ export class Comment extends Component { errorMessage={state.editDeadline === undefined ? intl.formatMessage(messages.expiredTime) : undefined} autofocus={true} uploadImage={uploadImageHandler} - simpleView={StaticStore.config.simple_view} /> )} diff --git a/frontend/app/components/root/root.tsx b/frontend/app/components/root/root.tsx index 1a490001..2f0801eb 100644 --- a/frontend/app/components/root/root.tsx +++ b/frontend/app/components/root/root.tsx @@ -10,7 +10,6 @@ import type { StoreState } from 'store'; import { COMMENT_NODE_CLASSNAME_PREFIX, MAX_SHOWN_ROOT_COMMENTS, THEMES, IS_MOBILE } from 'common/constants'; import { maxShownComments, url } from 'common/settings'; -import { StaticStore } from 'common/static-store'; import { setUser, fetchUser, @@ -250,7 +249,6 @@ export class Root extends Component { onSubmit={(text: string, title: string) => this.props.addComment(text, title)} getPreview={this.props.getPreview} uploadImage={imageUploadHandler} - simpleView={StaticStore.config.simple_view} /> )} {this.props.pinnedComments.length > 0 && ( diff --git a/frontend/app/components/textarea-autosize.tsx b/frontend/app/components/textarea-autosize.tsx index 9ee841f7..f9d57540 100644 --- a/frontend/app/components/textarea-autosize.tsx +++ b/frontend/app/components/textarea-autosize.tsx @@ -25,5 +25,5 @@ export const TextareaAutosize = forwardRef(({ onInpu autoResize(ref.current); }, [value, ref]); - return