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
3 changes: 2 additions & 1 deletion package-lock.json

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

3 changes: 1 addition & 2 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 @@ -59,4 +58,4 @@
"@types/react-dom": "^18.0.5",
"@types/styled-components": "^5.1.25"
}
}
}
1 change: 1 addition & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'emoji-mart/css/emoji-mart.css';
import { createTheme, ThemeProvider as ThemeProviderMui } from '@material-ui/core'
import React, { useEffect } from 'react'
import styled, { ThemeProvider } from 'styled-components'
Expand Down
1 change: 1 addition & 0 deletions src/api/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ export async function updateGoal(goalId: string, updatedGoal: Goal): Promise<boo
return false
}
}

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
95 changes: 91 additions & 4 deletions src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,54 @@ import { faDollarSign, IconDefinition } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { MaterialUiPickersDate } from '@material-ui/pickers/typings/date'
import 'date-fns'
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, 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 { useAppDispatch, useAppSelector } from '../../../store/hooks'
import DatePicker from '../../components/DatePicker'
import { Theme } from '../../components/Theme'
import { BaseEmoji, Picker } from 'emoji-mart'
import 'emoji-mart/css/emoji-mart.css'
import { selectMode } from '../../../store/themeSlice'

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

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

const themeMode = useAppSelector(selectMode)

const [name, setName] = useState<string | null>(null)
const [targetDate, setTargetDate] = useState<Date | null>(null)
const [targetAmount, setTargetAmount] = useState<number | null>(null)
const [selectedIcon, setSelectedIcon] = useState<string>('🎯')
const [showEmojiPicker, setShowEmojiPicker] = useState(false)
const emojiPickerRef = useRef<HTMLDivElement>(null)

// Close emoji picker when clicking outside
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (emojiPickerRef.current && !emojiPickerRef.current.contains(event.target as Node)) {
setShowEmojiPicker(false)
}
}
document.addEventListener('mousedown', handleClickOutside)
return () => document.removeEventListener('mousedown', handleClickOutside)
}, [])

useEffect(() => {
setName(props.goal.name)
setTargetDate(props.goal.targetDate)
setTargetAmount(props.goal.targetAmount)
setSelectedIcon(props.goal.icon || '🎯')

}, [
props.goal.id,
props.goal.name,
props.goal.icon,
props.goal.targetDate,
props.goal.targetAmount,
])
Expand Down Expand Up @@ -75,8 +97,40 @@ export function GoalManager(props: Props) {
}
}

const updateIconOnSelect = (emoji: BaseEmoji, event: React.MouseEvent) => {
event.stopPropagation()
const nextIcon = emoji.native
setSelectedIcon(nextIcon)
setShowEmojiPicker(false)
const updatedGoal: Goal = {
...props.goal,
name: name ?? props.goal.name,
targetDate: targetDate ?? props.goal.targetDate,
targetAmount: targetAmount ?? props.goal.targetAmount,
icon: nextIcon,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
}

return (
<GoalManagerContainer>
<IconRow>
<IconButton onClick={(e) => { e.stopPropagation(); setShowEmojiPicker(!showEmojiPicker) }}>
{selectedIcon}
</IconButton>
{showEmojiPicker && (
<EmojiPickerDropdown ref={emojiPickerRef} onClick={(e) => e.stopPropagation()}>
<Picker
theme={themeMode}
showPreview={false}
showSkinTones={false}
onClick={updateIconOnSelect}
color="rgb(22,115,255)"
/>
</EmojiPickerDropdown>
)}
</IconRow>
<NameInput value={name ?? ''} onChange={updateNameOnChange} />

<Group>
Expand Down Expand Up @@ -111,9 +165,42 @@ 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 IconRow = styled.div`
display: flex;
flex-direction: row;
align-items: center;
position: relative;
margin-bottom: 0.5rem;
`

const IconButton = styled.button`
font-size: 3rem;
background: none;
border: 2px solid transparent;
border-radius: 0.75rem;
cursor: pointer;
padding: 0.25rem 0.5rem;
transition: border-color 0.2s ease, transform 0.15s ease;
line-height: 1;

&:hover {
border-color: rgba(174, 174, 174, 0.4);
transform: scale(1.1);
}
`

const EmojiPickerDropdown = styled.div`
position: absolute;
top: 100%;
left: 0;
z-index: 100;
margin-top: 0.25rem;
box-shadow: 0px 8px 24px rgba(0, 0, 0, 0.25);
border-radius: 0.75rem;
overflow: hidden;
`


const Field = (props: FieldProps) => (
<FieldContainer>
Expand Down
4 changes: 4 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}>
{/* Add the icon here. Using a default emoji if none is set yet! */}
<div style={{ fontSize: '2rem', marginBottom: '8px' }}>
{goal.icon ? goal.icon : '🎯'}
</div>
<TargetAmount>${goal.targetAmount}</TargetAmount>
<TargetDate>{asLocaleDateString(goal.targetDate)}</TargetDate>
</Container>
Expand Down