From 5b1ac7286efb67903390e35e046529e006f46d6b Mon Sep 17 00:00:00 2001 From: Swornim Shakya Date: Sun, 19 May 2024 09:59:52 +0545 Subject: [PATCH] support icons --- src/api/types.ts | 1 + src/ui/features/goalmanager/GoalManager.tsx | 77 ++++++++++++++++++--- src/ui/pages/Main/goals/GoalCard.tsx | 7 +- 3 files changed, 76 insertions(+), 9 deletions(-) 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..ea56a5b 100644 --- a/src/ui/features/goalmanager/GoalManager.tsx +++ b/src/ui/features/goalmanager/GoalManager.tsx @@ -1,9 +1,9 @@ import { faCalendarAlt } from '@fortawesome/free-regular-svg-icons' -import { faDollarSign, IconDefinition } from '@fortawesome/free-solid-svg-icons' +import { faDollarSign, faSmile, 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, { MouseEvent, useEffect, useState } from 'react' import styled from 'styled-components' import { updateGoal as updateGoalApi } from '../../../api/lib' import { Goal } from '../../../api/types' @@ -11,6 +11,10 @@ import { selectGoalsMap, updateGoal as updateGoalRedux } from '../../../store/go import { useAppDispatch, useAppSelector } from '../../../store/hooks' import DatePicker from '../../components/DatePicker' import { Theme } from '../../components/Theme' +import { BaseEmoji } from 'emoji-mart' +import EmojiPicker from '../../components/EmojiPicker' +import GoalIcon from './GoalIcon' +import { TransparentButton } from '../../components/TransparentButton' type Props = { goal: Goal } export function GoalManager(props: Props) { @@ -21,17 +25,18 @@ export function GoalManager(props: Props) { const [name, setName] = useState(null) const [targetDate, setTargetDate] = useState(null) const [targetAmount, setTargetAmount] = useState(null) + const [emojiPickerIsOpen, setEmojiPickerIsOpen] = useState(false) + const [icon, setIcon] = useState(null) + + useEffect(() => { + setIcon(props.goal.icon) + }, [props.goal.id, props.goal.icon]) 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, - ]) + }, [props.goal.id, props.goal.name, props.goal.targetDate, props.goal.targetAmount]) useEffect(() => { setName(goal.name) @@ -75,6 +80,30 @@ export function GoalManager(props: Props) { } } + const hasIcon = () => icon != null + + const addIconOnClick = (event: React.MouseEvent) => { + event.stopPropagation() + setEmojiPickerIsOpen(true) + } + + const pickEmojiOnClick = (emoji: BaseEmoji, 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)) + } + return ( @@ -106,6 +135,23 @@ export function GoalManager(props: Props) { {new Date(props.goal.created).toLocaleDateString()} + + event.stopPropagation()} + > + + + + + + + + +

Add icon

+
+
) } @@ -122,6 +168,21 @@ const Field = (props: FieldProps) => ( ) +const GoalIconContainer = styled.div` + display: ${(props) => (props.shouldShow ? 'flex' : 'none')}; +` + +const AddIconButtonContainer = styled.div` + display: ${(props) => (props.shouldShow ? 'flex' : 'none')}; +` + +const EmojiPickerContainer = styled.div` + display: ${(props) => (props.isOpen ? 'flex' : 'none')}; + position: absolute; + top: ${(props) => (props.hasIcon ? '10rem' : '2rem')}; + left: 0; +` + 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..b0cb572 100644 --- a/src/ui/pages/Main/goals/GoalCard.tsx +++ b/src/ui/pages/Main/goals/GoalCard.tsx @@ -5,12 +5,16 @@ 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' 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} ) }