1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import React, { useState } from 'react';
import { marlowe } from '@/api/client';
import { useQuery, useMutation } from '@/hooks';
import { Button } from '@/components/ui';
import { Plus } from 'lucide-react';
import BudgetForm from '@/components/BudgetForm';
import BudgetCard from '@/components/BudgetCard';
export default function Budgets() {
const [showForm, setShowForm] = useState(false);
const [editing, setEditing] = useState(null);
const queryClient = useQueryClient();
const now = new Date();
const currentMonth = `${now.getFullYear()}-${month}`;
const { data: budgets } = useQuery({
queryKey: ['budgets', currentMonth],
queryFn: () => marlowe.entities.Budget.list(),
});
const { data: transactions } = useQuery({
queryKey: ['transactions', currentMonth],
queryFn: () => marlowe.entities.Transaction.list(),
});
const createMutation = useMutation({
mutationFn: (data) => marlowe.entities.Budget.create(data),
onMutate: async (data) => {
await queryClient.cancelQueries({ queryKey });
const prev = queryClient.getQueryData(key);
queryClient.setQueryData(key, (old) => [
return { prev };
},
onError: (err, data, ctx) => {
onSuccess: () => { queryClient.invalidate(
});
const updateMutation = useMutation({
mutationFn: ({ id, data }) => marlowe.e