2021-09-09 17:23:02 -07:00
|
|
|
import { useEffect, useState } from 'react'
|
2021-05-17 22:33:04 -07:00
|
|
|
import { RefreshClockwiseIcon } from './icons'
|
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
|
|
|
import Tooltip from './Tooltip'
|
2021-07-29 06:19:32 -07:00
|
|
|
import { IconButton } from './Button'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2021-05-17 22:33:04 -07:00
|
|
|
|
|
|
|
const ManualRefresh = ({ className = '' }) => {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-05-17 22:33:04 -07:00
|
|
|
const [spin, setSpin] = useState(false)
|
|
|
|
const actions = useMangoStore((s) => s.actions)
|
2021-12-17 01:51:54 -08:00
|
|
|
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
|
2021-05-17 22:33:04 -07:00
|
|
|
|
|
|
|
const handleRefreshData = async () => {
|
|
|
|
setSpin(true)
|
2021-09-09 17:23:02 -07:00
|
|
|
await actions.fetchMangoGroup()
|
2021-12-17 01:51:54 -08:00
|
|
|
if (mangoAccount) {
|
|
|
|
await actions.reloadMangoAccount()
|
|
|
|
actions.fetchTradeHistory()
|
|
|
|
}
|
2021-05-17 22:33:04 -07:00
|
|
|
}
|
|
|
|
|
2021-09-09 17:23:02 -07:00
|
|
|
useEffect(() => {
|
|
|
|
let timer
|
|
|
|
if (spin) {
|
2021-11-30 09:27:27 -08:00
|
|
|
timer = setTimeout(() => setSpin(false), 5000)
|
2021-09-09 17:23:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timer)
|
|
|
|
}
|
|
|
|
}, [spin])
|
|
|
|
|
2021-05-17 22:33:04 -07:00
|
|
|
return (
|
|
|
|
<div className={`inline-flex relative ${className}`}>
|
2021-10-20 05:42:40 -07:00
|
|
|
<Tooltip content={t('refresh-data')} className="text-xs py-1">
|
2021-09-09 17:23:02 -07:00
|
|
|
<IconButton onClick={handleRefreshData} disabled={spin}>
|
2021-05-17 22:33:04 -07:00
|
|
|
<RefreshClockwiseIcon
|
|
|
|
className={`w-4 h-4 ${spin ? 'animate-spin' : null}`}
|
|
|
|
/>
|
2021-07-29 06:19:32 -07:00
|
|
|
</IconButton>
|
2021-05-17 22:33:04 -07:00
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ManualRefresh
|