From bcbb1c8185bae42d37e9681e6025cf5ca533e9ff Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Wed, 31 Mar 2021 15:38:22 +0800 Subject: [PATCH] Add 'ReportContent' actions/reducers/selectors --- flow-typed/reportContent.js | 9 +++++ ui/constants/action_types.js | 5 +++ ui/reducers.js | 2 + ui/redux/actions/reportContent.js | 61 +++++++++++++++++++++++++++++ ui/redux/reducers/reportContent.js | 29 ++++++++++++++ ui/redux/selectors/reportContent.js | 13 ++++++ 6 files changed, 119 insertions(+) create mode 100644 flow-typed/reportContent.js create mode 100644 ui/redux/actions/reportContent.js create mode 100644 ui/redux/reducers/reportContent.js create mode 100644 ui/redux/selectors/reportContent.js diff --git a/flow-typed/reportContent.js b/flow-typed/reportContent.js new file mode 100644 index 000000000..f9ed98730 --- /dev/null +++ b/flow-typed/reportContent.js @@ -0,0 +1,9 @@ +// @flow +import * as ACTIONS from 'constants/action_types'; + +declare type ReportContentState = { + isReporting: boolean, + error: string, +}; + + diff --git a/ui/constants/action_types.js b/ui/constants/action_types.js index 32c272fae..cdf06dd79 100644 --- a/ui/constants/action_types.js +++ b/ui/constants/action_types.js @@ -319,3 +319,8 @@ export const REACTIONS_NEW_STARTED = 'REACTIONS_NEW_STARTED'; export const REACTIONS_NEW_FAILED = 'REACTIONS_NEW_FAILED'; export const REACTIONS_LIKE_COMPLETED = 'REACTIONS_LIKE_COMPLETED'; export const REACTIONS_DISLIKE_COMPLETED = 'REACTIONS_DISLIKE_COMPLETED'; + +// Report Content +export const REPORT_CONTENT_STARTED = 'REPORT_CONTENT_STARTED'; +export const REPORT_CONTENT_COMPLETED = 'REPORT_CONTENT_COMPLETED'; +export const REPORT_CONTENT_FAILED = 'REPORT_CONTENT_FAILED'; diff --git a/ui/reducers.js b/ui/reducers.js index c882530da..5da2d11cd 100644 --- a/ui/reducers.js +++ b/ui/reducers.js @@ -16,6 +16,7 @@ import coinSwapReducer from 'redux/reducers/coinSwap'; import searchReducer from 'redux/reducers/search'; import reactionsReducer from 'redux/reducers/reactions'; import syncReducer from 'redux/reducers/sync'; +import reportContentReducer from 'redux/reducers/reportContent'; export default (history) => combineReducers({ @@ -32,6 +33,7 @@ export default (history) => notifications: notificationsReducer, publish: publishReducer, reactions: reactionsReducer, + reportContent: reportContentReducer, rewards: rewardsReducer, search: searchReducer, settings: settingsReducer, diff --git a/ui/redux/actions/reportContent.js b/ui/redux/actions/reportContent.js new file mode 100644 index 000000000..f91f19dc1 --- /dev/null +++ b/ui/redux/actions/reportContent.js @@ -0,0 +1,61 @@ +// @flow +import * as ACTIONS from 'constants/action_types'; +import { COPYRIGHT_ISSUES, OTHER_LEGAL_ISSUES } from 'constants/report_content'; + +type Dispatch = (action: any) => any; + +export const doReportContent = (category: string, params: string) => (dispatch: Dispatch) => { + dispatch({ + type: ACTIONS.REPORT_CONTENT_STARTED, + }); + + let REPORT_URL; + switch (category) { + case COPYRIGHT_ISSUES: + REPORT_URL = 'https://reports.lbry.com/copyright_issue/new'; + break; + case OTHER_LEGAL_ISSUES: + REPORT_URL = 'https://reports.lbry.com/other_legal_issue/new'; + break; + default: + REPORT_URL = 'https://reports.lbry.com/common/new'; + break; + } + + fetch(`${REPORT_URL}?${params}`, { method: 'POST' }) + .then((response) => { + if (response) { + response + .json() + .then((json) => { + if (json.success) { + dispatch({ + type: ACTIONS.REPORT_CONTENT_COMPLETED, + }); + } else { + dispatch({ + type: ACTIONS.REPORT_CONTENT_FAILED, + data: json.error, + }); + } + }) + .catch((err) => { + dispatch({ + type: ACTIONS.REPORT_CONTENT_FAILED, + data: 'Server error: Invalid response', + }); + }); + } else { + dispatch({ + type: ACTIONS.REPORT_CONTENT_FAILED, + data: 'Server error: No response', + }); + } + }) + .catch((err) => { + dispatch({ + type: ACTIONS.REPORT_CONTENT_FAILED, + data: err, + }); + }); +}; diff --git a/ui/redux/reducers/reportContent.js b/ui/redux/reducers/reportContent.js new file mode 100644 index 000000000..550d8eca8 --- /dev/null +++ b/ui/redux/reducers/reportContent.js @@ -0,0 +1,29 @@ +// @flow +import * as ACTIONS from 'constants/action_types'; +import { handleActions } from 'util/redux-utils'; + +const defaultState: ReportContentState = { + isReporting: false, + error: '', +}; + +export default handleActions( + { + [ACTIONS.REPORT_CONTENT_STARTED]: (state: ReportContentState): ReportContentState => ({ + ...state, + isReporting: true, + error: '', + }), + [ACTIONS.REPORT_CONTENT_COMPLETED]: (state: ReportContentState): ReportContentState => ({ + ...state, + isReporting: false, + error: '', + }), + [ACTIONS.REPORT_CONTENT_FAILED]: (state: ReportContentState, action): ReportContentState => ({ + ...state, + isReporting: false, + error: action.data, + }), + }, + defaultState +); diff --git a/ui/redux/selectors/reportContent.js b/ui/redux/selectors/reportContent.js new file mode 100644 index 000000000..c3ac99439 --- /dev/null +++ b/ui/redux/selectors/reportContent.js @@ -0,0 +1,13 @@ +// @flow +import { createSelector } from 'reselect'; + +type State = { reportContent: ReportContentState }; + +export const selectState = (state: State): ReportContentState => state.reportContent; + +export const selectIsReportingContent: (state: State) => boolean = createSelector( + selectState, + (state) => state.isReporting +); + +export const selectReportContentError: (state: State) => string = createSelector(selectState, (state) => state.error);