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 icon support #31

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
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
140 changes: 89 additions & 51 deletions src/ui/features/goalmanager/GoalManager.tsx
Original file line number Diff line number Diff line change
@@ -1,113 +1,151 @@
import { faCalendarAlt } 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 { faCalendarAlt, faDollarSign, faSmile } from '@fortawesome/free-regular-svg-icons';
import { IconDefinition } from '@fortawesome/fontawesome-common-types';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { MaterialUiPickersDate } from '@material-ui/pickers/typings/date';
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 { Theme } from '../../components/Theme';
import EmojiPicker from '../../components/EmojiPicker'; // Import the EmojiPicker component
import GoalIcon from './GoalIcon'; // Import the GoalIcon component
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 { Theme } from '../../components/Theme'

type Props = { goal: Goal }
export function GoalManager(props: Props) {
const dispatch = useAppDispatch()

const goal = useAppSelector(selectGoalsMap)[props.goal.id]
type Props = { goal: Goal };

const [name, setName] = useState<string | null>(null)
const [targetDate, setTargetDate] = useState<Date | null>(null)
const [targetAmount, setTargetAmount] = useState<number | null>(null)
export function GoalManager(props: Props) {
const dispatch = useAppDispatch();
const goal = useAppSelector(selectGoalsMap)[props.goal.id];
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 [emojiPickerIsOpen, setEmojiPickerIsOpen] = useState(false);

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

useEffect(() => {
setName(goal.name)
}, [goal.name])
const hasIcon = () => icon != null;

const updateNameOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const nextName = event.target.value
setName(nextName)
const nextName = event.target.value;
setName(nextName);
const updatedGoal: Goal = {
...props.goal,
name: nextName,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
}
};
dispatch(updateGoalRedux(updatedGoal));
updateGoalApi(props.goal.id, updatedGoal);
};

const updateTargetAmountOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const nextTargetAmount = parseFloat(event.target.value)
setTargetAmount(nextTargetAmount)
const nextTargetAmount = parseFloat(event.target.value);
setTargetAmount(nextTargetAmount);
const updatedGoal: Goal = {
...props.goal,
name: name ?? props.goal.name,
targetDate: targetDate ?? props.goal.targetDate,
targetAmount: nextTargetAmount,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
}
};
dispatch(updateGoalRedux(updatedGoal));
updateGoalApi(props.goal.id, updatedGoal);
};

const pickDateOnChange = (date: MaterialUiPickersDate) => {
if (date != null) {
setTargetDate(date)
setTargetDate(date);
const updatedGoal: Goal = {
...props.goal,
name: name ?? props.goal.name,
targetDate: date ?? props.goal.targetDate,
targetAmount: targetAmount ?? props.goal.targetAmount,
}
dispatch(updateGoalRedux(updatedGoal))
updateGoalApi(props.goal.id, updatedGoal)
};
dispatch(updateGoalRedux(updatedGoal));
updateGoalApi(props.goal.id, updatedGoal);
}
};

const addIconOnClick = (event: React.MouseEvent) => {
event.stopPropagation();
setEmojiPickerIsOpen(true);
};

const pickEmojiOnClick = (emoji: any, 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));
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,
}
}

updateGoalApi(props.goal.id, updatedGoal)
};

return (
<GoalManagerContainer>
<NameInput value={name ?? ''} onChange={updateNameOnChange} />

<NameInput value={name || ''} onChange={updateNameOnChange} />
<Group>
<Field name="Target Date" icon={faCalendarAlt} />
<Value>
<DatePicker value={targetDate} onChange={pickDateOnChange} />
</Value>
</Group>

<Group>
<Field name="Target Amount" icon={faDollarSign} />
<Value>
<StringInput value={targetAmount ?? ''} onChange={updateTargetAmountOnChange} />
<StringInput value={targetAmount || ''} onChange={updateTargetAmountOnChange} />
</Value>
</Group>

<Group>
<Field name="Balance" icon={faDollarSign} />
<Value>
<StringValue>{props.goal.balance}</StringValue>
</Value>
</Group>

<Group>
<Field name="Date Created" icon={faCalendarAlt} />
<Value>
<StringValue>{new Date(props.goal.created).toLocaleDateString()}</StringValue>
</Value>
</Group>
<AddIconButtonContainer shouldShow={!hasIcon()}>
<TransparentButton onClick={addIconOnClick}>
<FontAwesomeIcon icon={faSmile} size="2x" />
<AddIconButtonText>Add icon</AddIconButtonText>
</TransparentButton>
</AddIconButtonContainer>
<GoalIconContainer shouldShow={hasIcon()}>
<GoalIcon icon={icon} onClick={addIconOnClick} />
</GoalIconContainer>
<EmojiPickerContainer isOpen={emojiPickerIsOpen} hasIcon={hasIcon} onClick={(event) => event.stopPropagation()}>
{emojiPickerIsOpen && <EmojiPicker onClick={pickEmojiOnClick} />}
</EmojiPickerContainer>
</GoalManagerContainer>
)
);
}

type FieldProps = { name: string; icon: IconDefinition }
Expand Down
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 @@ -11,6 +11,10 @@ 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>
</Container>
)
}
Expand Down