Implement Add and Icon components for Goal Manager#21
Open
MayuriYatagiri wants to merge 1 commit into
Open
Conversation
Added components for adding and displaying icons in the Goal Manager.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GoalManager.tsx
Added components for adding and displaying icons in the Goal Manager.
import { faCalendarAlt, faSmile } 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 { 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 { Theme } from '../../components/Theme'
// Sub-component required by the solution specification
type GoalIconProps = { icon: string | null; onClick: (event: React.MouseEvent) => void }
const Icon = styled.h1
font-size: 6rem; cursor: pointer;function GoalIcon(props: GoalIconProps) {
return (
{props.icon}
)
}
type Props = { goal: Goal }
export function GoalManager(props: Props) {
const dispatch = useAppDispatch()
const goal = useAppSelector(selectGoalsMap)[props.goal.id]
const [title, setTitle] = useState<string | null>(null)
const [icon, setIcon] = useState<string | null>(null)
const [targetDate, setTargetDate] = useState<Date | null>(null)
const [targetAmount, setTargetAmount] = useState<number | null>(null)
const [emojiPickerIsOpen, setEmojiPickerIsOpen] = useState(false)
useEffect(() => {
setTitle(props.goal.title)
setIcon(props.goal.icon)
setTargetDate(props.goal.targetDate)
setTargetAmount(props.goal.targetAmount)
}, [
props.goal.id,
props.goal.title,
props.goal.icon,
props.goal.targetDate,
props.goal.targetAmount,
])
useEffect(() => {
if (goal) {
setTitle(goal.title)
setIcon(goal.icon)
}
}, [goal?.title, goal?.icon, goal])
const hasIcon = () => icon != null
const addIconOnClick = (event: React.MouseEvent) => {
event.stopPropagation()
setEmojiPickerIsOpen(true)
}
const pickEmojiOnClick = (emoji: any, event: React.MouseEvent) => {
event.stopPropagation()
setIcon(emoji.native)
setEmojiPickerIsOpen(false)
}
const updateTitleOnChange = (event: React.ChangeEvent) => {
const nextTitle = event.target.value
setTitle(nextTitle)
const updatedGoal: Goal = {
...props.goal,
title: nextTitle,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
}
const updateTargetAmountOnChange = (event: React.ChangeEvent) => {
const nextTargetAmount = parseFloat(event.target.value)
setTargetAmount(nextTargetAmount)
const updatedGoal: Goal = {
...props.goal,
title: title ?? props.goal.title,
targetDate: targetDate ?? props.goal.targetDate,
targetAmount: nextTargetAmount,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
}
const pickDateOnChange = (date: MaterialUiPickersDate) => {
if (date != null) {
setTargetDate(date)
const updatedGoal: Goal = {
...props.goal,
title: title ?? props.goal.title,
targetDate: date ?? props.goal.targetDate,
targetAmount: targetAmount ?? props.goal.targetAmount,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
}
}
return (
{/* Requirement: Only visible when there is no icon */}
Add icon
)
}
type FieldProps = { name: string; icon: IconDefinition }
type EmojiPickerContainerProps = { isOpen: boolean; hasIcon: boolean }
type AddIconButtonContainerProps = { hasIcon: boolean }
type GoalIconContainerProps = { shouldShow: boolean }
const Field = (props: FieldProps) => (
{props.name}
)
// --- Styled Components ---
const TransparentButton = styled.button
background: transparent; border: none; outline: none; cursor: pointer; display: flex; align-items: center; padding: 0; color: inherit;const AddIconButtonContainer = styled.div
display: ${(props) => (props.hasIcon ? 'none' : 'flex')}; align-items: center;const AddIconButtonText = styled.span
margin-left: 0.5rem; font-size: 1.4rem;const GoalIconContainer = styled.div
display: ${(props) => (props.shouldShow ? 'flex' : 'none')};const GoalManagerContainer = styled.div
display: flex; flex-direction: column; justify-content: flex-start; align-items: flex-start; height: 100%; width: 100%; position: relative;const HeaderGroup = styled.div
display: flex; flex-direction: row; align-items: center; margin-bottom: 2rem; gap: 1.5rem; width: 100%;const EmojiPickerContainer = styled.div
display: ${(props) => (props.isOpen ? 'flex' : 'none')}; position: absolute; top: 7rem; left: 0; z-index: 999;const Group = styled.div
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: 3.5rem;
font-weight: bold;
width: 100%;
color: ${({ theme }: { theme: Theme }) => theme.text};
&::placeholder {
color: rgba(128, 128, 128, 0.5);
}
`
const FieldName = styled.h1
font-size: 1.8rem; margin-left: 1rem; color: rgba(174, 174, 174, 1); font-weight: normal;const FieldContainer = styled.div`
display: flex;
flex-direction: row;
align-items: center;
width: 20rem;
svg {
color: rgba(174, 174, 174, 1);
}
`
const StringValue = styled.h1
font-size: 1.8rem; font-weight: bold;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};const Value = styled.div
margin-left: 2rem;