friends page

This commit is contained in:
Primakov Alexandr Alexandrovich
2024-11-07 18:00:47 +03:00
parent f435d7b25c
commit db1fc8e634
8 changed files with 172 additions and 61 deletions

View File

@@ -0,0 +1,61 @@
import { useForm, Controller } from 'react-hook-form'
import { Box, Input } from '@chakra-ui/react'
import React, { useEffect, useRef } from 'react'
type Inputs = {
name: string
age: string
}
export const FormTest = () => {
const {
register,
control,
handleSubmit,
watch,
reset,
setValue,
formState: { errors },
} = useForm<Inputs>()
const [name, setName] = React.useState('')
const [age, setAge] = React.useState('12')
const ageRef = useRef(null)
useEffect(() => {
ageRef.current.focus()
}, [])
const onSibmit = ({ name, age }) => {
console.log(1111111, name, age)
}
console.log(1111111, 22222, watch().name)
return (
<Box
as="form"
flexDirection="column"
display="flex"
onSubmit={handleSubmit(onSibmit)}
>
<label>
Name:
<Controller
control={control}
name="name"
rules={{
required: 'required 4 now',
minLength: { value: 4, message: 'min 4 now' },
}}
render={({ field, fieldState, formState }) => <Input {...field} />}
/>
</label>
<label>
Age:
<input type="number" ref={ageRef} defaultValue={age} />
</label>
<button type="submit">Submit</button>
</Box>
)
}

View File

@@ -14,12 +14,11 @@ import {
Text,
VStack,
} from '@chakra-ui/react'
import { useTranslation } from 'react-i18next'
import { FaRegStar, FaStar } from 'react-icons/fa6'
import { Link, generatePath } from 'react-router-dom'
import React, { memo, useCallback, useState } from 'react'
import React, { memo, useState } from 'react'
import { URLs } from '../../__data__/urls'
import { FormTest } from './from'
import { Stars } from '../stars'
type User = Record<string, unknown> & {
id: string
@@ -39,15 +38,10 @@ export const Profile = ({
title?: string
}) => {
const [rated, setRated] = useState(user.rated || 0)
const [isTrue, setTrue] = useState(false)
const { t } = useTranslation()
const handleSubmit = useCallback(() => {
fetch('/#')
}, [])
return (
<Box mt={3} borderWidth="1px" p={3} overflowX="hidden">
{!isLink && <FormTest />}
<Heading as="h2">{title || 'Данные профиля'}</Heading>
<Box m={3}>
<Card width={'fit-content'} shadow="2xl">
@@ -57,53 +51,14 @@ export const Profile = ({
</Center>
</CardHeader>
<CardBody>
{isLink ? (
<Link to={generatePath(URLs.by.url, { userId: user.id })}>
Имя: {user.name.toUpperCase()}
{t('key.to.override', { name: user.name.toUpperCase() })}
</Link>
) : (
<Text fontWeight="bold">Имя: {user.name.toUpperCase()}</Text>
)}
<Text fontWeight="bold">Имя: {user.name.toUpperCase()}</Text>
</CardBody>
<CardFooter>
<HStack>
{Array.from({ length: 5 }).map((_, index) =>
index + 1 > rated ? (
<Icon
key={index}
color="orange.400"
cursor="pointer"
onClick={() => setRated(index + 1)}
>
<FaRegStar />
</Icon>
) : (
<Icon
key={index}
color="orange.400"
cursor="pointer"
onClick={() => setRated(index + 1)}
>
<FaStar />
</Icon>
)
)}
</HStack>
<Stars rated={rated} setRated={setRated} />
</CardFooter>
</Card>
{!isLink && <Form initialState="" onSubmit={handleSubmit} />}
<Box mt={3}>
{user.friends &&
user.friends.map((friend) => (
<Profile title="Друг" key={user.id} user={friend} isLink />
))}
</Box>
</Box>
{!isLink && (
<Box mt={3}>{isTrue ? <Counter key={1} /> : <Counter key={2} />}</Box>
)}
<Button onClick={() => setTrue(!isTrue)}>Switch</Button>
{!isLink && <Counter value={rated} setValue={setRated} />}
</Box>
)
}
@@ -152,15 +107,17 @@ const Form = memo<{ initialState: string; onSubmit(value: string): void }>(
Form.displayName = 'FormMemo'
const Counter = () => {
const [value, setValue] = useState(0)
const Counter = ({ value, setValue, horiaontal = false }) => {
const Wrapper = horiaontal ? HStack : VStack
return (
<VStack>
<Heading>{value}</Heading>
<Center>
<Wrapper>
<Heading>{value}</Heading>
<Button onClick={() => setValue(value + 1)}>+</Button>
<Button onClick={() => setValue(value - 1)}>-</Button>
</VStack>
<Button onClick={() => setValue(value + 1)}>+</Button>
<Button onClick={() => setValue(value - 1)}>-</Button>
</Wrapper>
</Center>
)
}

40
src/components/stars.tsx Normal file
View File

@@ -0,0 +1,40 @@
import {
HStack,
Icon,
} from '@chakra-ui/react'
import { FaRegStar, FaStar } from 'react-icons/fa6'
import React from 'react'
export const Stars = ({
rated,
setRated,
}: {
rated: number
setRated: (rate: number) => void
}) => {
return (
<HStack>
{Array.from({ length: 5 }).map((_, index) =>
index + 1 > rated ? (
<Icon
key={index}
color="orange.400"
cursor="pointer"
onClick={() => setRated(index + 1)}
>
<FaRegStar />
</Icon>
) : (
<Icon
key={index}
color="orange.400"
cursor="pointer"
onClick={() => setRated(index + 1)}
>
<FaStar />
</Icon>
),
)}
</HStack>
)
}

View File

@@ -6,13 +6,15 @@ import { URLs } from './__data__/urls'
import { NotFound } from './pages/not-found'
import { ByPage } from './pages/by'
import { Friends } from './pages/friends'
export const Dashboard = () => {
return (
<Routes>
<Route
path={URLs.baseUrl}
element={<Navigate replace to={URLs.toNotFound.url} />}
// element={<Navigate replace to={URLs.toNotFound.url} />}
element={<Friends />}
/>
<Route path={URLs.by.url} element={<Container maxW="container.xl"><ByPage /></Container>} />
<Route path="*" element={<NotFound />} />

33
src/pages/friends.tsx Normal file
View File

@@ -0,0 +1,33 @@
import { HStack } from '@chakra-ui/react'
import React from 'react'
import { Profile } from '../components'
const users = {
'some-user-id': {
id: 'some-user-id',
name: 'alexandr',
surname: null,
email: null,
rated: 3,
avatar:
'https://www.gravatar.com/avatar/6529e885535ef67a3fad810ad71167c2c03f79480936e9b3a714731753cbb47e?d=robohash',
},
'2': {
id: '2',
name: 'not alexandr',
surname: null,
email: null,
rated: 2,
avatar: 'https://www.gravatar.com/avatar/6e?d=robohash',
},
}
export const Friends = () => {
return (
<HStack>
<Profile user={users['some-user-id']} />
<Profile user={users['2']} />
</HStack>
)
}