Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week23 finished #27

Merged
merged 1 commit into from Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions homeworks/week23/hw1/.gitignore
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
1 change: 1 addition & 0 deletions homeworks/week23/hw1/README.md
@@ -0,0 +1 @@
# redux_todoList
Empty file removed homeworks/week23/hw1/index.html
Empty file.
16,675 changes: 16,675 additions & 0 deletions homeworks/week23/hw1/package-lock.json

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions homeworks/week23/hw1/package.json
@@ -0,0 +1,41 @@
{
"name": "todolist",
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.5.1",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"gh-pages": "^3.2.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.3",
"react-scripts": "4.0.3",
"styled-components": "^5.3.3"
},
"homepage": "https://dingdingdingliu.github.io/redux_todoList/",
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -b master -d build",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added homeworks/week23/hw1/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions homeworks/week23/hw1/public/index.html
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React Redux App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added homeworks/week23/hw1/public/logo192.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added homeworks/week23/hw1/public/logo512.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions homeworks/week23/hw1/public/manifest.json
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
2 changes: 2 additions & 0 deletions homeworks/week23/hw1/public/robots.txt
@@ -0,0 +1,2 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
40 changes: 40 additions & 0 deletions homeworks/week23/hw1/src/App.js
@@ -0,0 +1,40 @@
import React from 'react'
import styled, { ThemeProvider } from 'styled-components'
import GlobalStyle from './constants/globalStyle'
import Header from './components/header'
import MainTodoList from './components/mainTodoList'
import ThemeButton from './components/themeButton'
import { useSelector } from 'react-redux'
import { colorThemeSelector } from './redux/selector'

const AppDiv = styled.div`
min-height: 100vh;
background-color: ${(props) => props.theme.colors.background};
display: flex;
flex-direction: column;
align-items: center;
`
const ThemeButtonDiv = styled.div`
position: absolute;
top: 10%;
left: 90%;
`

function App() {
const theme = useSelector(colorThemeSelector)

return (
<ThemeProvider theme={theme}>
<GlobalStyle />
<AppDiv>
<ThemeButtonDiv>
<ThemeButton />
</ThemeButtonDiv>
<Header />
<MainTodoList />
</AppDiv>
</ThemeProvider>
)
}

export default App
8 changes: 8 additions & 0 deletions homeworks/week23/hw1/src/app/store.js
@@ -0,0 +1,8 @@
import { configureStore } from '@reduxjs/toolkit'
import todoItemsReducer from '../redux/reducer'

export const store = configureStore({
reducer: {
todoListState: todoItemsReducer
}
})
83 changes: 83 additions & 0 deletions homeworks/week23/hw1/src/components/addTodo/AddTodo.js
@@ -0,0 +1,83 @@
import styled from 'styled-components'
import { useCallback, useEffect } from 'react'
import { useDispatch } from 'react-redux'
import { LargeButtonStyle } from '../button/Button'
import useInput from '../../hooks/useInput'
import { addTodo } from '../../redux/actions'
import { useSelector } from 'react-redux'
import { todoItemsSelector } from '../../redux/selector'

const AddTodoDiv = styled.div`
display: flex;
justify-content: space-between;
padding: 10px;
`

const AddInputStyle = styled.input`
padding: 10px;
margin-right: 10px;
border-radius: 5px;
outline: none;
border: 1px solid ${(props) => props.theme.colors.border};
`

function AddInput({ type, placeholder, value, onChange, onKeyPress }) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果你這個 component 只是把東西都傳下去,其實不需要這個,你直接用 AddInputStyle 也可以達成一樣的效果

return (
<AddInputStyle
type={type}
placeholder={placeholder}
value={value}
onChange={onChange}
onKeyPress={onKeyPress}
></AddInputStyle>
)
}

function AddItemButton({ className, onClick, children }) {
return (
<LargeButtonStyle className={className} onClick={onClick}>
{children}
</LargeButtonStyle>
)
}

export default function AddTodo() {
const dispatch = useDispatch()
const { value, setValue, handleInputChange } = useInput()
const todoItemsState = useSelector(todoItemsSelector)

function setItemIntoLocalStorage(todoItemList) {
const localList = { todoItems: todoItemList }
window.localStorage.setItem('todoItems', JSON.stringify(localList))
}

useEffect(() => {
setItemIntoLocalStorage(todoItemsState)
}, [todoItemsState])

const handleAddTodo = useCallback(() => {
if (!value.trim('')) return setValue('')
dispatch(addTodo(value))
setValue('')
}, [dispatch, value, setValue])

const handleKeyPress = useCallback(
(e) => {
if (e.key === 'Enter') handleAddTodo()
},
[handleAddTodo]
)

return (
<AddTodoDiv>
<AddInput
type='text'
placeholder='Your Todo'
value={value}
onChange={handleInputChange}
onKeyPress={handleKeyPress}
/>
<AddItemButton onClick={handleAddTodo}>Add Todo</AddItemButton>
</AddTodoDiv>
)
}
1 change: 1 addition & 0 deletions homeworks/week23/hw1/src/components/addTodo/index.js
@@ -0,0 +1 @@
export { default } from './AddTodo'
29 changes: 29 additions & 0 deletions homeworks/week23/hw1/src/components/button/Button.js
@@ -0,0 +1,29 @@
import styled from 'styled-components'

const LargeButtonStyle = styled.button`
padding: 8px 16px;
font-size: 20px;
background: ${(props) => props.theme.colors.background};
color: ${(props) => props.theme.colors.font};
border: 2px solid ${(props) => props.theme.colors.border};
border-radius: 5px;

:hover {
cursor: pointer;
transform: scale(1.05);
}
`

const SmallButtonStyle = styled.button`
padding: 6px 10px;
font-size: 14px;
background: ${(props) => props.theme.colors.background};
border-radius: 5px;

:hover {
cursor: pointer;
transform: scale(1.05);
}
`

export { LargeButtonStyle, SmallButtonStyle }