From 1a84b514784325454cba936b7772d1e78a1a8dee Mon Sep 17 00:00:00 2001 From: amaldev75 Date: Fri, 12 Jun 2026 16:25:02 +0530 Subject: [PATCH] Add goal icons support with emoji picker --- src/ui/features/goalmanager/GoalManager.tsx | 344 ++++++++++++++++---- 1 file changed, 282 insertions(+), 62 deletions(-) diff --git a/src/ui/features/goalmanager/GoalManager.tsx b/src/ui/features/goalmanager/GoalManager.tsx index 0779dda..46b07d5 100644 --- a/src/ui/features/goalmanager/GoalManager.tsx +++ b/src/ui/features/goalmanager/GoalManager.tsx @@ -5,23 +5,37 @@ import { MaterialUiPickersDate } from '@material-ui/pickers/typings/date' import 'date-fns' import React, { useEffect, useState } from 'react' import styled from 'styled-components' +import { BaseEmoji } from 'emoji-mart' + 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 EmojiPicker from '../../components/EmojiPicker' +import GoalIcon from '../../components/GoalIcon' import { Theme } from '../../components/Theme' + type Props = { goal: Goal } + + export function GoalManager(props: Props) { + const dispatch = useAppDispatch() const goal = useAppSelector(selectGoalsMap)[props.goal.id] + const [name, setName] = useState(null) const [targetDate, setTargetDate] = useState(null) const [targetAmount, setTargetAmount] = useState(null) + const [icon, setIcon] = useState(null) + const [emojiPickerIsOpen, setEmojiPickerIsOpen] = useState(false) + + useEffect(() => { setName(props.goal.name) setTargetDate(props.goal.targetDate) @@ -33,152 +47,358 @@ export function GoalManager(props: Props) { props.goal.targetAmount, ]) + useEffect(() => { setName(goal.name) }, [goal.name]) - const updateNameOnChange = (event: React.ChangeEvent) => { + + useEffect(() => { + setIcon(props.goal.icon ?? null) + }, [props.goal.id, props.goal.icon]) + + + const hasIcon = () => icon != null + + + 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, + 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 + ) => { + 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) => { + + + const updateTargetAmountOnChange = ( + event: React.ChangeEvent + ) => { + 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) => { + + + const pickDateOnChange = ( + date: MaterialUiPickersDate + ) => { + if (date != null) { + setTargetDate(date) + 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)) updateGoalApi(props.goal.id, updatedGoal) + } + } + + return ( + - + + + {hasIcon() ? ( + + ) : ( + + Add icon + + )} + + + + event.stopPropagation()} + > + + + + + + + + + + - + + + - + + + + + + - + + + - + + + + + + - + + + - {props.goal.balance} + + + {props.goal.balance} + + + + + - + + + - {new Date(props.goal.created).toLocaleDateString()} + + + {new Date(props.goal.created).toLocaleDateString()} + + + + + + ) + +} + + + +type FieldProps = { + name: string + icon: IconDefinition } -type FieldProps = { name: string; icon: IconDefinition } -type AddIconButtonContainerProps = { shouldShow: boolean } -type GoalIconContainerProps = { shouldShow: boolean } -type EmojiPickerContainerProps = { isOpen: boolean; hasIcon: boolean } const Field = (props: FieldProps) => ( + - - {props.name} + + + + + {props.name} + + + ) + + const GoalManagerContainer = styled.div` - display: flex; - flex-direction: column; - justify-content: flex-start; - align-items: flex-start; - height: 100%; - width: 100%; - position: relative; + +display:flex; +flex-direction:column; +height:100%; +width:100%; +position:relative; + +` + + + +const EmojiPickerContainer = styled.div<{ + isOpen:boolean + hasIcon:boolean +}>` + +display:${props => props.isOpen ? 'flex' : 'none'}; +position:absolute; +top:${props => props.hasIcon ? '10rem' : '2rem'}; +left:0; + +` + + + +const AddIconButton = styled.button` + +margin-bottom:1rem; + ` + + const Group = styled.div` - display: flex; - flex-direction: row; - width: 100%; - margin-top: 1.25rem; - margin-bottom: 1.25rem; + +display:flex; +flex-direction:row; +width:100%; +margin-top:1.25rem; +margin-bottom:1.25rem; + ` + + + const NameInput = styled.input` - display: flex; - background-color: transparent; - outline: none; - border: none; - font-size: 4rem; - font-weight: bold; - color: ${({ theme }: { theme: Theme }) => theme.text}; -` -const FieldName = styled.h1` - font-size: 1.8rem; - margin-left: 1rem; - color: rgba(174, 174, 174, 1); - font-weight: normal; +background-color:transparent; +border:none; +outline:none; +font-size:4rem; +font-weight:bold; + ` + + + const FieldContainer = styled.div` - display: flex; - flex-direction: row; - align-items: center; - width: 20rem; - svg { - color: rgba(174, 174, 174, 1); - } +display:flex; +align-items:center; +width:20rem; + +` + + + +const FieldName = styled.h1` + +font-size:1.8rem; +margin-left:1rem; + ` + + + const StringValue = styled.h1` - font-size: 1.8rem; - font-weight: bold; + +font-size:1.8rem; + ` + + + const StringInput = styled.input` - display: flex; - background-color: transparent; - outline: none; - border: none; - font-size: 1.8rem; - font-weight: bold; - color: ${({ theme }: { theme: Theme }) => theme.text}; + +font-size:1.8rem; + ` + + const Value = styled.div` - margin-left: 2rem; -` + +margin-left:2rem; + +` \ No newline at end of file