Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added frontend support for icons #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
13 changes: 12 additions & 1 deletion src/api/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import axios from 'axios'
import { user } from '../data/user'
import { Goal, Transaction, User } from './types'

export const API_ROOT = 'https://fencer-commbank.azurewebsites.net'
// Update the API_ROOT to point to the local API URL
export const API_ROOT = 'http://localhost:5203'

export async function getUser(): Promise<User | null> {
try {
Expand Down Expand Up @@ -51,3 +52,13 @@ export async function updateGoal(goalId: string, updatedGoal: Goal): Promise<boo
return false
}
}

// Add PUT request to update Goal icon
export async function updateGoalIcon(goalId: string, icon: string): Promise<boolean> {
try {
await axios.put(`${API_ROOT}/api/Goal/${goalId}`, { icon })
return true
} catch (error: any) {
return false
}
}
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 // icon
}

export interface Tag {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/features/goalmanager/GoalIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export default function GoalIcon(props: Props) {
}

const Icon = styled.h1`
font-size: 6rem;
font-size: 5.5rem;
cursor: pointer;
`
64 changes: 63 additions & 1 deletion src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ 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' // Added import for updateGoalIcon
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' // Added import for EmojiPicker
import { BaseEmoji } from 'emoji-mart' // Added import for BaseEmoji

type Props = { goal: Goal }
export function GoalManager(props: Props) {
Expand All @@ -21,16 +23,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) // Added state for icon
const [emojiPickerIsOpen, setEmojiPickerIsOpen] = useState(false) // Added state for emoji picker

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

useEffect(() => {
Expand Down Expand Up @@ -75,6 +81,27 @@ export function GoalManager(props: Props) {
}
}

const pickEmojiOnClick = (emoji: BaseEmoji, event: React.MouseEvent) => {
event.stopPropagation() // Stop event propagation

setIcon(emoji.native) // Set icon locally
setEmojiPickerIsOpen(false) // Close emoji picker

const updatedGoal: Goal = {
...props.goal,
icon: emoji.native ?? props.goal.icon, // Update icon in goal
name: name ?? props.goal.name,
targetDate: targetDate ?? props.goal.targetDate,
targetAmount: targetAmount ?? props.goal.targetAmount,
}

dispatch(updateGoalRedux(updatedGoal)) // Update Redux store

updateGoalIconApi(props.goal.id, emoji.native) // Update icon in database
}

const hasIcon = () => icon != null

return (
<GoalManagerContainer>
<NameInput value={name ?? ''} onChange={updateNameOnChange} />
Expand Down Expand Up @@ -106,6 +133,23 @@ export function GoalManager(props: Props) {
<StringValue>{new Date(props.goal.created).toLocaleDateString()}</StringValue>
</Value>
</Group>

<Group>
<Field name="Icon" icon={faCalendarAlt} />
<Value>
<IconDisplay onClick={() => setEmojiPickerIsOpen(!emojiPickerIsOpen)}>
{icon ? <span>{icon}</span> : <FontAwesomeIcon icon={faCalendarAlt} size="2x" />}
</IconDisplay>
</Value>
</Group>

<EmojiPickerContainer
isOpen={emojiPickerIsOpen}
hasIcon={hasIcon()}
onClick={(event) => event.stopPropagation()}
>
<EmojiPicker onClick={pickEmojiOnClick} />
</EmojiPickerContainer>
</GoalManagerContainer>
)
}
Expand Down Expand Up @@ -182,3 +226,21 @@ const StringInput = styled.input`
const Value = styled.div`
margin-left: 2rem;
`

const IconDisplay = styled.div`
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;

span {
font-size: 2rem;
}
`

const EmojiPickerContainer = styled.div<EmojiPickerContainerProps>`
display: ${(props) => (props.isOpen ? 'flex' : 'none')};
position: absolute;
top: ${(props) => (props.hasIcon ? '10rem' : '2rem')};
left: 0;
`
7 changes: 6 additions & 1 deletion src/ui/pages/Main/goals/GoalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -29,6 +33,7 @@ export default function GoalCard(props: Props) {
<Container key={goal.id} onClick={onClick}>
<TargetAmount>${goal.targetAmount}</TargetAmount>
<TargetDate>{asLocaleDateString(goal.targetDate)}</TargetDate>
<Icon>{goal.icon}</Icon> {/* Added icon display */}
</Container>
)
}
Expand Down