diff --git a/src/api/lib.ts b/src/api/lib.ts index 3c593ca..1ffa94e 100644 --- a/src/api/lib.ts +++ b/src/api/lib.ts @@ -51,3 +51,12 @@ export async function updateGoal(goalId: string, updatedGoal: Goal): Promise { + try { + await axios.put(`${API_ROOT}/api/Goal/${goalId}`, { icon }) + return true + } catch (error: any) { + return false + } +} diff --git a/src/api/types.ts b/src/api/types.ts index f75edad..b668377 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 } export interface Tag { diff --git a/src/ui/features/goalmanager/AddIconButton.tsx b/src/ui/features/goalmanager/AddIconButton.tsx index d0c8c2c..cafb0e6 100644 --- a/src/ui/features/goalmanager/AddIconButton.tsx +++ b/src/ui/features/goalmanager/AddIconButton.tsx @@ -6,7 +6,6 @@ import styled from 'styled-components' import { TransparentButton } from '../../components/TransparentButton' type Props = { hasIcon: boolean; onClick: (event: React.MouseEvent) => void } - export default function AddIconButton(props: Props) { if (props.hasIcon) return null diff --git a/src/ui/features/goalmanager/GoalManager.tsx b/src/ui/features/goalmanager/GoalManager.tsx index 0779dda..586a74d 100644 --- a/src/ui/features/goalmanager/GoalManager.tsx +++ b/src/ui/features/goalmanager/GoalManager.tsx @@ -5,12 +5,16 @@ import { MaterialUiPickersDate } from '@material-ui/pickers/typings/date' import 'date-fns' import React, { useEffect, useState } from 'react' import styled from 'styled-components' -import { updateGoal as updateGoalApi } from '../../../api/lib' +import { updateGoal as updateGoalApi, updateGoalIcon as updateGoalIconApi } 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 AddIconButton from './AddIconButton' +import EmojiPicker from '../../components/EmojiPicker' +import GoalIcon from './GoalIcon' +import { BaseEmoji } from 'emoji-mart' type Props = { goal: Goal } export function GoalManager(props: Props) { @@ -21,16 +25,20 @@ export function GoalManager(props: Props) { const [name, setName] = useState(null) const [targetDate, setTargetDate] = useState(null) const [targetAmount, setTargetAmount] = useState(null) + const [isEmojiPickerOpen, setEmojiPickerOpen] = useState(false) + const [icon, setIcon] = useState(null) 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(() => { @@ -56,6 +64,7 @@ export function GoalManager(props: Props) { name: name ?? props.goal.name, targetDate: targetDate ?? props.goal.targetDate, targetAmount: nextTargetAmount, + icon: icon ?? props.goal.icon, } dispatch(updateGoalRedux(updatedGoal)) updateGoalApi(props.goal.id, updatedGoal) @@ -67,14 +76,30 @@ export function GoalManager(props: Props) { const updatedGoal: Goal = { ...props.goal, name: name ?? props.goal.name, - targetDate: date ?? props.goal.targetDate, + targetDate: targetDate ?? props.goal.targetDate, targetAmount: targetAmount ?? props.goal.targetAmount, + icon: icon ?? props.goal.icon, } dispatch(updateGoalRedux(updatedGoal)) updateGoalApi(props.goal.id, updatedGoal) } } + const pickEmojiOnClick = (emoji: BaseEmoji) => { + setIcon(emoji.native) + const updatedGoal: Goal = { + ...props.goal, + name: name ?? props.goal.name, + targetDate: targetDate ?? props.goal.targetDate, + targetAmount: targetAmount ?? props.goal.targetAmount, + icon: emoji.native ?? props.goal.icon, + } + + dispatch(updateGoalRedux(updatedGoal)) + updateGoalIconApi(props.goal.id, emoji.native) + setEmojiPickerOpen(!isEmojiPickerOpen) + } + return ( @@ -106,14 +131,33 @@ export function GoalManager(props: Props) { {new Date(props.goal.created).toLocaleDateString()} + + setEmojiPickerOpen(true)} /> + + + setEmojiPickerOpen(true)} + /> + + ) } type FieldProps = { name: string; icon: IconDefinition } -type AddIconButtonContainerProps = { shouldShow: boolean } -type GoalIconContainerProps = { shouldShow: boolean } -type EmojiPickerContainerProps = { isOpen: boolean; hasIcon: boolean } +type AddIconButtonContainerProps = { shouldShow: boolean; onClick: () => void } +type GoalIconContainerProps = { shouldShow: boolean; icon: string; onClick: () => void } +type EmojiPickerContainerProps = { + isOpen: boolean + hasIcon: boolean + onClick: (emoji: BaseEmoji) => void +} const Field = (props: FieldProps) => ( @@ -122,6 +166,22 @@ const Field = (props: FieldProps) => ( ) +const AddIconButtonContainer = (props: AddIconButtonContainerProps) => ( + + + +) + +const GoalIconContainer = (props: GoalIconContainerProps) => ( + + {props.shouldShow && } + +) + +const EmojiPickerContainer = (props: EmojiPickerContainerProps) => ( + {props.isOpen && } +) + const GoalManagerContainer = styled.div` display: flex; flex-direction: column; diff --git a/src/ui/pages/Main/goals/GoalCard.tsx b/src/ui/pages/Main/goals/GoalCard.tsx index e8f6d0a..b00b2a3 100644 --- a/src/ui/pages/Main/goals/GoalCard.tsx +++ b/src/ui/pages/Main/goals/GoalCard.tsx @@ -5,9 +5,10 @@ import { useAppDispatch, useAppSelector } from '../../../../store/hooks' import { setContent as setContentRedux, setIsOpen as setIsOpenRedux, - setType as setTypeRedux + setType as setTypeRedux, } from '../../../../store/modalSlice' import { Card } from '../../../components/Card' +import GoalIcon from '../../../features/goalmanager/GoalIcon' type Props = { id: string } @@ -29,6 +30,7 @@ export default function GoalCard(props: Props) { ${goal.targetAmount} {asLocaleDateString(goal.targetDate)} + {}} /> ) }