'@bizon/common'
import { NewsSource } from '@bizon/common'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
interface BaseStateNews<T> extends BaseState<T> {
totalCount: number
}
export interface State {
news: {
COMMON: BaseStateNews<News[]>,
BIZONAGRO: BaseStateNews<News[]>
};
newsTopics: BaseState<NewsTopics[]>,
source: NewsSource
}
const initialState: State = {
news: {
COMMON: {
loading: false,
data: [],
error: '',
totalCount: 0
},
BIZONAGRO: {
loading: false,
data: [],
error: '',
totalCount: 0
}
},
newsTopics: {
loading: false,
data: [],
error: '',
},
source: NewsSource.COMMON
}
const newsSlice = createSlice({
name: 'newsSlice',
initialState,
reducers: {
setSource(state: State, action: PayloadAction<NewsSource>) {
state.source = action.payload
}
},
extraReducers: (builder) => {
builder.addCase(getNews.pending, (state, {meta}) => {
const source: NewsSource = meta.arg.source;
state.news[source].loading = true;
}),
builder.addCase(getNews.fulfilled, (state, {meta, payload}) => {
const { data, isSkipped, totalCount } = payload;
const source: NewsSource = meta.arg.source;
let tempData: News[] = data;
state.news[source].loading = false
state.news[source].totalCount = totalCount
tempData = isSkipped ? [...state.news[source].data, ...data] : data;
tempData.map((el) => {
el.upload = sortMediaByOrder([...el.upload])
})
state.news[source].data = tempData;
}),
builder.addCase(getNews.rejected, (state, {meta}) => {
const source: NewsSource = meta.arg.source
state.news[source].loading = false;
})
},
})
export const { setSource } = newsSlice.actions
export default newsSlice.reducer
builder.addCase(getNews.fulfilled, (state, {meta, payload}) => { const { data, isSkipped, totalCount } = payload; const source: NewsSource = meta.arg.source; state.news[source] = { loading: false, totalCount, data: (isSkipped ? [...state.news[source].data, ...data] : data).map((el) => { el.upload = sortMediaByOrder([...el.upload]) }), } })
Разве тут не используется мутирование ?
в каком месте?
Обсуждают сегодня