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

Implemented frontend support for icons #49

Open
wants to merge 5 commits 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
11 changes: 11 additions & 0 deletions src/api/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from 'axios'
import { user } from '../data/user'
import { Goal, Transaction, User } from './types'

//Export necessary functions
export const API_ROOT = 'https://fencer-commbank.azurewebsites.net'

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

//Support for dynamically updating 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
}
}
2 changes: 2 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//Support for various necessary interfaces
export interface Account {
id: string
number: number
Expand All @@ -18,6 +19,7 @@ export interface Application {
}

export interface Goal {
icon: string | null
id: string
name: string
targetAmount: number
Expand Down
68 changes: 66 additions & 2 deletions src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 { Goal } from '../../../api/types'
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 EmojiPicker from '../../components/EmojiPicker' // Import for emojipicker
import { BaseEmoji } from 'emoji-mart' // Import for BaseEmoji



type Props = { goal: Goal }
export function GoalManager(props: Props) {
Expand All @@ -21,16 +25,21 @@ 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) // state for icon
const [emojiPickerIsOpen, setEmojiPickerIsOpen] = useState(false) // 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 +84,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 +136,24 @@ 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 @@ -181,4 +229,20 @@ 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;
`
9 changes: 7 additions & 2 deletions src/ui/pages/Main/goals/GoalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ 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 { Icon } from '@material-ui/core'

type Props = { id: string }

const icon = styled.h1`
font-size: 5.5rem;
`

export default function GoalCard(props: Props) {
const dispatch = useAppDispatch()

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

Expand Down