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 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
}

export interface Tag {
Expand Down
48 changes: 44 additions & 4 deletions src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { selectGoalsMap, updateGoal as updateGoalRedux } from '../../../store/go
import { useAppDispatch, useAppSelector } from '../../../store/hooks'
import DatePicker from '../../components/DatePicker'
import { Theme } from '../../components/Theme'
import GoalIcon from './GoalIcon'
import { Picker } from 'emoji-mart'
import 'emoji-mart/css/emoji-mart.css'

type Props = { goal: Goal }
export function GoalManager(props: Props) {
Expand All @@ -21,62 +24,102 @@ 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 ?? null)
}, [
props.goal.id,
props.goal.name,
props.goal.targetDate,
props.goal.targetAmount,
props.goal.icon,
])

useEffect(() => {
setName(goal.name)
}, [goal.name])
setIcon(goal.icon ?? null)
}, [goal.name, goal.icon])

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

const updatedGoal: Goal = {
...props.goal,
name: nextName,
icon: icon ?? props.goal.icon,
}

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

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,
icon: icon ?? props.goal.icon,
}

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

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,
icon: icon ?? props.goal.icon,
}

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

const pickEmojiOnClick = (emoji: any) => {
const selectedIcon = emoji.native

setIcon(selectedIcon)
setIsEmojiPickerOpen(false)

const updatedGoal: Goal = {
...props.goal,
icon: selectedIcon,
}

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

return (
<GoalManagerContainer>
<GoalIcon
icon={icon}
onClick={() => setIsEmojiPickerOpen(!isEmojiPickerOpen)}
/>

{isEmojiPickerOpen && (
<Picker
onSelect={pickEmojiOnClick}
/>
)}

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

<Group>
Expand Down Expand Up @@ -111,9 +154,6 @@ 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 }

const Field = (props: FieldProps) => (
<FieldContainer>
Expand Down
77 changes: 44 additions & 33 deletions src/ui/pages/Main/goals/GoalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,65 @@ import styled from 'styled-components'
import { selectGoalsMap } from '../../../../store/goalsSlice'
import { useAppDispatch, useAppSelector } from '../../../../store/hooks'
import {
setContent as setContentRedux,
setIsOpen as setIsOpenRedux,
setType as setTypeRedux
setContent as setContentRedux,
setIsOpen as setIsOpenRedux,
setType as setTypeRedux
} from '../../../../store/modalSlice'
import { Card } from '../../../components/Card'

type Props = { id: string }

export default function GoalCard(props: Props) {
const dispatch = useAppDispatch()
const dispatch = useAppDispatch()

const goal = useAppSelector(selectGoalsMap)[props.id]
const goal = useAppSelector(selectGoalsMap)[props.id]

const onClick = (event: React.MouseEvent) => {
event.stopPropagation()
dispatch(setContentRedux(goal))
dispatch(setTypeRedux('Goal'))
dispatch(setIsOpenRedux(true))
}
const onClick = (event: React.MouseEvent) => {
event.stopPropagation()
dispatch(setContentRedux(goal))
dispatch(setTypeRedux('Goal'))
dispatch(setIsOpenRedux(true))
}

const asLocaleDateString = (date: Date) => new Date(date).toLocaleDateString()
const asLocaleDateString = (date: Date) =>
new Date(date).toLocaleDateString()

return (
<Container key={goal.id} onClick={onClick}>
<TargetAmount>${goal.targetAmount}</TargetAmount>
<TargetDate>{asLocaleDateString(goal.targetDate)}</TargetDate>
</Container>
)
return (
<Container key={goal.id} onClick={onClick}>
<GoalEmoji>{goal.icon ?? '🎯'}</GoalEmoji>

<TargetAmount>${goal.targetAmount}</TargetAmount>

<TargetDate>
{asLocaleDateString(goal.targetDate)}
</TargetDate>
</Container>
)
}

const Container = styled(Card)`
display: flex;
flex-direction: column;
min-height: 140px;
min-width: 140px;
width: 33%;
cursor: pointer;
margin-left: 2rem;
margin-right: 2rem;
border-radius: 2rem;

align-items: center;
display: flex;
flex-direction: column;
min-height: 140px;
min-width: 140px;
width: 33%;
cursor: pointer;
margin-left: 2rem;
margin-right: 2rem;
border-radius: 2rem;
align-items: center;
`

const GoalEmoji = styled.h1`
font-size: 3rem;
margin-bottom: 1rem;
`

const TargetAmount = styled.h2`
font-size: 2rem;
font-size: 2rem;
`

const TargetDate = styled.h4`
color: rgba(174, 174, 174, 1);
font-size: 1rem;
`
color: rgba(174, 174, 174, 1);
font-size: 1rem;
`