Skip to content

Commit

Permalink
Merge pull request #8 from pieceowater-dev/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
baynt1 authored May 8, 2024
2 parents a544654 + 635fd8d commit 24a43d3
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/features/settings/new-post/model/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ export interface INewPostFormArgs {
address: string
identifier: string
id?: number
users?: number[]
}

export interface IUsersForPostsResponse {
id: number
user: {
id: number
name: string
}
}
39 changes: 36 additions & 3 deletions src/features/settings/new-post/new-post.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Button, Drawer, Form, FormProps, Input, Select } from 'antd'
import { useNotify } from 'app/providers/app'
import { INewPostFormArgs, INewPostProps } from 'features/settings/new-post/model/interface'
import { INewUserFormArgs } from 'features/settings/new-user/model/interface'
import {
INewPostFormArgs,
INewPostProps,
IUsersForPostsResponse,
} from 'features/settings/new-post/model/interface'
import React, { FC, useEffect, useState } from 'react'
import { getAxiosInstance } from 'shared/api/api-query/api-query'
import { useAppSelector } from 'shared/redux/store'
Expand All @@ -13,21 +16,51 @@ export const NewPost: FC<INewPostProps> = ({ open, handeOpen, item, refetch }) =

const options = useAppSelector((state) => state.settings.users)

const fetchData = async () => {
try {
const axiosInstance = await getAxiosInstance()
if (item && item.id)
await axiosInstance.get(`/posts-users-access/post/${item.id}`).then((res) => {
form.setFieldValue(
'users',
res.data.map((item: IUsersForPostsResponse) => {
return {
label: item.user.name,
value: item.user.id,
}
}),
)
})
} catch (error) {
openNotification('Что-то пошло не так')
}
}

useEffect(() => {
form.resetFields()
if (open) fetchData()
}, [item, open])

const onFinish: FormProps<INewPostFormArgs>['onFinish'] = async (data) => {
setLoading(true)
try {
const axiosInstance = await getAxiosInstance()
if (item && item.id) {
const users = data.users
? data.users.map((user) => {
return {
post: item.id,
user: user,
}
})
: []
await axiosInstance.patch(`/posts/${item.id}`, data).then(() => {
openNotification('Пост изменен', 'success')
handeOpen()
refetch()
setLoading(false)
})
await axiosInstance.post('/posts-users-access', users)
} else {
await axiosInstance.post('/posts', data).then(() => {
openNotification('Пост создан', 'success')
Expand Down Expand Up @@ -79,7 +112,7 @@ export const NewPost: FC<INewPostProps> = ({ open, handeOpen, item, refetch }) =
<Input />
</Form.Item>

<Form.Item<INewUserFormArgs> label={'Посты'} name={'posts'}>
<Form.Item<INewPostFormArgs> label={'Посты'} name={'users'}>
<Select
mode={'multiple'}
allowClear={true}
Expand Down
8 changes: 8 additions & 0 deletions src/features/settings/new-user/model/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ export interface INewUserFormArgs {
id?: number
posts?: number[]
}

export interface IPostsForUsersResponse {
id: number
post: {
id: number
name: string
}
}
37 changes: 36 additions & 1 deletion src/features/settings/new-user/new-user.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Button, Drawer, Form, FormProps, Input, Select } from 'antd'
import { useNotify } from 'app/providers/app'
import { INewUserFormArgs, INewUserProps } from 'features/settings/new-user/model/interface'
import {
INewUserFormArgs,
INewUserProps,
IPostsForUsersResponse,
} from 'features/settings/new-user/model/interface'
import React, { FC, useEffect, useState } from 'react'
import { getAxiosInstance } from 'shared/api/api-query/api-query'
import { useAppSelector } from 'shared/redux/store'
Expand All @@ -17,12 +21,22 @@ export const NewUser: FC<INewUserProps> = ({ open, handleModal, item, refetch })
try {
const axiosInstance = await getAxiosInstance()
if (item && item.id) {
const posts = data.posts
? data.posts.map((post) => {
return {
post: post,
user: item.id,
}
})
: []

await axiosInstance.patch(`/users/${item.id}`, data).then(() => {
openNotification('Пользователь изменен', 'success')
handleModal()
refetch()
setLoading(false)
})
await axiosInstance.post('/posts-users-access', posts)
} else {
await axiosInstance.post('/users', data).then(() => {
openNotification('Пользователь создан', 'success')
Expand All @@ -37,8 +51,29 @@ export const NewUser: FC<INewUserProps> = ({ open, handleModal, item, refetch })
}
}

const fetchData = async () => {
try {
const axiosInstance = await getAxiosInstance()
if (item && item.id)
await axiosInstance.get(`/posts-users-access/user/${item.id}`).then((res) => {
form.setFieldValue(
'posts',
res.data.map((item: IPostsForUsersResponse) => {
return {
label: item.post.name,
value: item.post.id,
}
}),
)
})
} catch (error) {
openNotification('Что-то пошло не так')
}
}

useEffect(() => {
form.resetFields()
fetchData()
}, [item, open])

return (
Expand Down
18 changes: 13 additions & 5 deletions src/widget/header/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ export const Header: FC = () => {
borderBottomRightRadius: '8px',
}}
>
<img
src={'/logo.png'}
style={{ width: '30px', cursor: 'pointer' }}
<div
style={{
display: 'flex',
gap: '15px',
alignItems: 'center',
color: '#eeeeee',
cursor: 'pointer',
}}
onClick={() => navigator('/')}
alt={'logo'}
/>
>
<img src={'/logo.png'} style={{ width: '30px' }} alt={'logo'} />
Grands-Pay
</div>

<Flex gap={'15px'}>
<Space
style={{ cursor: 'pointer', color: '#eeeeee' }}
Expand Down

0 comments on commit 24a43d3

Please sign in to comment.