Skip to content

Commit

Permalink
feat(codestyle): refactored code to use package by feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rbiedrawa committed Mar 14, 2022
1 parent 1cb2bb2 commit bb71c61
Show file tree
Hide file tree
Showing 30 changed files with 97 additions and 61 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Project Structure

```shell
./src
├── assets # assets folder contains all the static files (images, fonts, etc).
├── components # shared components
├── config # global configuration, env variables etc.
│   ├── Env.ts
│   └── i18n
├── features
│   ├── feature # 'feature'
│   │   ├── api # API folder contains http service calls
│   │   ├── assets # 'feature' assets folder
│   │   ├── components # 'feature' components
│   │   ├── hooks # 'feature' hooks
│   │   ├── index.ts # entry point for 'feature' public API
│   │   ├── store # 'feature' state stores contains slices, sagas, etc.
│   │   └── types # 'feature' typescript types
│   └── another_feature # 'another_feature'
├── hooks # shared hooks
├── libs # libraries imported or exported that can be used in different projects
│   └── ui
├── pages # contains all application pages
├── routes # routes configuration
├── store # root store and store settings
└── App.tsx # Application entrypoint
```

## Getting Started

### Development
Expand Down Expand Up @@ -72,7 +100,6 @@ npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'

[//]: # (TODO: add readme for release via Standard Version)


## References

* [React documentation](https://reactjs.org/)
Expand Down Expand Up @@ -100,4 +127,5 @@ npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
* [React Hook Form - typescript Support](https://react-hook-form.com/ts)
* [Redux Style Guide](https://redux.js.org/style-guide/style-guide#write-action-types-as-domaineventname)
* [MUI - theme switcher](https://mui.com/customization/dark-mode/)
* [Level up your CSS linting using Stylelint](https://blog.logrocket.com/using-stylelint-improve-lint-css-scss-sass/)
* [Level up your CSS linting using Stylelint](https://blog.logrocket.com/using-stylelint-improve-lint-css-scss-sass/)
* [Create React App: A quick setup guide](https://blog.logrocket.com/create-react-app-a-quick-setup-guide-b812f0aad03c/)
Empty file added src/assets/.gitkeep
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/store/api/post.api.ts → src/features/posts/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Env } from '../../constants/Env'
import { Post } from '../../libs/models/post.model'
import { Env } from '../../../config/Env'
import { Post } from '../types'

export const getPosts = async (): Promise<Post[]> =>
fetch(`${Env.POSTS_API_BASE_URL}`).then(res => res.json())
Expand Down
Binary file added src/features/posts/assets/imgs/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import Typography from '@mui/material/Typography'
import React from 'react'
import { useTranslation } from 'react-i18next'

import { Post } from '../../libs/models/post.model'
import logoImage from '../assets/imgs/logo192.png'
import { Post } from '../types'

type PostCardViewProps = {
export type PostCardViewProps = {
post: Post
onDeleteClick: (post: Post) => void
onUpdateClick: (post: Post) => void
}

const PostCardView = (props: PostCardViewProps) => {
export const PostCardView = (props: PostCardViewProps) => {
const { t } = useTranslation()

const { post, onDeleteClick, onUpdateClick } = props
Expand All @@ -25,7 +26,7 @@ const PostCardView = (props: PostCardViewProps) => {
return (
<>
<Card sx={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
<CardMedia component="img" image="https://source.unsplash.com/random" alt="random" />
<CardMedia component="img" src={logoImage} alt="random" />
<CardContent sx={{ flexGrow: 1 }}>
<Typography gutterBottom variant="h5" component="h2">
{post.title}
Expand All @@ -44,5 +45,3 @@ const PostCardView = (props: PostCardViewProps) => {
</>
)
}

export default PostCardView
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Container from '@mui/material/Container'
import React, { useEffect } from 'react'

import PostForm from '../components/Posts/PostForm'
import PostList from '../components/Posts/PostList'
import { usePostService } from '../hooks'

const PostContainer = () => {
import { PostForm } from './PostForm'
import { PostList } from './PostList'

export const PostContainer = () => {
const { posts, deletePost, updatePost, fetchAllPosts, createPost } = usePostService()

useEffect(() => {
Expand All @@ -22,5 +23,3 @@ const PostContainer = () => {
</>
)
}

export default PostContainer
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ import { useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import * as Yup from 'yup'

import { FormTextField } from '../../libs/ui/FormTextField'
import { FormTextField } from '../../../libs/ui/components/FormTextField'
import { PostFormInput } from '../types'

type PostFormProps = {
export type PostFormProps = {
defaultValues?: PostFormInput
onSubmitClick(data: PostFormInput): void
}

export interface PostFormInput {
title: string
body: string
}

const PostForm = (props: PostFormProps) => {
export const PostForm = (props: PostFormProps) => {
const { t } = useTranslation()

const {
Expand Down Expand Up @@ -55,5 +51,3 @@ const PostForm = (props: PostFormProps) => {
</Stack>
)
}

export default PostForm
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Grid } from '@mui/material'
import React from 'react'

import { Post } from '../../libs/models/post.model'
import { Post } from '../types'

import PostCardView from './PostCardView'
import { PostCardView } from './PostCardView'

type PostListProps = {
export type PostListProps = {
posts: Post[]
onDeletePost: (post: Post) => void
onUpdatePost: (post: Post) => void
}

const PostList = (props: PostListProps) => {
export const PostList = (props: PostListProps) => {
const { posts, onDeletePost, onUpdatePost } = props

return (
Expand All @@ -26,5 +26,3 @@ const PostList = (props: PostListProps) => {
</>
)
}

export default PostList
4 changes: 4 additions & 0 deletions src/features/posts/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './PostForm'
export * from './PostList'
export * from './PostCardView'
export * from './PostContainer'
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useCallback } from 'react'

import { PostFormInput } from '../components/Posts/PostForm'
import { Post } from '../libs/models/post.model'
import { postsActions, selectPosts } from '../store/features/posts/posts.slice'
import { useAppDispatch, useAppSelector } from '../store/hooks'
import { useAppDispatch, useAppSelector } from '../../../store/hooks'
import { postsActions, selectPosts } from '../store'
import { Post, PostFormInput } from '../types'

type PostServiceOperators = {
export type PostServiceOperators = {
posts: Post[]
createPost: (data: PostFormInput) => void
fetchAllPosts: () => void
Expand Down
4 changes: 4 additions & 0 deletions src/features/posts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './hooks'
export * from './store'
export * from './types'
export * from './components'
2 changes: 2 additions & 0 deletions src/features/posts/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './posts.sagas'
export * from './posts.slice'
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { put, takeEvery } from 'redux-saga/effects'

import { Post } from '../../../libs/models/post.model'
import { createPost, deletePost, getPosts, updatePost } from '../../api/post.api'
import { createPost, deletePost, getPosts, updatePost } from '../api'
import { Post } from '../types'

import { postsActions } from './posts.slice'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// DUCKS pattern
import { createAction, createSlice, nanoid, PayloadAction } from '@reduxjs/toolkit'

import { Post } from '../../../libs/models/post.model'
import type { RootState } from '../../store'
import type { RootState } from '../../../store/store'
import { Post } from '../types'

interface PostsState {
posts: Post[]
Expand Down
10 changes: 10 additions & 0 deletions src/features/posts/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type Post = {
id?: string
title: string
body: string
}

export type PostFormInput = {
title: string
body: string
}
Empty file added src/hooks/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import ReactDOM from 'react-dom'

import App from './App'
import './i18n'
import './config/i18n'
import './index.css'
import reportWebVitals from './reportWebVitals'

Expand Down
5 changes: 0 additions & 5 deletions src/libs/models/post.model.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/pages/AboutPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { useTranslation } from 'react-i18next'

import TitleTypography from '../libs/ui/TitleTypography'
import TitleTypography from '../libs/ui/components/TitleTypography'

const AboutPage = () => {
const { t } = useTranslation()
Expand Down
4 changes: 2 additions & 2 deletions src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from 'react'
import { useTranslation } from 'react-i18next'

import '../App.css'
import PostContainer from '../containers/PostContainer'
import TitleTypography from '../libs/ui/TitleTypography'
import { PostContainer } from '../features/posts'
import TitleTypography from '../libs/ui/components/TitleTypography'

const HomePage = () => {
const { t } = useTranslation()
Expand Down
22 changes: 13 additions & 9 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import React, { Suspense } from 'react'
import { Route, Routes } from 'react-router-dom'

import Layout from '../components/Layout'
import AboutPage from '../pages/AboutPage'
import HomePage from '../pages/HomePage'

const HomePage = React.lazy(() => import('../pages/HomePage'))
const AboutPage = React.lazy(() => import('../pages/AboutPage'))

const AppRoutes = () => (
<>
<Routes>
<Route element={<Layout />}>
<Route path="/" element={<HomePage />} />
<Route path="/posts" element={<HomePage />} />
<Route path="/about" element={<AboutPage />} />
</Route>
</Routes>
<Suspense fallback={<div>Loading</div>}>
<Routes>
<Route element={<Layout />}>
<Route path="/" element={<HomePage />} />
<Route path="/posts" element={<HomePage />} />
<Route path="/about" element={<AboutPage />} />
</Route>
</Routes>
</Suspense>
</>
)

Expand Down
2 changes: 1 addition & 1 deletion src/store/rootSaga.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { all, fork } from 'redux-saga/effects'

import { postsWatcherSaga } from './features/posts/posts.sagas'
import { postsWatcherSaga } from '../features/posts/store/posts.sagas'

export function* rootSaga() {
yield all([fork(postsWatcherSaga)])
Expand Down
4 changes: 2 additions & 2 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { createBrowserHistory } from 'history'
import { createReduxHistoryContext } from 'redux-first-history'
import logger from 'redux-logger'

import { Env } from '../constants/Env'
import { Env } from '../config/Env'
import postsReducer from '../features/posts/store/posts.slice'

import postsReducer from './features/posts/posts.slice'
import { rootSaga } from './rootSaga'

const { createReduxHistory, routerMiddleware, routerReducer } = createReduxHistoryContext({
Expand Down

0 comments on commit bb71c61

Please sign in to comment.