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
145 changes: 120 additions & 25 deletions src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { faCalendarAlt } from '@fortawesome/free-regular-svg-icons'
import { faDollarSign, IconDefinition } from '@fortawesome/free-solid-svg-icons'
import { faDollarSign, faSmile } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { MaterialUiPickersDate } from '@material-ui/pickers/typings/date'
import 'date-fns'
Expand All @@ -12,31 +12,79 @@ import { useAppDispatch, useAppSelector } from '../../../store/hooks'
import DatePicker from '../../components/DatePicker'
import { Theme } from '../../components/Theme'

// --- IMPORT EMOJI PICKER ---
import EmojiPicker from '../../components/EmojiPicker'
import { BaseEmoji } from 'emoji-mart'
// --- IMPORT TRANSPARENT BUTTON ---
import { TransparentButton } from '../../components/TransparentButton'


const BigIcon = styled.h1`
font-size: 5.5rem;
cursor: pointer;
`

function GoalIcon({ icon, onClick }: { icon: string, onClick: (e: React.MouseEvent) => void }) {
return (
<TransparentButton onClick={onClick}>
<BigIcon>{icon}</BigIcon>
</TransparentButton>
)
}

type Props = { goal: Goal }

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

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

// Form state
const [name, setName] = useState<string | null>(null)
const [targetDate, setTargetDate] = useState<Date | null>(null)
const [targetAmount, setTargetAmount] = useState<number | null>(null)

useEffect(() => {
setName(props.goal.name)
setTargetDate(props.goal.targetDate)
setTargetAmount(props.goal.targetAmount)
}, [
props.goal.id,
props.goal.name,
props.goal.targetDate,
props.goal.targetAmount,
])
// Icon state
const [icon, setIcon] = useState<string | null>(props.goal.icon ?? null)
// Emoji picker open/close
const [emojiPickerIsOpen, setEmojiPickerIsOpen] = useState(false)

useEffect(() => {
setName(goal.name)
}, [goal.name])
setTargetDate(goal.targetDate)
setTargetAmount(goal.targetAmount)
setIcon(goal.icon ?? null)
}, [goal.id, goal.name, goal.targetDate, goal.targetAmount, goal.icon])

// Inline handlers
const hasIcon = () => icon != null

// --- HERE'S THE MODIFIED HANDLER ---
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)
}

// Add icon button
const addIconOnClick = (event: React.MouseEvent) => {
event.stopPropagation()
setEmojiPickerIsOpen(true)
}
// Icon click (change)
const iconOnClick = (event: React.MouseEvent) => {
event.stopPropagation()
setEmojiPickerIsOpen(true)
}

// Other update handlers (unchanged)
const updateNameOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const nextName = event.target.value
setName(nextName)
Expand Down Expand Up @@ -67,7 +115,7 @@ export function GoalManager(props: Props) {
const updatedGoal: Goal = {
...props.goal,
name: name ?? props.goal.name,
targetDate: date ?? props.goal.targetDate,
targetDate: date,
targetAmount: targetAmount ?? props.goal.targetAmount,
}
dispatch(updateGoalRedux(updatedGoal))
Expand All @@ -78,28 +126,49 @@ export function GoalManager(props: Props) {
return (
<GoalManagerContainer>
<NameInput value={name ?? ''} onChange={updateNameOnChange} />

{/* --- ICON/EMOJI ADD & CHANGE UI --- */}
<Group>
{/* Show icon if exists */}
<GoalIconContainer shouldShow={!!icon}>
{icon && (
<GoalIcon icon={icon} onClick={iconOnClick} />
)}
</GoalIconContainer>
{/* Show add icon button if missing */}
<AddIconButtonContainer shouldShow={!icon}>
<TransparentButton onClick={addIconOnClick}>
<FontAwesomeIcon icon={faSmile} size="2x" />
<AddIconButtonText>Add icon</AddIconButtonText>
</TransparentButton>
</AddIconButtonContainer>
{/* Emoji Picker as popover/modal */}
<EmojiPickerContainer
isOpen={emojiPickerIsOpen}
hasIcon={!!icon}
onClick={e => e.stopPropagation()}
>
<EmojiPicker onClick={pickEmojiOnClick()} />
</EmojiPickerContainer>
</Group>
{/* --- REST OF GOAL FIELDS --- */}
<Group>
<Field name="Target Date" icon={faCalendarAlt} />
<Value>
<DatePicker value={targetDate} onChange={pickDateOnChange} />
</Value>
</Group>

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

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

<Group>
<Field name="Date Created" icon={faCalendarAlt} />
<Value>
Expand All @@ -110,18 +179,44 @@ 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 }
// --- FIELD AND STYLES BELOW ---

type FieldProps = { name: string; icon: any }
const Field = (props: FieldProps) => (
<FieldContainer>
<FontAwesomeIcon icon={props.icon} size="2x" />
<FieldName>{props.name}</FieldName>
</FieldContainer>
)

type AddIconButtonContainerProps = { shouldShow: boolean }
const AddIconButtonContainer = styled.div<AddIconButtonContainerProps>`
display: ${(props) => (props.shouldShow ? 'flex' : 'none')};
align-items: center;
`

type GoalIconContainerProps = { shouldShow: boolean }
const GoalIconContainer = styled.div<GoalIconContainerProps>`
display: ${(props) => (props.shouldShow ? 'flex' : 'none')};
align-items: center;
min-width: 3rem;
margin-right: 1rem;
`

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

const AddIconButtonText = styled.span`
font-size: 1.2rem;
margin-left: 0.5rem;
`

const GoalManagerContainer = styled.div`
display: flex;
flex-direction: column;
Expand All @@ -131,7 +226,6 @@ const GoalManagerContainer = styled.div`
width: 100%;
position: relative;
`

const Group = styled.div`
display: flex;
flex-direction: row;
Expand Down Expand Up @@ -178,7 +272,8 @@ const StringInput = styled.input`
font-weight: bold;
color: ${({ theme }: { theme: Theme }) => theme.text};
`

const Value = styled.div`
margin-left: 2rem;
`

export default GoalManager
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 @@ -29,6 +29,7 @@ export default function GoalCard(props: Props) {
<Container key={goal.id} onClick={onClick}>
<TargetAmount>${goal.targetAmount}</TargetAmount>
<TargetDate>{asLocaleDateString(goal.targetDate)}</TargetDate>
<Icon>{goal.icon}</Icon>
</Container>
)
}
Expand All @@ -54,3 +55,6 @@ const TargetDate = styled.h4`
color: rgba(174, 174, 174, 1);
font-size: 1rem;
`
const Icon = styled.h1`
font-size: 5.5rem;
`