Skip to content

Commit

Permalink
Merge pull request #20 from pieceowater-dev/dev
Browse files Browse the repository at this point in the history
fix: bugs
  • Loading branch information
baynt1 authored Sep 12, 2024
2 parents f2528d5 + 52a9974 commit df47c89
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 42 deletions.
4 changes: 3 additions & 1 deletion src/entities/dashboard/payment-data/model/interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ReactNode } from 'react'

export interface IColumnPayments {
date: string
post: string
sum: number
id: number
status: string
status: ReactNode
type: string
}
6 changes: 4 additions & 2 deletions src/entities/dashboard/table-data/model/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ReactNode } from 'react'

export interface IRowsPaymentResponse {
id: number
datetime: number | string
createdAt: number | string
sum: string
type: number
result: number
Expand All @@ -16,6 +18,6 @@ export interface ITablePaymentsState {
post: string
sum: number
id: number
status: string
status: ReactNode
type: string
}
35 changes: 0 additions & 35 deletions src/entities/dashboard/table-data/table-data.ts

This file was deleted.

42 changes: 42 additions & 0 deletions src/entities/dashboard/table-data/table-data.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Tag } from 'antd'
import {
IRowsPaymentResponse,
ITablePaymentsState,
} from 'entities/dashboard/table-data/model/interface'
import { useEffect, useState } from 'react'
import { transformPrice } from 'shared/lib/functions/transform-price'
import { unixDate } from 'shared/lib/functions/unis-date'
import { useAppSelector } from 'shared/redux/store'

export const useTableData = () => {
const payments = useAppSelector((state) => state.dashboard.payments)
const [paymentTable, setPaymentTable] = useState<ITablePaymentsState[]>([])
const [paymentTableSum, setPaymentTableSum] = useState(0)
const totalPaymentTable = payments?.totals?.count ?? 0

useEffect(() => {
setPaymentTableSum(0)
const paymentsRows =
payments?.items?.map((row: IRowsPaymentResponse) => {
setPaymentTableSum((prevState) => prevState + transformPrice(row.sum))
return {
id: row.id,
status:
row.result === 0 ? (
<Tag color='success'>Успешный</Tag>
) : (
<Tag color='error'>Не успешный</Tag>
),
type: row.type === 0 ? 'Наличные' : 'Безналичные',
key: row.id,
date: row.createdAt ? unixDate(+row.createdAt * 1000, 'DMYHM') : '',
post: row.device.name,
sum: transformPrice(row.sum),
}
}) || []

setPaymentTable(paymentsRows)
}, [payments])

return { paymentTable, paymentTableSum, totalPaymentTable }
}
24 changes: 20 additions & 4 deletions src/entities/settings/posts-table/use-posts-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,21 @@ export const usePostsTable = () => {
}
}

const deletePost = async (postId: number, stop?: boolean) => {
const deletePost = async (postId: number, stop?: boolean, postState?: boolean) => {
const loading = (state: boolean) => (stop ? setStopLoading(state) : setDeleteLoading(state))
loading(true)

try {
const axiosInstance = await getAxiosInstance()

await axiosInstance.patch(`/posts/${postId}`, stop ? { stopped: true } : { deleted: true })
openNotification(`Пост ${stop ? 'приостановлен' : 'удален'}`, 'success')
await axiosInstance.patch(
`/posts/${postId}`,
stop ? { stopped: postState } : { deleted: true },
)
openNotification(
`Пост ${stop ? (!postState ? 'возобновлен' : 'приостановлен') : 'удален'}`,
'success',
)
fetchData()
loading(false)
} catch (error) {
Expand Down Expand Up @@ -133,7 +139,17 @@ export const usePostsTable = () => {
>
<EditOutlined />
</a>
<a onClick={() => (stopLoading ? null : deletePost(record.key, true))}>
<a
onClick={() =>
stopLoading
? null
: deletePost(
record.key,
true,
!stoppedPosts.filter((item) => item.id === record.key)[0].stopped,
)
}
>
{stopLoading ? (
<LoadingOutlined />
) : stoppedPosts.filter((item) => item.id === record.key)[0].stopped ? (
Expand Down
1 change: 1 addition & 0 deletions src/pages/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React, { FC } from 'react'
export const Dashboard: FC = () => {
const { fetchData, postSelect } = usePaymentData()
const { paymentTable, paymentTableSum, totalPaymentTable } = useTableData()

const columns: TableProps<IColumnPayments>['columns'] = [
{
title: 'Платеж №',
Expand Down
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default defineConfig({
svgr(),
],
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
alias: {
'@': path.resolve(__dirname, './src/'),
app: `${path.resolve(__dirname, './src/app/')}`,
Expand Down

0 comments on commit df47c89

Please sign in to comment.