Skip to content

Commit

Permalink
Merge pull request #10 from pieceowater-dev/dev
Browse files Browse the repository at this point in the history
fix: payment table
  • Loading branch information
baynt1 authored May 14, 2024
2 parents fc73296 + d6aa43f commit 26b5af1
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/entities/dashboard/payment-data/model/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface IColumnPayments {
key?: number
date: string
post: string
sum: string
Expand Down
2 changes: 1 addition & 1 deletion src/entities/dashboard/payment-data/payment-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const usePaymentData = () => {
const res = await axiosInstance.get('/payments')
dispatch(setPaymentsState(res.data))
} catch (error) {
openNotification('Произошла ошибка при загрузке данных о пользователях')
openNotification('Произошла ошибка при загрузке данных о платежах')
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/entities/dashboard/table-data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { useTableData } from 'entities/dashboard/table-data/table-data'

export { useTableData }
16 changes: 16 additions & 0 deletions src/entities/dashboard/table-data/model/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface IRowsPaymentResponse {
id: number
date: number
sum: string
device: {
id: number
name: string
}
}

export interface ITablePaymentsState {
key: number
date: string
post: string
sum: number
}
32 changes: 32 additions & 0 deletions src/entities/dashboard/table-data/table-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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

useEffect(() => {
setPaymentTableSum(0)
const paymentsRows = payments.items.map((row: IRowsPaymentResponse) => {
setPaymentTableSum((prevState) => prevState + transformPrice(row.sum))
return {
key: row.id,
date: unixDate(row.date * 1000, 'DMYHM'),
post: row.device.name,
sum: transformPrice(row.sum),
}
})

setPaymentTable(paymentsRows)
}, [payments])

return { paymentTable, paymentTableSum, totalPaymentTable }
}
13 changes: 7 additions & 6 deletions src/pages/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { Table } from 'antd'
import { usePaymentData } from 'entities/dashboard/payment-data'
import { useTableData } from 'entities/dashboard/table-data'
import { FC } from 'react'
import { useAppSelector } from 'shared/redux/store'

export const Dashboard: FC = () => {
const { fetchData, columns } = usePaymentData()
const payments = useAppSelector((state) => state.dashboard.payments)

console.log(payments)
const { paymentTable, paymentTableSum, totalPaymentTable } = useTableData()

return (
<>
<Table
columns={columns}
dataSource={[]}
dataSource={paymentTable}
bordered={true}
showHeader={true}
tableLayout={'fixed'}
pagination={{
total: 0,
total: totalPaymentTable,
}}
/>
<div style={{ display: 'flex', alignItems: 'end', justifyContent: 'end' }}>
{'Итого: ' + paymentTableSum}
</div>
</>
)
}
3 changes: 3 additions & 0 deletions src/shared/lib/functions/transform-price/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { transformPrice } from 'shared/lib/functions/transform-price/transform-price'

export { transformPrice }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const transformPrice = (number: string) => parseFloat(number.replace(/[$,]/g, ''))
3 changes: 3 additions & 0 deletions src/shared/lib/functions/unis-date/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { unixDate } from 'shared/lib/functions/unis-date/unix-date'

export { unixDate }
59 changes: 59 additions & 0 deletions src/shared/lib/functions/unis-date/unix-date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export const unixDate = (
value: string | number,
type: 'DHMS' | 'DM' | 'HM' | 'HMS' | 'DMY' | 'DMYNUM' | 'DMYHM',
inSeconds = false,
monthsArr = [
'янв.',
'фев.',
'мар ',
'апр.',
'май ',
'июнь',
'июль',
'авг.',
'сент.',
'окт.',
'нояб.',
'дек.',
],
): string => {
const date =
typeof value === 'string'
? new Date(+value * (inSeconds ? 1000 : 1))
: new Date(value * (inSeconds ? 1000 : 1))
const minutes = '0' + date.getMinutes()
const hours = '0' + date.getHours()
const seconds = '0' + date.getSeconds()
const month = '0' + date.getMonth()
const dayDate = '0' + date.getDate()

let day: number | string = date.getDay()
let minutesZero: number | string = date.getMinutes()
let secondsZero: number | string = date.getSeconds()
let hoursZero: number | string = date.getHours()

switch (type) {
case 'DHMS':
day = day > 0 ? day + ' дн.' : ''
hoursZero = hoursZero > 0 ? hoursZero + ' ч.' : ''
minutesZero = minutesZero > 0 ? minutesZero + ' мин.' : ''
secondsZero = secondsZero > 0 ? secondsZero + ' сек.' : ''
return `${day} ${hoursZero} ${minutesZero} ${secondsZero}`
case 'DM':
return `${date.getDate()} ${monthsArr[date.getMonth()]}`
case 'HM':
return `${hours.substr(-2)}:${minutes.substr(-2)}`
case 'HMS':
return `${hours.substr(-2)}:${minutes.substr(-2)}:${seconds.substr(-2)}`
case 'DMY':
return `${date.getDate()} ${monthsArr[date.getMonth()]} ${date.getFullYear()}`
case 'DMYNUM':
return `${dayDate.substr(-2)}.${month.substr(-2)}.${date.getFullYear()}`
case 'DMYHM':
return `${date.getDate()} ${monthsArr[date.getMonth()]} ${date.getFullYear()} ${hours.substr(
-2,
)}:${minutes.substr(-2)}`
default:
return 'Missing type param in func'
}
}
2 changes: 1 addition & 1 deletion src/shared/redux/dashboard/dashboard-slice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createSlice } from '@reduxjs/toolkit'

const initialState = {
payments: { items: [], total: { count: 0 } },
payments: { items: [], totals: { count: 0 } },
}

export const DashboardSlice = createSlice({
Expand Down

0 comments on commit 26b5af1

Please sign in to comment.