Skip to content

Commit

Permalink
create interfaces, get and render initial data from slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Grazia Palombella committed Dec 21, 2022
1 parent 7af75f3 commit 64ee97b
Show file tree
Hide file tree
Showing 10 changed files with 1,333 additions and 1,167 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"rules": {
"react/jsx-uses-vars": "error",
"react/jsx-uses-react": "error",
"semi": ["error", "never"],
"space-in-parens": ["error", "always", { "exceptions": [ "{}" ] }],
//"semi": ["error", "never"],
// "space-in-parens": ["error", "always", { "exceptions": [ "{}" ] }],
"@typescript-eslint/member-delimiter-style": ["error", {
"multiline": {
"delimiter": "none",
Expand Down
2,372 changes: 1,214 additions & 1,158 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
"@types/react-dom": "^18.0.9",
"@types/redux-logger": "^3.0.9",
"@typescript-eslint/eslint-plugin": "^5.46.1",
"buffer": "^5.7.1",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-react": "^7.31.11",
"eslint-plugin-typescript": "^0.14.0",
"parcel": "^2.8.0",
"prettier": "^2.8.0",
"process": "^0.11.10",
"redux-devtools-extension": "^2.13.9"
},
"dependencies": {
Expand Down
20 changes: 17 additions & 3 deletions src/EntryPoint.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import React from 'react'
import { useAppSelector } from './redux/selectors'
import CommentComponent from './components/CommentComponent'
import User from './components/User'

type Props = {}

const EntryPoint = ( props: Props ) => {
const EntryPoint: React.FC = () => {
const comments = useAppSelector(state => state.data?.comments)

console.log( comments)

return (
<div>Entry</div>
<>
{comments?.map((comment) => {
return (
<CommentComponent key={comment.id}>
<User user={comment.user}/>
</CommentComponent>
)
})}
</>
)
}

Expand Down
16 changes: 16 additions & 0 deletions src/components/CommentComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react'

interface CommentComponentProps {
readonly children: React.ReactNode
}


const CommentComponent = (props: CommentComponentProps) => {
return (
<>
{props.children}
</>
)
}

export default CommentComponent
14 changes: 14 additions & 0 deletions src/components/User.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react'
import { User } from '../redux/slices/commentsSlice'

interface UserProps {
readonly user: User
}

const User = (props: UserProps) => {
return (
<div>{props.user.username}</div>
)
}

export default User
7 changes: 7 additions & 0 deletions src/redux/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useDispatch, useSelector } from "react-redux";

import type { TypedUseSelectorHook } from "react-redux";
import type { RootState, AppDispatch } from "../store";

export const useAppDispatch: () => AppDispatch = useDispatch
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector
55 changes: 55 additions & 0 deletions src/redux/slices/commentsSlice.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

import { createSlice } from "@reduxjs/toolkit";

import initialCommentsRaw from '../../data.json'

const initialComments: Data = initialCommentsRaw


interface Image {
png: string
webp: string
}

export interface User {
image: Image
username: string
}

type Replies = Omit<Comment, 'replies'> & { replyingTo: string }

export interface Data {
currentUser: User
comments: Comment[]
}

export interface Comment {
id: number
content: string
createdAt: string
score: number
user: User
replies: Replies[]
}

interface CommentState {
loading: boolean
error: null | string
data: null | Data
}

const initialState = {
loading: false,
error: null,
data: initialComments
} as CommentState

const commentsSlice = createSlice({
name: 'comments',
initialState: initialState,
reducers: {

},
})

export default commentsSlice.reducer
6 changes: 4 additions & 2 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { configureStore } from '@reduxjs/toolkit'
import thunk from 'redux-thunk'
import {logger} from 'redux-logger'
import { logger } from 'redux-logger'

import commentsSlice from './redux/slices/commentsSlice'

export const store = configureStore({
reducer: { },
reducer: commentsSlice,
middleware: [thunk, logger],
})

Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "resolveJsonModule": true, /* Enable importing .json files. */
"resolveJsonModule": true, /* Enable importing .json files. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
Expand Down

0 comments on commit 64ee97b

Please sign in to comment.