-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
380 additions
and
9 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,7 @@ | |
.img { | ||
width: 200px; | ||
height: 200px; | ||
object-fit: cover; | ||
} | ||
|
||
.infoWrapper { | ||
|
3 changes: 3 additions & 0 deletions
3
src/entities/Article/ui/ArticleViewSelector/ArticleViewSelector.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.notSelected { | ||
fill: var(--secondary-color); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/entities/Article/ui/ArticleViewSelector/ArticleViewSelector.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
import { ArticleViewSelector } from './ArticleViewSelector'; | ||
|
||
export default { | ||
title: 'entities/Article/ArticleViewSelector', | ||
component: ArticleViewSelector, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} as ComponentMeta<typeof ArticleViewSelector>; | ||
|
||
const Template: ComponentStory<typeof ArticleViewSelector> = (args) => <ArticleViewSelector {...args} />; | ||
|
||
export const Normal = Template.bind({}); | ||
Normal.args = {}; |
58 changes: 58 additions & 0 deletions
58
src/entities/Article/ui/ArticleViewSelector/ArticleViewSelector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { memo } from 'react'; | ||
|
||
import { Button, ButtonTheme } from 'shared/ui/Button'; | ||
import { Icon } from 'shared/ui/Icon'; | ||
import { classNames } from 'shared/lib/classNames'; | ||
import ViewSmallIcon from 'shared/assets/icons/view-small.svg'; | ||
import ViewBigIcon from 'shared/assets/icons/view-big.svg'; | ||
|
||
import { ArticleView } from '../../model/types/article'; | ||
import cls from './ArticleViewSelector.module.scss'; | ||
|
||
interface ArticleViewSelectorProps { | ||
className?: string; | ||
view: ArticleView; | ||
onViewClick?: (view: ArticleView) => void; | ||
} | ||
|
||
const viewTypes = [ | ||
{ | ||
view: ArticleView.SMALL, | ||
icon: ViewSmallIcon, | ||
}, | ||
{ | ||
view: ArticleView.BIG, | ||
icon: ViewBigIcon, | ||
}, | ||
]; | ||
|
||
export const ArticleViewSelector = memo((props: ArticleViewSelectorProps) => { | ||
const { | ||
className, | ||
view, | ||
onViewClick, | ||
} = props; | ||
|
||
const onClick = (newView: ArticleView) => () => { | ||
onViewClick?.(newView); | ||
}; | ||
|
||
return ( | ||
<div className={classNames(cls.articleViewSelector, {}, [className])}> | ||
{viewTypes.map((viewType) => ( | ||
<Button | ||
key={viewType.view} | ||
theme={ButtonTheme.CLEAR} | ||
onClick={onClick(viewType.view)} | ||
> | ||
<Icon | ||
className={classNames('', { [cls.notSelected]: viewType.view !== view }, [])} | ||
Svg={viewType.icon} | ||
/> | ||
</Button> | ||
))} | ||
</div> | ||
); | ||
}); | ||
|
||
ArticleViewSelector.displayName = 'ArticleViewSelector'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { ArticlesPageAsync as ArticlesPage } from './ui/ArticlesPage/ArticlesPage.async'; | ||
export { ArticlesPageSchema } from './model/types/articlesPageSchema'; |
6 changes: 6 additions & 0 deletions
6
src/pages/ArticlesPage/model/selectors/articlesPageSelectors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { StateSchema } from 'app/providers/StoreProvider'; | ||
import { ArticleView } from 'entities/Article'; | ||
|
||
export const getArticlesPageIsLoading = (state: StateSchema) => state.articlesPage?.isLoading || false; | ||
export const getArticlesPageError = (state: StateSchema) => state.articlesPage?.error; | ||
export const getArticlesPageView = (state: StateSchema) => state.articlesPage?.view || ArticleView.SMALL; |
28 changes: 28 additions & 0 deletions
28
src/pages/ArticlesPage/model/services/fetchArticlesList/fetchArticlesList.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
|
||
import { ThunkConfig } from 'app/providers/StoreProvider'; | ||
|
||
import { Article } from 'entities/Article'; | ||
|
||
export const fetchArticlesList = createAsyncThunk<Article[], void, ThunkConfig<string>>( | ||
'articlesPage/fetchArticlesList', | ||
async (_, thunkAPI) => { | ||
const { extra, rejectWithValue } = thunkAPI; | ||
|
||
try { | ||
const response = await extra.api.get<Article[]>('/articles', { | ||
params: { | ||
_expand: 'user', | ||
}, | ||
}); | ||
|
||
if (!response.data) { | ||
throw new Error(); | ||
} | ||
|
||
return response.data; | ||
} catch (e) { | ||
return rejectWithValue('error'); | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { createEntityAdapter, createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
|
||
import { Article, ArticleView } from 'entities/Article'; | ||
import { StateSchema } from 'app/providers/StoreProvider'; | ||
import { ARTICLES_VIEW_LOCALSTORAGE_KEY } from 'shared/const/localStorage'; | ||
|
||
import { fetchArticlesList } from '../../model/services/fetchArticlesList/fetchArticlesList'; | ||
import { ArticlesPageSchema } from '../types/articlesPageSchema'; | ||
|
||
const articlesAdapter = createEntityAdapter<Article>({ | ||
selectId: (article) => article.id, | ||
}); | ||
|
||
export const getArticles = articlesAdapter.getSelectors<StateSchema>( | ||
(state) => state.articlesPage || articlesAdapter.getInitialState(), | ||
); | ||
|
||
const articlesPageSlice = createSlice({ | ||
name: 'articlesPageSlice', | ||
initialState: articlesAdapter.getInitialState<ArticlesPageSchema>({ | ||
isLoading: false, | ||
error: undefined, | ||
ids: [], | ||
entities: {}, | ||
view: ArticleView.SMALL, | ||
}), | ||
reducers: { | ||
setView: (state, action: PayloadAction<ArticleView>) => { | ||
state.view = action.payload; | ||
localStorage.setItem(ARTICLES_VIEW_LOCALSTORAGE_KEY, action.payload); | ||
}, | ||
initState: (state) => { | ||
state.view = localStorage.getItem(ARTICLES_VIEW_LOCALSTORAGE_KEY) as ArticleView || ArticleView.SMALL; | ||
}, | ||
}, | ||
extraReducers: (builder) => { | ||
builder | ||
.addCase(fetchArticlesList.pending, (state) => { | ||
state.error = undefined; | ||
state.isLoading = true; | ||
}) | ||
.addCase(fetchArticlesList.fulfilled, (state, action: PayloadAction<Article[]>) => { | ||
state.isLoading = false; | ||
articlesAdapter.setAll(state, action.payload); | ||
}) | ||
.addCase(fetchArticlesList.rejected, (state, action) => { | ||
state.isLoading = false; | ||
state.error = action.payload; | ||
}); | ||
}, | ||
}); | ||
|
||
export const { | ||
reducer: articlesPageReducer, | ||
actions: articlesPageActions, | ||
} = articlesPageSlice; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { EntityState } from '@reduxjs/toolkit'; | ||
|
||
import { Article, ArticleView } from 'entities/Article'; | ||
|
||
export interface ArticlesPageSchema extends EntityState<Article>{ | ||
isLoading?: boolean; | ||
error?: string; | ||
|
||
view: ArticleView; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const USER_LOCALSTORAGE_KEY = 'user'; | ||
export const ARTICLES_VIEW_LOCALSTORAGE_KEY = 'articles_view'; |