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
17 changes: 9 additions & 8 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"@material-ui/core": "^4.12.4",
"@material-ui/pickers": "^3.3.10",
"@reduxjs/toolkit": "^1.5.1",
"@types/emoji-mart": "^3.0.5",
"@types/node": "^17.0.41",
"@types/react": "^16.9.0",
"@types/react-redux": "^7.1.7",
Expand Down Expand Up @@ -55,8 +54,8 @@
]
},
"devDependencies": {
"@types/emoji-mart": "^3.0.9",
"@types/emoji-mart": "^3.0.14",
"@types/react-dom": "^18.0.5",
"@types/styled-components": "^5.1.25"
}
}
}
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
130 changes: 112 additions & 18 deletions src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { Picker } from 'emoji-mart'
import 'emoji-mart/css/emoji-mart.css'
import AddIconButton from './AddIconButton'
import GoalIcon from './GoalIcon'
import { faCalendarAlt } from '@fortawesome/free-regular-svg-icons'
import { faDollarSign, IconDefinition } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
Expand All @@ -7,7 +11,10 @@ import React, { useEffect, useState } from 'react'
import styled from 'styled-components'
import { updateGoal as updateGoalApi } from '../../../api/lib'
import { Goal } from '../../../api/types'
import { selectGoalsMap, updateGoal as updateGoalRedux } from '../../../store/goalsSlice'
import {
selectGoalsMap,
updateGoal as updateGoalRedux,
} from '../../../store/goalsSlice'
import { useAppDispatch, useAppSelector } from '../../../store/hooks'
import DatePicker from '../../components/DatePicker'
import { Theme } from '../../components/Theme'
Expand All @@ -21,99 +28,186 @@ 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])
if (goal) {
setName(goal.name)
setIcon(goal.icon ?? null)
}
}, [goal?.name, goal?.icon])

const updateNameOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
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 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) => {
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 onEmojiSelect = (emoji: any) => {
setIcon(emoji.native)
setIsEmojiPickerOpen(false)

const updatedGoal: Goal = {
...props.goal,
name: name ?? props.goal.name,
targetDate: targetDate ?? props.goal.targetDate,
targetAmount: targetAmount ?? props.goal.targetAmount,
icon: emoji.native,
}

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

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

{icon ? (
<GoalIcon
icon={icon}
onClick={() =>
setIsEmojiPickerOpen(!isEmojiPickerOpen)
}
/>
) : (
<AddIconButton
hasIcon={false}
onClick={() =>
setIsEmojiPickerOpen(!isEmojiPickerOpen)
}
/>
)}

{isEmojiPickerOpen && (
<Picker
title="Pick your emoji"
onSelect={onEmojiSelect}
/>
)}

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

<Group>
<Field name="Target Date" icon={faCalendarAlt} />
<Field
name="Target Date"
icon={faCalendarAlt}
/>
<Value>
<DatePicker value={targetDate} onChange={pickDateOnChange} />
<DatePicker
value={targetDate}
onChange={pickDateOnChange}
/>
</Value>
</Group>

<Group>
<Field name="Target Amount" icon={faDollarSign} />
<Field
name="Target Amount"
icon={faDollarSign}
/>
<Value>
<StringInput value={targetAmount ?? ''} onChange={updateTargetAmountOnChange} />
<StringInput
value={targetAmount ?? ''}
onChange={updateTargetAmountOnChange}
/>
</Value>
</Group>

<Group>
<Field name="Balance" icon={faDollarSign} />
<Field
name="Balance"
icon={faDollarSign}
/>
<Value>
<StringValue>{props.goal.balance}</StringValue>
<StringValue>
{props.goal.balance}
</StringValue>
</Value>
</Group>

<Group>
<Field name="Date Created" icon={faCalendarAlt} />
<Field
name="Date Created"
icon={faCalendarAlt}
/>
<Value>
<StringValue>{new Date(props.goal.created).toLocaleDateString()}</StringValue>
<StringValue>
{new Date(
props.goal.created
).toLocaleDateString()}
</StringValue>
</Value>
</Group>

</GoalManagerContainer>
)
}

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
11 changes: 11 additions & 0 deletions src/ui/pages/Main/goals/GoalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export default function GoalCard(props: Props) {

return (
<Container key={goal.id} onClick={onClick}>
{goal.icon && <GoalIcon>{goal.icon}</GoalIcon>}
{goal.icon && <GoalEmoji>{goal.icon}</GoalEmoji>}
{goal.icon && <h1>{goal.icon}</h1>}
<TargetAmount>${goal.targetAmount}</TargetAmount>
<TargetDate>{asLocaleDateString(goal.targetDate)}</TargetDate>
</Container>
Expand All @@ -46,6 +49,14 @@ const Container = styled(Card)`

align-items: center;
`
const GoalIcon = styled.h1`
font-size: 3rem;
margin: 0;
`
const GoalEmoji = styled.h1`
font-size: 3rem;
margin: 0;
`
const TargetAmount = styled.h2`
font-size: 2rem;
`
Expand Down