From bc1b2fc07e839162098f023107d4397aab2fe8db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Tue, 12 Sep 2023 00:28:34 +0200 Subject: [PATCH] Move account notes to a panel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../actions/__tests__/account-notes.test.ts | 64 +------------- app/soapbox/actions/account-notes.ts | 84 +++++-------------- .../features/ui/components/modal-root.tsx | 2 - .../components/modals/account-note-modal.tsx | 67 --------------- .../ui/components/modals/join-event-modal.tsx | 6 +- .../components/panels/account-note-panel.tsx | 79 +++++++++++++++++ .../features/ui/util/async-components.ts | 8 +- app/soapbox/locales/cy.json | 2 +- app/soapbox/locales/el.json | 2 +- app/soapbox/locales/en-Shaw.json | 2 +- app/soapbox/locales/en.json | 5 +- app/soapbox/locales/es-AR.json | 2 +- app/soapbox/locales/fa.json | 2 +- app/soapbox/locales/ga.json | 2 +- app/soapbox/locales/he.json | 2 +- app/soapbox/locales/hi.json | 2 +- app/soapbox/locales/hu.json | 2 +- app/soapbox/locales/id.json | 2 +- app/soapbox/locales/io.json | 2 +- app/soapbox/locales/kk.json | 2 +- app/soapbox/locales/ko.json | 2 +- app/soapbox/locales/lt.json | 2 +- app/soapbox/locales/lv.json | 2 +- app/soapbox/locales/mk.json | 2 +- app/soapbox/locales/ms.json | 2 +- app/soapbox/locales/nn.json | 2 +- app/soapbox/locales/oc.json | 2 +- app/soapbox/locales/pt-BR.json | 2 +- app/soapbox/locales/pt.json | 2 +- app/soapbox/locales/ro.json | 2 +- app/soapbox/locales/ru.json | 2 +- app/soapbox/locales/sk.json | 2 +- app/soapbox/locales/sl.json | 2 +- app/soapbox/locales/sr-Latn.json | 2 +- app/soapbox/locales/sr.json | 2 +- app/soapbox/locales/sv.json | 2 +- app/soapbox/locales/ta.json | 2 +- app/soapbox/locales/te.json | 2 +- app/soapbox/locales/th.json | 2 +- app/soapbox/locales/uk.json | 2 +- app/soapbox/pages/profile-page.tsx | 7 ++ app/soapbox/reducers/account-notes.ts | 43 ---------- app/soapbox/reducers/index.ts | 4 +- 43 files changed, 154 insertions(+), 279 deletions(-) delete mode 100644 app/soapbox/features/ui/components/modals/account-note-modal.tsx create mode 100644 app/soapbox/features/ui/components/panels/account-note-panel.tsx delete mode 100644 app/soapbox/reducers/account-notes.ts diff --git a/app/soapbox/actions/__tests__/account-notes.test.ts b/app/soapbox/actions/__tests__/account-notes.test.ts index fdae308381..53c1c85dfc 100644 --- a/app/soapbox/actions/__tests__/account-notes.test.ts +++ b/app/soapbox/actions/__tests__/account-notes.test.ts @@ -1,19 +1,13 @@ -import { Map as ImmutableMap } from 'immutable'; - import { __stub } from 'soapbox/api'; -import { buildAccount, buildRelationship } from 'soapbox/jest/factory'; import { mockStore, rootState } from 'soapbox/jest/test-helpers'; -import { ReducerRecord, EditRecord } from 'soapbox/reducers/account-notes'; -import { changeAccountNoteComment, initAccountNoteModal, submitAccountNote } from '../account-notes'; +import { submitAccountNote } from '../account-notes'; describe('submitAccountNote()', () => { let store: ReturnType; beforeEach(() => { - const state = rootState - .set('account_notes', ReducerRecord({ edit: EditRecord({ account: '1', comment: 'hello' }) })); - store = mockStore(state); + store = mockStore(rootState); }); describe('with a successful API request', () => { @@ -26,10 +20,9 @@ describe('submitAccountNote()', () => { it('post the note to the API', async() => { const expectedActions = [ { type: 'ACCOUNT_NOTE_SUBMIT_REQUEST' }, - { type: 'MODAL_CLOSE', modalType: undefined }, { type: 'ACCOUNT_NOTE_SUBMIT_SUCCESS', relationship: {} }, ]; - await store.dispatch(submitAccountNote()); + await store.dispatch(submitAccountNote('1', 'hello')); const actions = store.getActions(); expect(actions).toEqual(expectedActions); @@ -51,59 +44,10 @@ describe('submitAccountNote()', () => { error: new Error('Network Error'), }, ]; - await store.dispatch(submitAccountNote()); + await store.dispatch(submitAccountNote('1', 'hello')); const actions = store.getActions(); expect(actions).toEqual(expectedActions); }); }); }); - -describe('initAccountNoteModal()', () => { - let store: ReturnType; - - beforeEach(() => { - const state = rootState - .set('relationships', ImmutableMap({ '1': buildRelationship({ note: 'hello' }) })); - store = mockStore(state); - }); - - it('dispatches the proper actions', async() => { - const account = buildAccount({ - id: '1', - acct: 'justin-username', - display_name: 'Justin L', - avatar: 'test.jpg', - verified: true, - }); - const expectedActions = [ - { type: 'ACCOUNT_NOTE_INIT_MODAL', account, comment: 'hello' }, - { type: 'MODAL_CLOSE', modalType: 'ACCOUNT_NOTE' }, - { type: 'MODAL_OPEN', modalType: 'ACCOUNT_NOTE' }, - ]; - await store.dispatch(initAccountNoteModal(account)); - const actions = store.getActions(); - - expect(actions).toEqual(expectedActions); - }); -}); - -describe('changeAccountNoteComment()', () => { - let store: ReturnType; - - beforeEach(() => { - const state = rootState; - store = mockStore(state); - }); - - it('dispatches the proper actions', async() => { - const comment = 'hello world'; - const expectedActions = [ - { type: 'ACCOUNT_NOTE_CHANGE_COMMENT', comment }, - ]; - await store.dispatch(changeAccountNoteComment(comment)); - const actions = store.getActions(); - - expect(actions).toEqual(expectedActions); - }); -}); diff --git a/app/soapbox/actions/account-notes.ts b/app/soapbox/actions/account-notes.ts index 691f63fc34..0194c6f81a 100644 --- a/app/soapbox/actions/account-notes.ts +++ b/app/soapbox/actions/account-notes.ts @@ -1,82 +1,44 @@ import api from '../api'; -import { openModal, closeModal } from './modals'; - import type { AxiosError } from 'axios'; import type { AnyAction } from 'redux'; -import type { Account } from 'soapbox/schemas'; -import type { AppDispatch, RootState } from 'soapbox/store'; +import type { RootState } from 'soapbox/store'; const ACCOUNT_NOTE_SUBMIT_REQUEST = 'ACCOUNT_NOTE_SUBMIT_REQUEST'; const ACCOUNT_NOTE_SUBMIT_SUCCESS = 'ACCOUNT_NOTE_SUBMIT_SUCCESS'; const ACCOUNT_NOTE_SUBMIT_FAIL = 'ACCOUNT_NOTE_SUBMIT_FAIL'; -const ACCOUNT_NOTE_INIT_MODAL = 'ACCOUNT_NOTE_INIT_MODAL'; +const submitAccountNote = (id: string, value: string) => + (dispatch: React.Dispatch, getState: () => RootState) => { + dispatch(submitAccountNoteRequest()); -const ACCOUNT_NOTE_CHANGE_COMMENT = 'ACCOUNT_NOTE_CHANGE_COMMENT'; - -const submitAccountNote = () => (dispatch: React.Dispatch, getState: () => RootState) => { - dispatch(submitAccountNoteRequest()); - - const id = getState().account_notes.edit.account; - - return api(getState) - .post(`/api/v1/accounts/${id}/note`, { - comment: getState().account_notes.edit.comment, - }) - .then(response => { - dispatch(closeModal()); - dispatch(submitAccountNoteSuccess(response.data)); - }) - .catch(error => dispatch(submitAccountNoteFail(error))); -}; - -function submitAccountNoteRequest() { - return { - type: ACCOUNT_NOTE_SUBMIT_REQUEST, + return api(getState) + .post(`/api/v1/accounts/${id}/note`, { + comment: value, + }) + .then(response => { + dispatch(submitAccountNoteSuccess(response.data)); + }) + .catch(error => dispatch(submitAccountNoteFail(error))); }; -} -function submitAccountNoteSuccess(relationship: any) { - return { - type: ACCOUNT_NOTE_SUBMIT_SUCCESS, - relationship, - }; -} +const submitAccountNoteRequest = () => ({ + type: ACCOUNT_NOTE_SUBMIT_REQUEST, +}); -function submitAccountNoteFail(error: AxiosError) { - return { - type: ACCOUNT_NOTE_SUBMIT_FAIL, - error, - }; -} +const submitAccountNoteSuccess = (relationship: any) => ({ + type: ACCOUNT_NOTE_SUBMIT_SUCCESS, + relationship, +}); -const initAccountNoteModal = (account: Account) => (dispatch: AppDispatch, getState: () => RootState) => { - const comment = getState().relationships.get(account.id)!.note; - - dispatch({ - type: ACCOUNT_NOTE_INIT_MODAL, - account, - comment, - }); - - dispatch(openModal('ACCOUNT_NOTE')); -}; - -function changeAccountNoteComment(comment: string) { - return { - type: ACCOUNT_NOTE_CHANGE_COMMENT, - comment, - }; -} +const submitAccountNoteFail = (error: AxiosError) => ({ + type: ACCOUNT_NOTE_SUBMIT_FAIL, + error, +}); export { submitAccountNote, - initAccountNoteModal, - changeAccountNoteComment, ACCOUNT_NOTE_SUBMIT_REQUEST, ACCOUNT_NOTE_SUBMIT_SUCCESS, ACCOUNT_NOTE_SUBMIT_FAIL, - ACCOUNT_NOTE_INIT_MODAL, - ACCOUNT_NOTE_CHANGE_COMMENT, }; diff --git a/app/soapbox/features/ui/components/modal-root.tsx b/app/soapbox/features/ui/components/modal-root.tsx index 08fd4c88cb..86d1b55ccf 100644 --- a/app/soapbox/features/ui/components/modal-root.tsx +++ b/app/soapbox/features/ui/components/modal-root.tsx @@ -3,7 +3,6 @@ import React from 'react'; import Base from 'soapbox/components/modal-root'; import { AccountModerationModal, - AccountNoteModal, ActionsModal, BirthdaysModal, BoostModal, @@ -50,7 +49,6 @@ import ModalLoading from './modal-loading'; /* eslint sort-keys: "error" */ const MODAL_COMPONENTS = { 'ACCOUNT_MODERATION': AccountModerationModal, - 'ACCOUNT_NOTE': AccountNoteModal, 'ACTIONS': ActionsModal, 'BIRTHDAYS': BirthdaysModal, 'BOOST': BoostModal, diff --git a/app/soapbox/features/ui/components/modals/account-note-modal.tsx b/app/soapbox/features/ui/components/modals/account-note-modal.tsx deleted file mode 100644 index 95afc614e0..0000000000 --- a/app/soapbox/features/ui/components/modals/account-note-modal.tsx +++ /dev/null @@ -1,67 +0,0 @@ -import React from 'react'; -import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; - -import { changeAccountNoteComment, submitAccountNote } from 'soapbox/actions/account-notes'; -import { closeModal } from 'soapbox/actions/modals'; -import { useAccount } from 'soapbox/api/hooks'; -import { Modal, Text } from 'soapbox/components/ui'; -import { useAppDispatch, useAppSelector } from 'soapbox/hooks'; - -const messages = defineMessages({ - placeholder: { id: 'account_note.placeholder', defaultMessage: 'No comment provided' }, - save: { id: 'account_note.save', defaultMessage: 'Save' }, -}); - -const AccountNoteModal = () => { - const intl = useIntl(); - const dispatch = useAppDispatch(); - - const isSubmitting = useAppSelector((state) => state.account_notes.edit.isSubmitting); - const accountId = useAppSelector((state) => state.account_notes.edit.account); - const { account } = useAccount(accountId || undefined); - const comment = useAppSelector((state) => state.account_notes.edit.comment); - - const onClose = () => { - dispatch(closeModal('ACCOUNT_NOTE')); - }; - - const handleCommentChange: React.ChangeEventHandler = e => { - dispatch(changeAccountNoteComment(e.target.value)); - }; - - const handleSubmit = () => { - dispatch(submitAccountNote()); - }; - - const handleKeyDown: React.KeyboardEventHandler = e => { - if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) { - handleSubmit(); - } - }; - - return ( - } - onClose={onClose} - confirmationAction={handleSubmit} - confirmationText={intl.formatMessage(messages.save)} - confirmationDisabled={isSubmitting} - > - - - - -