strapi-plugin-comments 2.0.0-beta.1 → 2.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@ import pluginId from './pluginId';
4
4
  import Initializer from './components/Initializer';
5
5
  import PluginIcon from './components/PluginIcon';
6
6
  import pluginPermissions from './permissions';
7
+ import reducers from './reducers';
7
8
 
8
9
  const name = pluginPkg.strapi.name;
9
10
 
@@ -24,6 +25,7 @@ export default {
24
25
  },
25
26
  permissions: pluginPermissions,
26
27
  });
28
+ app.addReducers(reducers);
27
29
  app.registerPlugin({
28
30
  id: pluginId,
29
31
  initializer: Initializer,
@@ -33,10 +35,10 @@ export default {
33
35
  },
34
36
 
35
37
  bootstrap(app) {
36
- app.injectContentManagerComponent('editView', 'informations', {
37
- name: 'comments-link',
38
- Component: () => 'TODO: Comments count',
39
- });
38
+ // app.injectContentManagerComponent('editView', 'informations', {
39
+ // name: 'comments-link',
40
+ // Component: () => 'TODO: Comments count',
41
+ // });
40
42
  },
41
43
 
42
44
  async registerTrads({ locales }) {
@@ -5,16 +5,44 @@
5
5
  *
6
6
  */
7
7
 
8
- import React from 'react';
8
+ import React, { useReducer } from 'react';
9
9
  import { Switch, Route } from 'react-router-dom';
10
- import { NotFound } from '@strapi/helper-plugin';
10
+ import { useQuery } from 'react-query';
11
+ import {
12
+ NotFound,
13
+ LoadingIndicatorPage,
14
+ useNotification } from '@strapi/helper-plugin';
15
+
11
16
  import ComingSoonPage from '../ComingSoonPage';
12
17
  import getUrl from '../../utils/getUrl';
13
18
  import Discover from '../Discover';
14
19
  import Details from '../Details';
15
-
20
+ import { fetchConfig } from './utils/api';
21
+ import initReducer from './reducer/init';
22
+ import reducer, { initialState } from './reducer';
23
+ import { setConfig } from './reducer/actions';
16
24
 
17
25
  const App = () => {
26
+
27
+ const toggleNotification = useNotification();
28
+ const [reducerState, dispatch] = useReducer(reducer, initialState, () =>
29
+ initReducer(initialState)
30
+ );
31
+
32
+ const { isLoading, isFetching } = useQuery(
33
+ 'get-config', () => fetchConfig(toggleNotification),
34
+ {
35
+ initialData: { },
36
+ onSuccess: (response) => {
37
+ dispatch(setConfig(response));
38
+ }
39
+ }
40
+ );
41
+
42
+ if (isLoading || isFetching) {
43
+ return (<LoadingIndicatorPage />)
44
+ }
45
+
18
46
  return (
19
47
  <div>
20
48
  <Switch>
@@ -0,0 +1,8 @@
1
+ import { GET_CONFIG, SET_CONFIG } from "./constants";
2
+
3
+ export const setConfig = data => ({
4
+ data,
5
+ type: SET_CONFIG,
6
+ });
7
+
8
+ export const getConfig = () => ({ type: GET_CONFIG });
@@ -0,0 +1,4 @@
1
+ export const REDUCER_NAME = 'plugin-comments_app';
2
+
3
+ export const SET_CONFIG = 'SET_CONFIG';
4
+ export const GET_CONFIG = 'GET_CONFIG';
@@ -0,0 +1,27 @@
1
+ import produce from 'immer'; // current
2
+ import { set } from 'lodash';
3
+ import { GET_CONFIG, SET_CONFIG } from './constants';
4
+
5
+ const initialState = {
6
+ config: {},
7
+ };
8
+
9
+ const reducer = (state = initialState, action) =>
10
+ // eslint-disable-next-line consistent-return
11
+ produce(state, draftState => {
12
+ switch (action.type) {
13
+ case SET_CONFIG: {
14
+ set(draftState, 'config', action.data);
15
+ break;
16
+ }
17
+ case GET_CONFIG: {
18
+ return draftState?.config;
19
+ }
20
+ default:
21
+ console.log('draftState', draftState);
22
+ return draftState;
23
+ }
24
+ });
25
+
26
+ export default reducer;
27
+ export { initialState };
@@ -0,0 +1,7 @@
1
+ const initReducer = (initialState) => {
2
+ return {
3
+ ...initialState,
4
+ };
5
+ };
6
+
7
+ export default initReducer;
@@ -0,0 +1,35 @@
1
+
2
+
3
+ import { createSelector } from 'reselect';
4
+ import { initialState } from './';
5
+ import { REDUCER_NAME } from './constants';
6
+
7
+ /**
8
+ * Direct selector to the listView state domain
9
+ */
10
+ const appView = () => state => state[REDUCER_NAME] || initialState;
11
+
12
+ /**
13
+ * Other specific selectors
14
+ */
15
+
16
+ /**
17
+ * Default selector used by listView
18
+ */
19
+
20
+ const makeAppView = () =>
21
+ createSelector(
22
+ appView(),
23
+ substate => {
24
+ return substate;
25
+ }
26
+ );
27
+
28
+ const selectConfig = state => {
29
+ const { config } = state[REDUCER_NAME];
30
+
31
+ return config;
32
+ };
33
+
34
+ export default makeAppView;
35
+ export { appView, selectConfig };
@@ -0,0 +1,20 @@
1
+ import { stringify } from 'qs';
2
+ import { isEmpty } from 'lodash';
3
+
4
+ import { getApiURL, axiosInstance } from '../../../utils';
5
+
6
+ // eslint-disable-next-line import/prefer-default-export
7
+ export const fetchConfig = async (toggleNotification) => {
8
+ try {
9
+ const { data } = await axiosInstance.get(getApiURL(`moderate/config`));
10
+
11
+ return data;
12
+ } catch (err) {
13
+ toggleNotification({
14
+ type: 'warning',
15
+ message: { id: 'notification.error' },
16
+ });
17
+
18
+ throw new Error('error');
19
+ }
20
+ };
@@ -5,6 +5,9 @@
5
5
  */
6
6
 
7
7
  import React, { memo, useRef, useMemo } from 'react';
8
+ import { connect } from 'react-redux';
9
+ import { bindActionCreators, compose } from 'redux';
10
+ import isEqual from 'react-fast-compare';
8
11
  import { useIntl } from 'react-intl';
9
12
  import { useQuery } from 'react-query';
10
13
  import { useHistory } from 'react-router-dom';
@@ -41,8 +44,10 @@ import { COMMENT_STATUS } from '../../utils/constants';
41
44
  import TablePagination from '../../components/TablePagination';
42
45
  import filtersSchema from './utils/filtersSchema';
43
46
  import TableFilters from '../../components/TableFilters';
47
+ import appReducer from '../App/reducer';
48
+ import makeAppView, { selectConfig } from '../App/reducer/selectors';
44
49
 
45
- const Discover = () => {
50
+ const Discover = ({ config }) => {
46
51
  useFocusWhenNavigate();
47
52
 
48
53
  const { push } = useHistory();
@@ -182,4 +187,17 @@ const Discover = () => {
182
187
  </Box>;
183
188
  }
184
189
 
185
- export default memo(Discover);
190
+ const mapStateToProps = makeAppView();
191
+
192
+ export function mapDispatchToProps(dispatch) {
193
+ return bindActionCreators(
194
+ { },
195
+ dispatch
196
+ );
197
+ }
198
+ const withConnect = connect(
199
+ mapStateToProps,
200
+ mapDispatchToProps
201
+ );
202
+
203
+ export default compose(withConnect)(memo(Discover, isEqual));
@@ -6,9 +6,6 @@ import { getApiURL, axiosInstance } from '../../../utils';
6
6
  // eslint-disable-next-line import/prefer-default-export
7
7
  export const fetchData = async (queryParams, toggleNotification) => {
8
8
  try {
9
- console.log('api');
10
- console.log(queryParams);
11
- console.log(getApiURL(`moderate/all${!isEmpty(queryParams) ? `?${stringify(queryParams, { encode: false })}` : ''}`));
12
9
  const { data } = await axiosInstance.get(getApiURL(`moderate/all${queryParams ? `?${stringify(queryParams, { encode: false })}` : ''}`));
13
10
 
14
11
  return data;
@@ -0,0 +1,8 @@
1
+ import appReducer from './pages/App/reducer';
2
+ import { REDUCER_NAME } from './pages/App/reducer/constants';
3
+
4
+ const reducers = {
5
+ [`${REDUCER_NAME}`]: appReducer,
6
+ };
7
+
8
+ export default reducers;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strapi-plugin-comments",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.2",
4
4
  "description": "Strapi - Comments plugin",
5
5
  "strapi": {
6
6
  "name": "comments",
@@ -1,6 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports = {
4
- default: {},
4
+ default: {
5
+ entryLabel: {
6
+ '*': ['Title', 'title', 'Name', 'name', 'Subject', 'subject']
7
+ }
8
+ },
5
9
  validator() {},
6
10
  };
@@ -102,27 +102,11 @@ module.exports = {
102
102
  },
103
103
 
104
104
  async config() {
105
- const configRelatedContentTypes = _.get(strapi.config, ['plugins', 'comments', 'relatedContentTypes'], {});
106
- const contentsTypes = this.getContentsTypes();
107
- const relatedContentTypes = Object
108
- .keys(configRelatedContentTypes)
109
- .reduce((acc, currentKey) => {
110
- const current = configRelatedContentTypes[currentKey];
111
- const { key } = contentsTypes.find(content => content.value === currentKey) || {};
112
- return {
113
- ...acc,
114
- [currentKey]: {
115
- ...current,
116
- globalName: _.snakeCase(key),
117
- },
118
- };
119
- },
120
- {},
121
- );
122
- return {
123
- relatedContentTypes,
124
- contentsTypes,
125
- };
105
+ try {
106
+ return await this.getService().config();
107
+ } catch (e) {
108
+ throwError(ctx, e);
109
+ }
126
110
  },
127
111
 
128
112
  getContentsTypes() {
@@ -22,6 +22,14 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
22
22
  return getPluginService('common');
23
23
  },
24
24
 
25
+ // Config
26
+ async config() {
27
+ const entryLabel = this.getCommonService().getConfig('entryLabel');
28
+ return {
29
+ entryLabel,
30
+ };
31
+ },
32
+
25
33
  // Find all comments
26
34
  async findAll({ related, entity, ...query }) {
27
35
  const { _q, orderBy, pageSize = 10, page = 1, filters, ...rest } = parseParams(query);
@@ -57,8 +65,6 @@ const { APPROVAL_STATUS, REGEX } = require('./../utils/constants')
57
65
  .map(_ => filterOurResolvedReports(this.getCommonService().sanitizeCommentEntity(_)))
58
66
  .map(_ => this.getCommonService().mergeRelatedEntityTo(_, relatedEntities));
59
67
 
60
- console.log(relatedEntities);
61
-
62
68
  return {
63
69
  result,
64
70
  pagination: {
@@ -93,7 +93,7 @@ module.exports = ({ strapi }) => ({
93
93
  return { id: _.id, itemsInTread: count, firstThreadItemId: first(nestedEntries)?.id }
94
94
  }));
95
95
 
96
- const relatedEntities = relatedEntity !== null ? [relatedEntity] : await this.findRelatedEntitiesFor([...entitiesRoot, ...entitiesNested]);
96
+ const relatedEntities = relatedEntity !== null ? [relatedEntity] : await this.findRelatedEntitiesFor([...entries]);
97
97
  const result = entries
98
98
  .map(_ => {
99
99
  const threadedItem = entriesWithThreads.find(item => item.id === _.id);