+
+
+ // console.log("idarticle------------" + idarticle)
+ return (
+
+
{body}
-
+
)
}
diff --git a/src/components/Filters/Select.js b/src/components/Filters/Select.js
index 1a91857..0bf8716 100644
--- a/src/components/Filters/Select.js
+++ b/src/components/Filters/Select.js
@@ -8,14 +8,14 @@ import 'react-select/dist/react-select.css'
class SelectFilter extends Component {
static propTypes = {
- articles: PropTypes.array.isRequired
+ articles: PropTypes.object.isRequired
};
handleChange = selected => this.props.changeSelection(selected.map(option => option.value))
render() {
const { articles, selected } = this.props
- const options = articles.map(article => ({
+ const options = Object.values(articles).map(article => ({
label: article.title,
value: article.id
}))
diff --git a/src/constants/index.js b/src/constants/index.js
index a2f0a80..97770b2 100644
--- a/src/constants/index.js
+++ b/src/constants/index.js
@@ -3,4 +3,10 @@ export const INCREMENT = 'INCREMENT'
export const DELETE_ARTICLE = 'DELETE_ARTICLE'
export const CHANGE_SELECTION = 'CHANGE_SELECTION'
-export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'
\ No newline at end of file
+export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'
+
+
+
+export const ADD_NEW_COMMENT = 'ADD_NEW_COMMENT'
+
+export const ADD_NEW_ID_COMMENT_TO_ARCTICLE = "ADD_NEW_ID_COMMENT_TO_ARCTICLE" // это врятли получится
\ No newline at end of file
diff --git a/src/middlewares/newIdComment.js b/src/middlewares/newIdComment.js
new file mode 100644
index 0000000..17fcc77
--- /dev/null
+++ b/src/middlewares/newIdComment.js
@@ -0,0 +1,26 @@
+import { ADD_NEW_COMMENT } from '../constants'
+
+
+export default store => next => action => {
+ console.log('--- state before: ', store.getState())
+ console.log('--- action: ', action)
+
+ function randId() {
+ return (Math.random() * 1e32).toString(36);
+ }
+
+ // console.log(randId()) // для теста
+
+
+ if (action.type === ADD_NEW_COMMENT) {
+
+ console.log("new id" + randId())
+
+ action.payload.id = randId()
+ }
+ console.log('--- new pay ac', action)
+
+
+ next(action)
+ console.log('--- state after: ', store.getState())
+}
\ No newline at end of file
diff --git a/src/reducer/articles.js b/src/reducer/articles.js
index 480a522..cf96da9 100644
--- a/src/reducer/articles.js
+++ b/src/reducer/articles.js
@@ -1,13 +1,96 @@
-import { DELETE_ARTICLE } from '../constants'
-import {normalizedArticles as defaultArticles} from '../fixtures'
+import { DELETE_ARTICLE, ADD_NEW_ID_COMMENT_TO_ARCTICLE } from '../constants'
+import { normalizedArticles as defaultArticles } from '../fixtures'
+
+
+const articleMap = defaultArticles.reduce((acc, article) => ({
+ ...acc,
+ [article.id]: article
+}), {})
-export default (articlesState = defaultArticles, action) => {
- const { type, payload } = action
+export default (articlesState = articleMap, action) => {
+ const { type, payload } = action
+ /// исправить потом
switch (type) {
case DELETE_ARTICLE:
- return articlesState.filter(article => article.id !== payload.id)
+
+ const articles = Object.keys(articlesState).reduce((obj, key) => {
+ if (key !== payload.id) return {...obj, [key]: articlesState[key] }
+ return obj
+ }, {})
+
+ return articles
+ case ADD_NEW_ID_COMMENT_TO_ARCTICLE:
+ console.log(" тут будет логика добавления ")
+ // console.log(" тут произошло обновление " + payload.idarcticle)
+ const articleid = payload.idarcticle;
+ // console.log(articlesState[id])
+ // console.log(store)
+ // console.log(store.getState().comments)
+ // console.log( Object.key(store.getState().comments).reduce( ( obj ) ) )
+ console.log(Object.values(store.getState().comments).length)
+ const l = Object.values(store.getState().comments).length;
+
+ console.log(Object.values(store.getState().comments)[l - 1])
+
+
+ const newIdComment = Object.values(store.getState().comments)[l - 1].id
+ console.log(newIdComment)
+
+
+ const art = Object.keys(articlesState).reduce((obj, key) => {
+
+
+ if (key == articleid) {
+ console.log(articlesState[key].comments)
+ articlesState[key].comments.push(newIdComment)
+ console.log(articlesState[key].comments)
+ return {...obj, [key]: articlesState[key] }
+ }
+ if (key !== articleid) return {...obj, [key]: articlesState[key] }
+
+ }, {})
+
+ return art
+
+
+
+
+
+
+ // Object.values(store.getState().comments).forEach(element => {
+ // console.log(" zlo" + element)
+ // });
+
+
+
+
+
+
+
+ // return articlesState.filter(article => article.id !== payload.id)
}
return articlesState
-}
\ No newline at end of file
+}
+
+
+
+
+
+// import {normalizedComments as defaultComments} from '../fixtures'
+
+// const commentsMap = defaultComments.reduce((acc, comment) => ({
+// ...acc,
+// [comment.id]: comment
+// }), {})
+
+// export default (state = commentsMap, action) => {
+// const { type } = action
+
+// switch (type) {
+
+// }
+
+// return state
+// }
\ No newline at end of file
diff --git a/src/reducer/comments.js b/src/reducer/comments.js
index 30bd10c..dad2d8b 100644
--- a/src/reducer/comments.js
+++ b/src/reducer/comments.js
@@ -1,5 +1,5 @@
-import { } from '../constants'
-import {normalizedComments as defaultComments} from '../fixtures'
+import { ADD_NEW_COMMENT } from '../constants'
+import { normalizedComments as defaultComments } from '../fixtures'
const commentsMap = defaultComments.reduce((acc, comment) => ({
...acc,
@@ -10,7 +10,17 @@ export default (state = commentsMap, action) => {
const { type } = action
switch (type) {
+ case ADD_NEW_COMMENT:
+ {
+ // console.log("==new data" + action.payload.id)
+ // return state
+ return {
+ ...state,
+ [action.payload.id]: { id: action.payload.id, user: action.payload.comment.user, text: action.payload.comment.text }
+ }
+
+ }
}
return state
diff --git a/src/selectors/index.js b/src/selectors/index.js
index 4e3172b..78f2e50 100644
--- a/src/selectors/index.js
+++ b/src/selectors/index.js
@@ -1,4 +1,4 @@
-import {createSelector} from 'reselect'
+import { createSelector } from 'reselect'
export const articlesSelector = state => state.articles
export const filtersSelector = state => state.filters
@@ -12,11 +12,20 @@ export const createCommentSelector = () => createSelector(commentsSelector, idSe
export const filtratedArticlesSelector = createSelector(articlesSelector, filtersSelector, (articles, filters) => {
console.log('---', 'computing filters')
- const {selected, dateRange: {from, to}} = filters
+ const { selected, dateRange: { from, to } } = filters
- return articles.filter(article => {
+ return Object.values(articles).filter(article => {
const published = Date.parse(article.date)
return (!selected.length || selected.includes(article.id)) &&
(!from || !to || (published > from && published < to))
})
+})
+
+
+// export const articleSelector = state => state.article
+export const idArtile = (_, props) => props.id
+
+export const createArticleSelector = () => createSelector(articlesSelector, idArtile, (articles, id) => {
+ console.log("search article", id)
+ return articles[id]
})
\ No newline at end of file
diff --git a/src/store/index.js b/src/store/index.js
index f83d367..a1813f2 100644
--- a/src/store/index.js
+++ b/src/store/index.js
@@ -1,16 +1,16 @@
-import {createStore, applyMiddleware, compose} from 'redux'
+import { createStore, applyMiddleware, compose } from 'redux'
import rootReducer from '../reducer'
import logger from '../middlewares/logger'
-
+import newIdComment from '../middlewares/newIdComment'
const composeEnhancers =
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
- window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
- // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
- }) : compose
+ window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
+ // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
+ }) : compose
const enhancer = composeEnhancers(
- applyMiddleware(logger)
+ applyMiddleware(logger, newIdComment)
)
const store = createStore(rootReducer, enhancer)