feat: Add redeem points functionality and button for circle admin only admins

This commit is contained in:
Mo Tarbin 2025-01-14 02:03:42 -05:00
parent 1689e5eb9c
commit 1162d13dd8
2 changed files with 60 additions and 12 deletions

View file

@ -259,26 +259,31 @@ const UserActivites = () => {
} }
const generateHistoryPieChartData = history => { const generateHistoryPieChartData = history => {
const totalCompleted = history.filter( const totalCompleted =
item => item.dueDate > item.completedAt, history.filter(item => item.dueDate > item.completedAt).length || 0
).length const totalLate =
const totalLate = history.filter( history.filter(item => item.dueDate < item.completedAt).length || 0
item => item.dueDate < item.completedAt, const totalNoDueDate = history.filter(item => !item.dueDate).length || 0
).length
return [ return [
{ {
label: `On time`, label: `On time`,
value: totalCompleted, value: totalCompleted,
color: '#4ec1a2', color: TASK_COLOR.COMPLETED,
id: 1, id: 1,
}, },
{ {
label: `Late`, label: `Late`,
value: totalLate, value: totalLate,
color: '#f6ad55', color: TASK_COLOR.LATE,
id: 2, id: 2,
}, },
{
label: `Completed`,
value: totalNoDueDate,
color: TASK_COLOR.ANYTIME,
id: 3,
},
] ]
} }
if (isChoresHistoryLoading || isChoresLoading) { if (isChoresHistoryLoading || isChoresLoading) {

View file

@ -7,10 +7,11 @@ import {
YAxis, YAxis,
} from 'recharts' } from 'recharts'
import { Toll } from '@mui/icons-material' import { CreditCard, Toll } from '@mui/icons-material'
import { import {
Avatar, Avatar,
Box, Box,
Button,
Card, Card,
Chip, Chip,
Container, Container,
@ -27,11 +28,17 @@ import LoadingComponent from '../components/Loading.jsx'
import { useChoresHistory } from '../../queries/ChoreQueries.jsx' import { useChoresHistory } from '../../queries/ChoreQueries.jsx'
import { useCircleMembers } from '../../queries/UserQueries.jsx' import { useCircleMembers } from '../../queries/UserQueries.jsx'
import { RedeemPoints } from '../../utils/Fetcher.jsx'
import RedeemPointsModal from '../Modals/RedeemPointsModal'
const UserPoints = () => { const UserPoints = () => {
const [tabValue, setTabValue] = useState(7) const [tabValue, setTabValue] = useState(7)
const [isRedeemModalOpen, setIsRedeemModalOpen] = useState(false)
const { data: circleMembersData, isLoading: isCircleMembersLoading } = const {
useCircleMembers() data: circleMembersData,
isLoading: isCircleMembersLoading,
handleRefetch: handleCircleMembersRefetch,
} = useCircleMembers()
const { const {
data: choresHistoryData, data: choresHistoryData,
@ -208,7 +215,7 @@ const UserPoints = () => {
return yearlyAggregated return yearlyAggregated
} }
if (isChoresHistoryLoading || isCircleMembersLoading) { if (isChoresHistoryLoading || isCircleMembersLoading || !userProfile) {
return <LoadingComponent /> return <LoadingComponent />
} }
@ -233,6 +240,8 @@ const UserPoints = () => {
sx={{ sx={{
gap: 1, gap: 1,
my: 2, my: 2,
display: 'flex',
justifyContent: 'start',
}} }}
> >
<Select <Select
@ -278,6 +287,19 @@ const UserPoints = () => {
</Option> </Option>
))} ))}
</Select> </Select>
{circleUsers.find(user => user.userId === userProfile.id)?.role ===
'admin' && (
<Button
variant='soft'
size='md'
startDecorator={<CreditCard />}
onClick={() => {
setIsRedeemModalOpen(true)
}}
>
Redeem Points
</Button>
)}
</Box> </Box>
<Box <Box
@ -458,6 +480,27 @@ const UserPoints = () => {
</ResponsiveContainer> </ResponsiveContainer>
</Box> </Box>
</Box> </Box>
<RedeemPointsModal
config={{
onClose: () => {
setIsRedeemModalOpen(false)
},
isOpen: isRedeemModalOpen,
available: circleUsers.find(user => user.userId === selectedUser)
?.points,
user: circleUsers.find(user => user.userId === selectedUser),
onSave: ({ userId, points }) => {
RedeemPoints(userId, points, userProfile.circleID)
.then(res => {
setIsRedeemModalOpen(false)
handleCircleMembersRefetch()
})
.catch(err => {
console.log(err)
})
},
}}
/>
</Container> </Container>
) )
} }