diff --git a/src/api/types.ts b/src/api/types.ts index f75edad..8cbabc0 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -27,6 +27,7 @@ export interface Goal { accountId: string transactionIds: string[] tagIds: string[] + icon: string | null } export interface Tag { diff --git a/src/ui/features/goalmanager/GoalManager.tsx b/src/ui/features/goalmanager/GoalManager.tsx index 0779dda..38d1e10 100644 --- a/src/ui/features/goalmanager/GoalManager.tsx +++ b/src/ui/features/goalmanager/GoalManager.tsx @@ -1,113 +1,151 @@ -import { faCalendarAlt } from '@fortawesome/free-regular-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 'date-fns' -import React, { useEffect, useState } from 'react' -import styled from 'styled-components' +import { faCalendarAlt, faDollarSign, faSmile } from '@fortawesome/free-regular-svg-icons'; +import { IconDefinition } from '@fortawesome/fontawesome-common-types'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { MaterialUiPickersDate } from '@material-ui/pickers/typings/date'; +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 { useAppDispatch, useAppSelector } from '../../../store/hooks'; +import DatePicker from '../../components/DatePicker'; +import { Theme } from '../../components/Theme'; +import EmojiPicker from '../../components/EmojiPicker'; // Import the EmojiPicker component +import GoalIcon from './GoalIcon'; // Import the GoalIcon component 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' -type Props = { goal: Goal } -export function GoalManager(props: Props) { - const dispatch = useAppDispatch() - - const goal = useAppSelector(selectGoalsMap)[props.goal.id] +type Props = { goal: Goal }; - const [name, setName] = useState(null) - const [targetDate, setTargetDate] = useState(null) - const [targetAmount, setTargetAmount] = useState(null) +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) - setTargetAmount(props.goal.targetAmount) + setName(props.goal.name); + setTargetDate(props.goal.targetDate); + setTargetAmount(props.goal.targetAmount); + setIcon(goal.icon); }, [ props.goal.id, props.goal.name, props.goal.targetDate, props.goal.targetAmount, - ]) + ]); - useEffect(() => { - setName(goal.name) - }, [goal.name]) + const hasIcon = () => icon != null; const updateNameOnChange = (event: React.ChangeEvent) => { - const nextName = event.target.value - setName(nextName) + const nextName = event.target.value; + setName(nextName); const updatedGoal: Goal = { ...props.goal, name: nextName, - } - dispatch(updateGoalRedux(updatedGoal)) - updateGoalApi(props.goal.id, updatedGoal) - } + }; + dispatch(updateGoalRedux(updatedGoal)); + updateGoalApi(props.goal.id, updatedGoal); + }; const updateTargetAmountOnChange = (event: React.ChangeEvent) => { - const nextTargetAmount = parseFloat(event.target.value) - setTargetAmount(nextTargetAmount) + 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) - } + }; + dispatch(updateGoalRedux(updatedGoal)); + updateGoalApi(props.goal.id, updatedGoal); + }; const pickDateOnChange = (date: MaterialUiPickersDate) => { if (date != null) { - setTargetDate(date) + 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) + }; + dispatch(updateGoalRedux(updatedGoal)); + updateGoalApi(props.goal.id, updatedGoal); + } + }; + + const addIconOnClick = (event: React.MouseEvent) => { + event.stopPropagation(); + setEmojiPickerIsOpen(true); + }; + + const pickEmojiOnClick = (emoji: any, event: 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)); + 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, } - } + + updateGoalApi(props.goal.id, updatedGoal) + }; return ( - - + - - + - {props.goal.balance} - {new Date(props.goal.created).toLocaleDateString()} + + + + Add icon + + + + + + event.stopPropagation()}> + {emojiPickerIsOpen && } + - ) + ); } type FieldProps = { name: string; icon: IconDefinition } diff --git a/src/ui/pages/Main/goals/GoalCard.tsx b/src/ui/pages/Main/goals/GoalCard.tsx index e8f6d0a..1196ac4 100644 --- a/src/ui/pages/Main/goals/GoalCard.tsx +++ b/src/ui/pages/Main/goals/GoalCard.tsx @@ -11,6 +11,10 @@ import { Card } from '../../../components/Card' type Props = { id: string } +const Icon = styled.h1` + font-size: 5.5rem; +` + export default function GoalCard(props: Props) { const dispatch = useAppDispatch() @@ -29,6 +33,7 @@ export default function GoalCard(props: Props) { ${goal.targetAmount} {asLocaleDateString(goal.targetDate)} + {goal.icon} ) }