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 @@ -20,6 +20,7 @@ export interface Application {
export interface Goal {
id: string
name: string
icon: string | null
targetAmount: number
balance: number
targetDate: Date
Expand Down
143 changes: 129 additions & 14 deletions src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import { faCalendarAlt } from '@fortawesome/free-regular-svg-icons'
import { faDollarSign, IconDefinition } from '@fortawesome/free-solid-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 { BaseEmoji } from 'emoji-mart'
import 'date-fns'
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 EmojiPicker from '../../components/EmojiPicker'
import { Theme } from '../../components/Theme'
import AddIconButton from './AddIconButton'
import GoalIcon from './GoalIcon'

type Props = {
goal: Goal
}

type Props = { goal: Goal }
export function GoalManager(props: Props) {
const dispatch = useAppDispatch()

Expand All @@ -21,75 +35,146 @@ 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 [emojiPickerIsOpen, setEmojiPickerIsOpen] = 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])

const updateNameOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const hasIcon = () => icon != null && icon.trim() !== ''

const addIconOnClick = (event: React.MouseEvent) => {
event.stopPropagation()
setEmojiPickerIsOpen(true)
}

const pickEmojiOnClick = (
emoji: BaseEmoji,
event: React.MouseEvent
) => {
event.stopPropagation()

setIcon(emoji.native)
setEmojiPickerIsOpen(false)

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

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

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

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

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

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

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

return (
<GoalManagerContainer>
<NameInput value={name ?? ''} onChange={updateNameOnChange} />
<AddIconButton
hasIcon={hasIcon()}
onClick={addIconOnClick}
/>

<GoalIconContainer shouldShow={hasIcon()}>
<GoalIcon
icon={goal.icon}
onClick={addIconOnClick}
/>
</GoalIconContainer>

<EmojiPickerContainer
isOpen={emojiPickerIsOpen}
hasIcon={hasIcon()}
onClick={(event) => event.stopPropagation()}
>
<EmojiPicker onClick={pickEmojiOnClick} />
</EmojiPickerContainer>

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

<Group>
<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} />
<Value>
<StringInput value={targetAmount ?? ''} onChange={updateTargetAmountOnChange} />
<StringInput
value={targetAmount ?? ''}
onChange={updateTargetAmountOnChange}
/>
</Value>
</Group>

Expand All @@ -103,17 +188,28 @@ export function GoalManager(props: Props) {
<Group>
<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 }
type FieldProps = {
name: string
icon: IconDefinition
}

type GoalIconContainerProps = {
shouldShow: boolean
}

type EmojiPickerContainerProps = {
isOpen: boolean
hasIcon: boolean
}

const Field = (props: FieldProps) => (
<FieldContainer>
Expand All @@ -132,13 +228,29 @@ const GoalManagerContainer = styled.div`
position: relative;
`

const GoalIconContainer = styled.div<GoalIconContainerProps>`
display: ${(props) =>
props.shouldShow ? 'flex' : 'none'};
`

const EmojiPickerContainer =
styled.div<EmojiPickerContainerProps>`
display: ${(props) =>
props.isOpen ? 'flex' : 'none'};
position: absolute;
top: ${(props) =>
props.hasIcon ? '10rem' : '2rem'};
left: 0;
`

const Group = styled.div`
display: flex;
flex-direction: row;
width: 100%;
margin-top: 1.25rem;
margin-bottom: 1.25rem;
`

const NameInput = styled.input`
display: flex;
background-color: transparent;
Expand All @@ -155,6 +267,7 @@ const FieldName = styled.h1`
color: rgba(174, 174, 174, 1);
font-weight: normal;
`

const FieldContainer = styled.div`
display: flex;
flex-direction: row;
Expand All @@ -165,10 +278,12 @@ const FieldContainer = styled.div`
color: rgba(174, 174, 174, 1);
}
`

const StringValue = styled.h1`
font-size: 1.8rem;
font-weight: bold;
`

const StringInput = styled.input`
display: flex;
background-color: transparent;
Expand All @@ -181,4 +296,4 @@ const StringInput = styled.input`

const Value = styled.div`
margin-left: 2rem;
`
`
5 changes: 4 additions & 1 deletion src/ui/pages/Main/goals/GoalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function GoalCard(props: Props) {

return (
<Container key={goal.id} onClick={onClick}>
<Icon>{goal.icon}</Icon>
<TargetAmount>${goal.targetAmount}</TargetAmount>
<TargetDate>{asLocaleDateString(goal.targetDate)}</TargetDate>
</Container>
Expand All @@ -49,7 +50,9 @@ const Container = styled(Card)`
const TargetAmount = styled.h2`
font-size: 2rem;
`

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