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.

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
105 changes: 92 additions & 13 deletions src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,54 @@ 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 'emoji-mart/css/emoji-mart.css'
import { Picker } from 'emoji-mart'

type Props = { goal: Goal }

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

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

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>(goal?.icon || '')
const [showPicker, setShowPicker] = 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 pushUpdate = (updated: Goal) => {
dispatch(updateGoalRedux(updated))
updateGoalApi(props.goal.id, updated)
}

const updateNameOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const nextName = event.target.value
setName(nextName)
const updatedGoal: Goal = {
...props.goal,
name: nextName,
targetDate: targetDate ?? props.goal.targetDate,
targetAmount: targetAmount ?? props.goal.targetAmount,
icon: icon || undefined,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
pushUpdate(updatedGoal)
}

const updateTargetAmountOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand All @@ -56,28 +69,60 @@ export function GoalManager(props: Props) {
name: name ?? props.goal.name,
targetDate: targetDate ?? props.goal.targetDate,
targetAmount: nextTargetAmount,
icon: icon || undefined,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
pushUpdate(updatedGoal)
}

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

const onEmojiSelect = (emoji: any) => {
const nextIcon = emoji?.native ?? ''
setIcon(nextIcon)
setShowPicker(false)

const updatedGoal: Goal = {
...props.goal,
name: name ?? props.goal.name,
targetDate: targetDate ?? props.goal.targetDate,
targetAmount: targetAmount ?? props.goal.targetAmount,
icon: nextIcon || undefined,
}
pushUpdate(updatedGoal)
}

return (
<GoalManagerContainer>
<NameInput value={name ?? ''} onChange={updateNameOnChange} />
<TitleRow>
<NameInput value={name ?? ''} onChange={updateNameOnChange} />
<IconArea>
<AddIconButton onClick={() => setShowPicker(!showPicker)}>
{icon ? <span style={{ fontSize: '2rem' }}>{icon}</span> : 'Choose Emoji'}
</AddIconButton>
{showPicker && (
<EmojiPickerContainer>
<Picker
onSelect={onEmojiSelect}
title="Pick your goal icon"
emoji="sparkles"
showPreview={false}
/>
</EmojiPickerContainer>
)}
</IconArea>
</TitleRow>

<Group>
<Field name="Target Date" icon={faCalendarAlt} />
Expand Down Expand Up @@ -111,9 +156,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 All @@ -132,13 +174,47 @@ const GoalManagerContainer = styled.div`
position: relative;
`

const TitleRow = styled.div`
display: flex;
align-items: center;
gap: 1rem;
width: 100%;
`

const IconArea = styled.div`
position: relative;
display: inline-flex;
align-items: center;
`

const AddIconButton = styled.button`
padding: 0.4rem 0.8rem;
font-size: 1rem;
border-radius: 8px;
border: 1px solid rgba(174, 174, 174, 0.6);
background: transparent;
color: ${({ theme }: { theme: Theme }) => theme.text};
cursor: pointer;
`

const EmojiPickerContainer = styled.div`
position: absolute;
top: 2.8rem;
right: 0;
z-index: 1000;
box-shadow: 0 8px 24px rgba(0,0,0,0.2);
border-radius: 12px;
overflow: hidden;
`

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 +231,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 +242,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 Down
18 changes: 18 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,10 @@ export default function GoalCard(props: Props) {

return (
<Container key={goal.id} onClick={onClick}>
<GoalTitle>
{goal.icon && <IconSpan>{goal.icon}</IconSpan>}
{goal.name}
</GoalTitle>
<TargetAmount>${goal.targetAmount}</TargetAmount>
<TargetDate>{asLocaleDateString(goal.targetDate)}</TargetDate>
</Container>
Expand All @@ -46,6 +50,20 @@ const Container = styled(Card)`

align-items: center;
`

const GoalTitle = styled.h3`
font-size: 1.4rem;
margin-bottom: 0.5rem;
display: flex;
align-items: center;
justify-content: center;
`

const IconSpan = styled.span`
font-size: 1.8rem;
margin-right: 0.4rem;
`

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