Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions src/api/lib.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import axios from 'axios'
import { user } from '../data/user'
import { API_ROOT, getGoals } from './lib'
import { Goal } from './types'

jest.mock('axios')

const mockedAxios = axios as jest.Mocked<typeof axios>

describe('getGoals', () => {
afterEach(() => {
jest.clearAllMocks()
})

it('calls GetGoalsForUser route and returns goals for the current user', async () => {
const goals: Goal[] = [
{
id: 'goal-1',
name: 'Holiday',
targetAmount: 2500,
balance: 100,
targetDate: new Date('2027-01-01'),
created: new Date('2026-01-01'),
accountId: 'account-1',
transactionIds: [],
tagIds: [],
icon: '🚀',
},
{
id: 'goal-2',
name: 'Emergency',
targetAmount: 5000,
balance: 800,
targetDate: new Date('2027-06-01'),
created: new Date('2026-02-01'),
accountId: 'account-2',
transactionIds: [],
tagIds: [],
icon: '🎯',
},
]

mockedAxios.get.mockResolvedValueOnce({ data: goals })

const result = await getGoals()

expect(mockedAxios.get).toHaveBeenCalledWith(`${API_ROOT}/api/Goal/User/${user.id}`)
expect(result).not.toBeNull()

for (const goal of result ?? []) {
expect(goal).toEqual(expect.any(Object))
expect(goal).toEqual(expect.objectContaining({ id: expect.any(String) }))
}
})

it('returns null when request fails', async () => {
mockedAxios.get.mockRejectedValueOnce(new Error('request failed'))

const result = await getGoals()

expect(result).toBeNull()
})
})
1 change: 1 addition & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface Goal {
accountId: string
transactionIds: string[]
tagIds: string[]
icon: string | null
}

export interface Tag {
Expand Down
6 changes: 3 additions & 3 deletions src/ui/components/EmojiPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BaseEmoji, Picker } from 'emoji-mart'
import { EmojiData, Picker } from 'emoji-mart'
import 'emoji-mart/css/emoji-mart.css'
import { useAppSelector } from '../../store/hooks'
import { selectMode } from '../../store/themeSlice'

type Props = { onClick: (emoji: BaseEmoji, event: React.MouseEvent) => void }
type Props = { onClick: (emoji: EmojiData) => void }

export default function EmojiPicker(props: Props) {
const theme = useAppSelector(selectMode)
Expand All @@ -13,7 +13,7 @@ export default function EmojiPicker(props: Props) {
theme={theme}
showPreview={false}
showSkinTones={false}
onClick={props.onClick}
onClick={(emoji) => props.onClick(emoji)}
color="primary"
/>
)
Expand Down
82 changes: 56 additions & 26 deletions src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { faCalendarAlt } from '@fortawesome/free-regular-svg-icons'
import { faDollarSign, IconDefinition } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { MaterialUiPickersDate } from '@material-ui/pickers/typings/date'
import { EmojiData } from 'emoji-mart'
import 'date-fns'
import React, { useEffect, useState } from 'react'
import styled from 'styled-components'
Expand All @@ -10,7 +11,10 @@ import { Goal } from '../../../api/types'
import { selectGoalsMap, updateGoal as updateGoalRedux } from '../../../store/goalsSlice'
import { useAppDispatch, useAppSelector } from '../../../store/hooks'
import DatePicker from '../../components/DatePicker'
import EmojiPicker from '../../components/EmojiPicker'
import { Theme } from '../../components/Theme'
import AddIconButton from './AddIconButton'
import GoalIcon from './GoalIcon'

type Props = { goal: Goal }
export function GoalManager(props: Props) {
Expand All @@ -21,64 +25,81 @@ export function GoalManager(props: Props) {
const [name, setName] = useState<string | null>(null)
const [targetDate, setTargetDate] = useState<Date | null>(null)
const [targetAmount, setTargetAmount] = useState<number | null>(null)
const [icon, setIcon] = useState<string | null>(null)
const [isEmojiPickerOpen, setIsEmojiPickerOpen] = useState(false)

useEffect(() => {
setName(props.goal.name)
setTargetDate(props.goal.targetDate)
setTargetAmount(props.goal.targetAmount)
setIcon(props.goal.icon)
}, [
props.goal.id,
props.goal.name,
props.goal.targetDate,
props.goal.targetAmount,
props.goal.icon,
])

useEffect(() => {
setName(goal.name)
}, [goal.name])
setTargetDate(goal.targetDate)
setTargetAmount(goal.targetAmount)
setIcon(goal.icon)
}, [goal.name, goal.targetDate, goal.targetAmount, goal.icon])

const updateNameOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const nextName = event.target.value
setName(nextName)
const persistGoal = (updates: Partial<Goal>) => {
const updatedGoal: Goal = {
...props.goal,
name: nextName,
...goal,
...updates,
}

dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
updateGoalApi(updatedGoal.id, updatedGoal)
}

const updateNameOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const nextName = event.target.value
setName(nextName)
persistGoal({ name: nextName })
}

const updateTargetAmountOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const nextTargetAmount = parseFloat(event.target.value)
setTargetAmount(nextTargetAmount)
const updatedGoal: Goal = {
...props.goal,
name: name ?? props.goal.name,
targetDate: targetDate ?? props.goal.targetDate,
targetAmount: nextTargetAmount,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
persistGoal({ targetAmount: nextTargetAmount })
}

const pickDateOnChange = (date: MaterialUiPickersDate) => {
if (date != null) {
setTargetDate(date)
const updatedGoal: Goal = {
...props.goal,
name: name ?? props.goal.name,
targetDate: date ?? props.goal.targetDate,
targetAmount: targetAmount ?? props.goal.targetAmount,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
persistGoal({ targetDate: date })
}
}

const toggleEmojiPicker = () => setIsEmojiPickerOpen((current) => !current)

const pickEmojiOnClick = (emoji: EmojiData) => {
if (!("native" in emoji)) return

setIsEmojiPickerOpen(false)
setIcon(emoji.native)
persistGoal({ icon: emoji.native })
}

return (
<GoalManagerContainer>
<NameInput value={name ?? ''} onChange={updateNameOnChange} />

<IconsContainer>
<AddIconButton hasIcon={icon != null} onClick={toggleEmojiPicker} />
{icon != null && <GoalIcon icon={icon} onClick={toggleEmojiPicker} />}
</IconsContainer>

<EmojiPickerContainer isOpen={isEmojiPickerOpen}>
<EmojiPicker onClick={pickEmojiOnClick} />
</EmojiPickerContainer>

<Group>
<Field name="Target Date" icon={faCalendarAlt} />
<Value>
Expand Down Expand Up @@ -111,9 +132,7 @@ export function GoalManager(props: Props) {
}

type FieldProps = { name: string; icon: IconDefinition }
type AddIconButtonContainerProps = { shouldShow: boolean }
type GoalIconContainerProps = { shouldShow: boolean }
type EmojiPickerContainerProps = { isOpen: boolean; hasIcon: boolean }
type EmojiPickerContainerProps = { isOpen: boolean }

const Field = (props: FieldProps) => (
<FieldContainer>
Expand Down Expand Up @@ -149,6 +168,17 @@ const NameInput = styled.input`
color: ${({ theme }: { theme: Theme }) => theme.text};
`

const IconsContainer = styled.div`
display: flex;
flex-direction: row;
align-items: center;
`

const EmojiPickerContainer = styled.div`
display: ${({ isOpen }: EmojiPickerContainerProps) => (isOpen ? 'block' : 'none')};
margin-top: 1.5rem;
`

const FieldName = styled.h1`
font-size: 1.8rem;
margin-left: 1rem;
Expand Down
9 changes: 7 additions & 2 deletions src/ui/pages/Main/goals/GoalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useAppDispatch, useAppSelector } from '../../../../store/hooks'
import {
setContent as setContentRedux,
setIsOpen as setIsOpenRedux,
setType as setTypeRedux
setType as setTypeRedux,
} from '../../../../store/modalSlice'
import { Card } from '../../../components/Card'

Expand All @@ -29,6 +29,7 @@ export default function GoalCard(props: Props) {
<Container key={goal.id} onClick={onClick}>
<TargetAmount>${goal.targetAmount}</TargetAmount>
<TargetDate>{asLocaleDateString(goal.targetDate)}</TargetDate>
{goal.icon != null && <Icon>{goal.icon}</Icon>}
</Container>
)
}
Expand All @@ -43,9 +44,9 @@ const Container = styled(Card)`
margin-left: 2rem;
margin-right: 2rem;
border-radius: 2rem;

align-items: center;
`

const TargetAmount = styled.h2`
font-size: 2rem;
`
Expand All @@ -54,3 +55,7 @@ const TargetDate = styled.h4`
color: rgba(174, 174, 174, 1);
font-size: 1rem;
`

const Icon = styled.h1`
font-size: 5.5rem;
`