Add 'ReportContent' actions/reducers/selectors

This commit is contained in:
infinite-persistence 2021-03-31 15:38:22 +08:00 committed by Sean Yesmunt
parent fe885ae6de
commit bcbb1c8185
6 changed files with 119 additions and 0 deletions

9
flow-typed/reportContent.js vendored Normal file
View file

@ -0,0 +1,9 @@
// @flow
import * as ACTIONS from 'constants/action_types';
declare type ReportContentState = {
isReporting: boolean,
error: string,
};

View file

@ -319,3 +319,8 @@ export const REACTIONS_NEW_STARTED = 'REACTIONS_NEW_STARTED';
export const REACTIONS_NEW_FAILED = 'REACTIONS_NEW_FAILED'; export const REACTIONS_NEW_FAILED = 'REACTIONS_NEW_FAILED';
export const REACTIONS_LIKE_COMPLETED = 'REACTIONS_LIKE_COMPLETED'; export const REACTIONS_LIKE_COMPLETED = 'REACTIONS_LIKE_COMPLETED';
export const REACTIONS_DISLIKE_COMPLETED = 'REACTIONS_DISLIKE_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';

View file

@ -16,6 +16,7 @@ import coinSwapReducer from 'redux/reducers/coinSwap';
import searchReducer from 'redux/reducers/search'; import searchReducer from 'redux/reducers/search';
import reactionsReducer from 'redux/reducers/reactions'; import reactionsReducer from 'redux/reducers/reactions';
import syncReducer from 'redux/reducers/sync'; import syncReducer from 'redux/reducers/sync';
import reportContentReducer from 'redux/reducers/reportContent';
export default (history) => export default (history) =>
combineReducers({ combineReducers({
@ -32,6 +33,7 @@ export default (history) =>
notifications: notificationsReducer, notifications: notificationsReducer,
publish: publishReducer, publish: publishReducer,
reactions: reactionsReducer, reactions: reactionsReducer,
reportContent: reportContentReducer,
rewards: rewardsReducer, rewards: rewardsReducer,
search: searchReducer, search: searchReducer,
settings: settingsReducer, settings: settingsReducer,

View file

@ -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,
});
});
};

View file

@ -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
);

View file

@ -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);