Skip to content

Commit

Permalink
fix: delete and navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelomota committed Feb 19, 2024
1 parent 77d3cfd commit 4431094
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
10 changes: 5 additions & 5 deletions frontend/src/context/TargetContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
type TargetContextValue = {
targets: TargetProps[] | undefined;
addTarget: (target: Omit<TargetProps, "id">) => void;
getTargetById: (id: number) => TargetProps | undefined;
incrementEntry: (id: number, entry: Entry) => void;
getTargetById: (id: string) => TargetProps | undefined;
incrementEntry: (id: string, entry: Entry) => void;
removeTarget: (targetId: string) => void;
};

Expand All @@ -32,7 +32,7 @@ interface Entry {
}

export interface TargetProps {
id: number;
id: string;
name: string;
target: number;
currentValue: number;
Expand All @@ -48,13 +48,13 @@ export const TargetContextProvider: React.FC<React.PropsWithChildren> = ({
const [targets, setTargets] = useState<TargetProps[]>([]);

const getTargetById = useCallback(
(id: number) => {
(id: string) => {
return targets?.find((target) => target.id === id);
},
[targets]
);

const incrementEntry = useCallback((id: number, entry: Entry) => {
const incrementEntry = useCallback((id: string, entry: Entry) => {
setTargets((prev) =>
prev?.map((target) => {
if (target.id === id) {
Expand Down
25 changes: 12 additions & 13 deletions frontend/src/screens/home/HomeScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import React from "react";
import {
Flex,
List, Progress,
Space
} from "antd";
import { Flex, List, Progress, Space } from "antd";
import { Link } from "react-router-dom";
import { useTargets } from "../../context/TargetContext";
import DeleteButton from "../../components/buttons/deleteButton/DeleteButton";

export const HomeScreen: React.FC = () => {
const { targets, removeTarget } = useTargets();


return (
<Flex vertical gap={30} className="w-full">
<Space direction="vertical" align="end" className="w-full">
<Link to="/new-target" className="py-2 px-3 rounded bg-blue-600 text-white">
<Link
to="/new-target"
className="py-2 px-3 rounded bg-blue-600 text-white"
>
Nova Meta
</Link>
</Space>

<List
className="w-full"
className="w-full"
size="large"
bordered
dataSource={targets}
renderItem={(item) => (
<Link
to={`/target/${item.id}`}
className="w-full hover:opacity-80 transition-all duration-200">
<List.Item>
{item.name}
<Link
to={`/target/${item.id}`}
className="w-full hover:opacity-80 transition-all duration-200"
>
{item.name}
</Link>
<div style={{ float: "right" }}>
<DeleteButton onDelete={() => removeTarget(item.id)} />
</div>
Expand All @@ -40,7 +40,6 @@ export const HomeScreen: React.FC = () => {
/>
{item.currentValue} / {item.target}
</List.Item>
</Link>
)}
/>
</Flex>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/new-entry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const NewEntry = () => {
const [form] = Form.useForm<EntryProps>();

const handleAddEntry = (values: EntryProps) => {
incrementEntry(Number(id), values);
incrementEntry(id!, values);

navigate(`/target/${id}`);
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/target-detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const TargetDetailScreen = () => {
const { getTargetById } = useTargets();

useEffect(() => {
const target = getTargetById(Number(id));
const target = getTargetById(id!);

setTarget(target);
}, [id, getTargetById]);
Expand Down

0 comments on commit 4431094

Please sign in to comment.