Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios'
import { user } from '../data/user'
import { Goal, Transaction, User } from './types'

export const API_ROOT = 'https://fencer-commbank.azurewebsites.net'
export const API_ROOT = 'http://localhost:5203'

export async function getUser(): Promise<User | null> {
try {
Expand Down
1 change: 1 addition & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface Goal {
accountId: string
transactionIds: string[]
tagIds: string[]
icon?: string | null
}

export interface Tag {
Expand Down
35 changes: 33 additions & 2 deletions src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ 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 { BaseEmoji } from 'emoji-mart'
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'
import AddIconButton from './AddIconButton'
import GoalIcon from './GoalIcon'

type Props = { goal: Goal }
export function GoalManager(props: Props) {
Expand All @@ -21,16 +25,20 @@ export function GoalManager(props: Props) {
const [name, setName] = useState<string | null>(null)
const [targetDate, setTargetDate] = useState<Date | null>(null)
const [targetAmount, setTargetAmount] = useState<number | null>(null)
const [icon, setIcon] = useState<string | null>(null)
const [isEmojiPickerOpen, setIsEmojiPickerOpen] = useState(false)

useEffect(() => {
setName(props.goal.name)
setTargetDate(props.goal.targetDate)
setTargetAmount(props.goal.targetAmount)
setIcon(props.goal.icon ?? null)
}, [
props.goal.id,
props.goal.name,
props.goal.targetDate,
props.goal.targetAmount,
props.goal.icon,
])

useEffect(() => {
Expand Down Expand Up @@ -61,6 +69,19 @@ export function GoalManager(props: Props) {
updateGoalApi(props.goal.id, updatedGoal)
}

const pickEmojiOnClick = (emoji: BaseEmoji, event: React.MouseEvent) => {
const nextIcon = emoji.native
setIcon(nextIcon)
setIsEmojiPickerOpen(false)
const updatedGoal: Goal = { ...props.goal, name: name ?? props.goal.name, icon: nextIcon }
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
}

const toggleEmojiPicker = (event: React.MouseEvent) => {
setIsEmojiPickerOpen(!isEmojiPickerOpen)
}

const pickDateOnChange = (date: MaterialUiPickersDate) => {
if (date != null) {
setTargetDate(date)
Expand All @@ -77,6 +98,11 @@ export function GoalManager(props: Props) {

return (
<GoalManagerContainer>
<GoalIcon icon={icon} onClick={toggleEmojiPicker} />
<AddIconButton hasIcon={!!icon} onClick={toggleEmojiPicker} />
<EmojiPickerContainer isOpen={isEmojiPickerOpen} hasIcon={!!icon}>
<EmojiPicker onClick={pickEmojiOnClick} />
</EmojiPickerContainer>
<NameInput value={name ?? ''} onChange={updateNameOnChange} />

<Group>
Expand Down Expand Up @@ -111,8 +137,6 @@ export function GoalManager(props: Props) {
}

type FieldProps = { name: string; icon: IconDefinition }
type AddIconButtonContainerProps = { shouldShow: boolean }
type GoalIconContainerProps = { shouldShow: boolean }
type EmojiPickerContainerProps = { isOpen: boolean; hasIcon: boolean }

const Field = (props: FieldProps) => (
Expand Down Expand Up @@ -182,3 +206,10 @@ const StringInput = styled.input`
const Value = styled.div`
margin-left: 2rem;
`

const EmojiPickerContainer = styled.div<EmojiPickerContainerProps>`
display: ${({ isOpen }) => (isOpen ? 'flex' : 'none')};
position: absolute;
top: ${({ hasIcon }) => (hasIcon ? '8rem' : '3rem')};
z-index: 100;
`
5 changes: 5 additions & 0 deletions src/ui/pages/Main/goals/GoalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function GoalCard(props: Props) {

return (
<Container key={goal.id} onClick={onClick}>
{goal.icon && <Icon>{goal.icon}</Icon>}
<TargetAmount>${goal.targetAmount}</TargetAmount>
<TargetDate>{asLocaleDateString(goal.targetDate)}</TargetDate>
</Container>
Expand Down Expand Up @@ -54,3 +55,7 @@ const TargetDate = styled.h4`
color: rgba(174, 174, 174, 1);
font-size: 1rem;
`

const Icon = styled.h1`
font-size: 3rem;
`