-
Notifications
You must be signed in to change notification settings - Fork 16
React patterns #3
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
Open
SuhareevAI
wants to merge
2
commits into
dashanalivayko:main
Choose a base branch
from
SuhareevAI:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,48 +1,25 @@ | ||
| import "./App.scss"; | ||
| import { MemberCard } from "../src/components/memberCard"; | ||
| import { useEffect, useState } from "react"; | ||
| import {useState} from "react"; | ||
| import { UserProps } from "./components/memberCard/types"; | ||
| import { ButtonWithLabel } from "./components/buttonWithLabel"; | ||
| import Form from "./components/form"; | ||
| import { Tabs } from "./components/tabs"; | ||
| import {UserList} from "./components/userList/UserList"; | ||
|
|
||
| export default function App() { | ||
| const [users, setUsers] = useState<UserProps[]>([]); | ||
| const [moreUsers, setMoreUsers] = useState<UserProps[]>([]); | ||
| const [addedUser, setAddedUser] = useState<UserProps | null>(null); | ||
| const [tabForm, setTabForm] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| fetch('https://jsonplaceholder.typicode.com/users') | ||
| .then((response) => response.json()) | ||
| .then((res) => setUsers(res)); | ||
| }, []); | ||
|
|
||
| const onButtonClick = () => { | ||
| fetch('https://jsonplaceholder.typicode.com/users') | ||
| .then((response) => response.json()) | ||
| .then((res) => setMoreUsers(res)); | ||
| }; | ||
|
|
||
| const handleUserAddition = (user: UserProps) => { | ||
| setAddedUser(user); | ||
| }; | ||
| const [addedUser, setAddedUser] = useState<UserProps | null>(null); | ||
|
|
||
| const onUserAddition = (user : UserProps) => { | ||
| setAddedUser(user); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="App"> | ||
| <Tabs onChange={setTabForm}/> | ||
| {!tabForm && users.map((user) => <MemberCard name={user.name} phone={user.phone} username={user.username} website={user.website} />)} | ||
| {!tabForm && moreUsers.map((user) => <MemberCard name={user.name} phone={user.phone} username={user.username} website={user.website} />)} | ||
| {!tabForm && <ButtonWithLabel onClick={onButtonClick}>more users</ButtonWithLabel>} | ||
| {tabForm && <Form onUserAddition={handleUserAddition} />} | ||
| {addedUser && ( | ||
| <MemberCard | ||
| name={addedUser.name} | ||
| phone={addedUser.phone} | ||
| username={addedUser.username} | ||
| website={addedUser.website} | ||
| /> | ||
| )} | ||
| {!tabForm && <UserList/>} | ||
| {tabForm && <Form onUserAddition={onUserAddition} />} | ||
| {addedUser && (<MemberCard name={addedUser.name} phone={addedUser.phone} username={addedUser.username} website={addedUser.website}/>)} | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or 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 @@ | ||
| const apiUrl = "https://jsonplaceholder.typicode.com/users"; | ||
|
|
||
| export const getUsers = fetch(apiUrl) | ||
| .then((response) => response.json()); | ||
|
|
||
| export const addUser = (username: string, phone: string, website: string) => fetch(apiUrl, { | ||
| method: 'POST', | ||
| body: JSON.stringify({ | ||
| username, | ||
| phone, | ||
| website, | ||
| }), | ||
| headers: { | ||
| 'Content-type': 'application/json; charset=UTF-8', | ||
| }, | ||
| }).then((response) => response.json()); |
This file contains hidden or 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 hidden or 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,6 +1,13 @@ | ||
| .button { | ||
| padding: 10px; | ||
| border-radius: 5px; | ||
| background-color: antiquewhite; | ||
| margin-top: 10px; | ||
| padding: 8px 16px; | ||
| border: none; | ||
| border-radius: 4px; | ||
| background-color: #007bff; | ||
| color: #fff; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| button:hover { | ||
| background-color: #0056b3; | ||
| } |
This file contains hidden or 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,4 @@ | ||
| export interface ButtonProps { | ||
| onClick: () => void, | ||
| children?: string | ||
| } |
This file contains hidden or 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,12 +1,13 @@ | ||
| import { Button } from "../button"; | ||
| import "./style.scss"; | ||
| import {FC} from "react"; | ||
| import {ButtonWithLabelProps} from "./types"; | ||
|
|
||
| export const ButtonWithLabel = ({ onClick, children }: { onClick: () => void, children: string }) => { | ||
| const text = <p style={{ marginRight: '5px' }}>нажми меня!</p>; | ||
| export const ButtonWithLabel : FC<ButtonWithLabelProps> = ({label, ...restProps}) => { | ||
| return ( | ||
| <div className="button-with-label"> | ||
| {text} | ||
| <Button onClick={onClick}>{children}</Button> | ||
| <p>{label}</p> | ||
| <Button {...restProps}/> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or 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,7 +1,11 @@ | ||
| .button-with-label { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 5px; | ||
| justify-content: center; | ||
| align-items: center; | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 5px; | ||
| justify-content: center; | ||
| align-items: center; | ||
|
|
||
| p { | ||
| margin-right: 5px; | ||
| } | ||
| } |
This file contains hidden or 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,5 @@ | ||
| export interface ButtonWithLabelProps { | ||
| onClick: () => void, | ||
| children: string, | ||
| label: string | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
| import {UserProps} from "../memberCard/types"; | ||
|
|
||
| export interface FormProps { | ||
| onUserAddition: (user : UserProps) => void; // Принимаем функцию для обновления состояния верхнего компонента | ||
| } |
This file contains hidden or 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,14 +1,13 @@ | ||
| import { FC } from "react"; | ||
| import "./style.scss"; | ||
|
|
||
| import { UserProps } from "./types"; | ||
| import { CardInfo } from "./parts/cardInfo"; | ||
|
|
||
| export const MemberCard: FC<UserProps> = ({ name, username, phone, website }) => { | ||
| export const MemberCard: FC<UserProps> = ({name, ...restProps}) => { | ||
| return ( | ||
| <div className="member-card"> | ||
| <p className="title">{name}</p> | ||
| <CardInfo username={username} phone={phone} website={website} /> | ||
| <CardInfo {...restProps}/> | ||
| </div> | ||
| ); | ||
| }; | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,5 +3,4 @@ export interface UserProps { | |
| username: string; | ||
| phone: string; | ||
| website: string; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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,11 +1,13 @@ | ||
| import { Button } from "../button"; | ||
|
|
||
| export const Tabs = ({ onChange }: { onChange: (tab: boolean) => void }) => { | ||
| import "./style.scss"; | ||
| import {FC} from "react"; | ||
| import {TabsProps} from "./types"; | ||
| export const Tabs : FC<TabsProps> = ({ onChange }) => { | ||
|
|
||
| return ( | ||
| <div style={{ width: '100%', display: 'flex', gap: '15px', justifyContent: 'center', marginBottom: '20px' }}> | ||
| <Button onClick={() => onChange(false)}>form</Button> | ||
| <Button onClick={() => onChange(true)}>users</Button> | ||
| <div className="tab"> | ||
| <Button onClick={() => onChange(false)}>users</Button> | ||
| <Button onClick={() => onChange(true)}>form</Button> | ||
| </div> | ||
| ); | ||
| }; |
This file contains hidden or 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,7 @@ | ||
| .tab { | ||
| width: 100%; | ||
| display: flex; | ||
| gap: 15px; | ||
| justify-content: center; | ||
| margin-bottom: 20px | ||
| } |
This file contains hidden or 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 @@ | ||
| export interface TabsProps { | ||
| onChange: (tab: boolean) => void | ||
| } |
This file contains hidden or 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,24 @@ | ||
| import {MemberCard} from "../memberCard"; | ||
| import {ButtonWithLabel} from "../buttonWithLabel"; | ||
| import {useUsers} from "../../hooks/useUsers"; | ||
| import {useEffect} from "react"; | ||
|
|
||
| export const UserList = () => { | ||
| const {users, setUserList} = useUsers(); | ||
| useEffect(() => { | ||
| setUserList(); | ||
| }, []); | ||
|
|
||
| const onButtonClick = () => { | ||
| setUserList(); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| {users.map((user) => { | ||
| return <MemberCard key={user.name} name={user.name} phone={user.phone} username={user.username} website={user.website} /> | ||
| })} | ||
| <ButtonWithLabel onClick={onButtonClick} label="нажми меня!">more users</ButtonWithLabel> | ||
| </> | ||
| ); | ||
| }; |
This file contains hidden or 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,23 @@ | ||
| import {useState} from "react"; | ||
| import {addUser, getUsers} from "../client/api"; | ||
| import {UserProps} from "../components/memberCard/types"; | ||
|
|
||
| export const useUsers = () => { | ||
| const [users, setUsers] = useState<UserProps[]>([]); | ||
|
|
||
| const setUserList = () => { | ||
| getUsers.then((data : UserProps[]) => { | ||
| setUsers(data) | ||
| }); | ||
| } | ||
|
|
||
| const setUser = (username: string, phone: string, website: string) => { | ||
| return addUser(username, phone, website) | ||
| } | ||
|
|
||
| return { | ||
| users, | ||
| setUser, | ||
| setUserList | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export const ButtonWithLabel : FC = ( {label, ...restProps}: ButtonWithLabelProps) => {
В паттерне о этой говорится)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Внес правки