Fix data persistence issue
Addresses a problem where budget and expense data was not being saved correctly.
This commit is contained in:
@@ -1,25 +1,16 @@
|
||||
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import { PlusIcon } from 'lucide-react';
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from './ui/dialog';
|
||||
import { toast } from '@/components/ui/use-toast';
|
||||
import { useBudget } from '@/contexts/BudgetContext';
|
||||
import { supabase } from '@/lib/supabase';
|
||||
import { isSyncEnabled } from '@/utils/syncUtils';
|
||||
import ExpenseForm, { ExpenseFormValues } from './expenses/ExpenseForm';
|
||||
|
||||
const AddTransactionButton = () => {
|
||||
const [showExpenseDialog, setShowExpenseDialog] = useState(false);
|
||||
const [userId, setUserId] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
// 현재 로그인한 사용자 가져오기
|
||||
const getUser = async () => {
|
||||
const { data: { user } } = await supabase.auth.getUser();
|
||||
setUserId(user?.id || null);
|
||||
};
|
||||
|
||||
getUser();
|
||||
}, []);
|
||||
const { addTransaction } = useBudget();
|
||||
|
||||
// Format number with commas
|
||||
const formatWithCommas = (value: string): string => {
|
||||
@@ -45,19 +36,16 @@ const AddTransactionButton = () => {
|
||||
type: 'expense'
|
||||
};
|
||||
|
||||
// 로컬 스토리지에서 기존 지출 내역을 가져옵니다
|
||||
const existingTransactionsJSON = localStorage.getItem('transactions');
|
||||
let existingTransactions = existingTransactionsJSON ? JSON.parse(existingTransactionsJSON) : [];
|
||||
|
||||
// 새 지출을 추가하고 다시 저장합니다
|
||||
existingTransactions = [newExpense, ...existingTransactions];
|
||||
localStorage.setItem('transactions', JSON.stringify(existingTransactions));
|
||||
// BudgetContext를 통해 지출 추가
|
||||
addTransaction(newExpense);
|
||||
|
||||
// 동기화가 활성화되어 있고 사용자가 로그인되어 있다면 Supabase에도 저장
|
||||
if (isSyncEnabled() && userId) {
|
||||
try {
|
||||
try {
|
||||
const { data: { user } } = await supabase.auth.getUser();
|
||||
|
||||
if (isSyncEnabled() && user) {
|
||||
const { error } = await supabase.from('transactions').insert({
|
||||
user_id: userId,
|
||||
user_id: user.id,
|
||||
title: data.title,
|
||||
amount: parseInt(numericAmount),
|
||||
date: formattedDate,
|
||||
@@ -67,10 +55,10 @@ const AddTransactionButton = () => {
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
} catch (error) {
|
||||
console.error('Supabase에 지출 추가 실패:', error);
|
||||
// 실패해도 로컬에는 저장되어 있으므로 사용자에게 알리지 않음
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Supabase에 지출 추가 실패:', error);
|
||||
// 실패해도 로컬에는 저장되어 있으므로 사용자에게 알리지 않음
|
||||
}
|
||||
|
||||
// 다이얼로그를 닫습니다
|
||||
|
||||
Reference in New Issue
Block a user