Refactor GiftModal and PersonModal components to utilize updated data types, enhancing type safety. Introduce 'ceneo_id' field in GiftModal for price tracking integration and adjust onSave methods to align with new data structures in the database. Update DB methods to reflect changes in data handling for gifts and persons.

This commit is contained in:
Norbert Maciaszek
2025-11-16 19:18:29 +01:00
parent 99dfe5a302
commit c19cccc444
5 changed files with 35 additions and 68 deletions

View File

@@ -10,16 +10,7 @@ type Props = {
gift?: Gift; gift?: Gift;
yearId: string; yearId: string;
personId?: string; personId?: string;
onSave: (data: { onSave: (data: Omit<DB.Gift, 'id' | 'created' | 'updated'>) => Promise<void>;
title: string;
description: string;
link: string;
cost: number;
imageUrl: string;
status: Gift['status'];
year: string;
person: string;
}) => Promise<void>;
}; };
export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId, onSave }) => { export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId, onSave }) => {
@@ -30,6 +21,7 @@ export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId,
const [imageUrl, setImageUrl] = useState(gift?.imageUrl || ''); const [imageUrl, setImageUrl] = useState(gift?.imageUrl || '');
const [status, setStatus] = useState<Gift['status']>(gift?.status || 'planned'); const [status, setStatus] = useState<Gift['status']>(gift?.status || 'planned');
const [selectedPersonId, setSelectedPersonId] = useState(personId || ''); const [selectedPersonId, setSelectedPersonId] = useState(personId || '');
const [ceneoId, setCeneoId] = useState(gift?.ceneo_id || '');
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
useEffect(() => { useEffect(() => {
@@ -41,6 +33,7 @@ export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId,
setImageUrl(gift.imageUrl || ''); setImageUrl(gift.imageUrl || '');
setStatus(gift.status); setStatus(gift.status);
setSelectedPersonId(gift.person); setSelectedPersonId(gift.person);
setCeneoId(gift.ceneo_id || '');
} else { } else {
setTitle(''); setTitle('');
setDescription(''); setDescription('');
@@ -49,6 +42,7 @@ export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId,
setImageUrl(''); setImageUrl('');
setStatus('planned'); setStatus('planned');
setSelectedPersonId(personId || ''); setSelectedPersonId(personId || '');
setCeneoId('');
} }
}, [gift, personId, isOpen]); }, [gift, personId, isOpen]);
@@ -63,6 +57,7 @@ export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId,
cost, cost,
imageUrl, imageUrl,
status, status,
ceneo_id: ceneoId,
year: yearId, year: yearId,
person: selectedPersonId, person: selectedPersonId,
}); });
@@ -166,6 +161,23 @@ export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId,
disabled={isLoading} disabled={isLoading}
/> />
</div> </div>
<div>
<label htmlFor='ceneoId' className='block text-sm font-medium text-gray-700 mb-2'>
ID Ceneo
</label>
<input
id='ceneoId'
type='text'
value={ceneoId}
onChange={(e) => setCeneoId(e.target.value)}
className='w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-transparent outline-none transition-all'
disabled={isLoading}
/>
<p className='text-xs text-gray-500 mt-2'>
Jeśli produkt nie został jeszcze kupiony i zostanie podany jego ID na ceneo, bot będzie obserwował ceny i w
razie zmian będzie informował o tym na discord.
</p>
</div>
<div> <div>
<label htmlFor='imageUrl' className='block text-sm font-medium text-gray-700 mb-2'> <label htmlFor='imageUrl' className='block text-sm font-medium text-gray-700 mb-2'>
URL obrazu URL obrazu

View File

@@ -8,7 +8,7 @@ type Props = {
onClose: () => void; onClose: () => void;
year: string; year: string;
person?: Person; person?: Person;
onSave: (data: { name: string; notes: string; year: string }) => Promise<void>; onSave: (data: Pick<Person, 'name' | 'notes' | 'years'>) => Promise<void>;
}; };
export const PersonModal: FC<Props> = ({ isOpen, onClose, year, person, onSave }) => { export const PersonModal: FC<Props> = ({ isOpen, onClose, year, person, onSave }) => {
@@ -30,7 +30,7 @@ export const PersonModal: FC<Props> = ({ isOpen, onClose, year, person, onSave }
e.preventDefault(); e.preventDefault();
setIsLoading(true); setIsLoading(true);
try { try {
await onSave({ name, notes, year }); await onSave({ name, notes, years: [year] });
onClose(); onClose();
} catch (error) { } catch (error) {
console.error('Error saving person:', error); console.error('Error saving person:', error);

View File

@@ -14,13 +14,13 @@ export const DB = {
return await pb.collection('gifts_year').getFullList(); return await pb.collection('gifts_year').getFullList();
}, },
getPerson: async (name: string): Promise<Person> => { getPerson: async (name: string): Promise<DB.Person> => {
return await pb.collection('gifts_person').getFirstListItem(`name = "${name}"`); return await pb.collection('gifts_person').getFirstListItem(`name = "${name}"`);
}, },
createPerson: async (data: { name: string; notes: string; year: string }): Promise<Person> => { createPerson: async (data: Pick<DB.Person, 'name' | 'notes' | 'years'>): Promise<DB.Person> => {
return await pb.collection('gifts_person').create({ ...data, years: data.year }); return await pb.collection('gifts_person').create({ ...data });
}, },
updatePerson: async (id: string, data: { name: string; notes: string }): Promise<Person> => { updatePerson: async (id: string, data: Pick<DB.Person, 'name' | 'notes'>): Promise<Person> => {
return await pb.collection('gifts_person').update(id, data); return await pb.collection('gifts_person').update(id, data);
}, },
deletePerson: async (id: string): Promise<void> => { deletePerson: async (id: string): Promise<void> => {
@@ -40,18 +40,8 @@ export const DB = {
fields: '*,expand.year.year,expand.person.name,expand.year.id,expand.person.id', fields: '*,expand.year.year,expand.person.name,expand.year.id,expand.person.id',
}); });
}, },
createGift: async (data: { createGift: async (data: Omit<DB.Gift, 'id' | 'created' | 'updated'>): Promise<DB.Gift> => {
title: string;
description: string;
link: string;
cost: number;
imageUrl: string;
status: Gift['status'];
year: string;
person: string;
}): Promise<Gift> => {
const gift = await pb.collection('gifts_items').create(data); const gift = await pb.collection('gifts_items').create(data);
console.log(gift);
pb.collection('gifts_person').update(data.person, { pb.collection('gifts_person').update(data.person, {
'gifts+': gift.id, 'gifts+': gift.id,
@@ -63,19 +53,7 @@ export const DB = {
return gift as unknown as Gift; return gift as unknown as Gift;
}, },
updateGift: async ( updateGift: async (id: string, data: Omit<DB.Gift, 'id' | 'created' | 'updated'>): Promise<DB.Gift> => {
id: string,
data: {
title: string;
description: string;
link: string;
cost: number;
imageUrl: string;
status: Gift['status'];
year: string;
person: string;
},
): Promise<Gift> => {
return await pb.collection('gifts_items').update(id, data); return await pb.collection('gifts_items').update(id, data);
}, },
deleteGift: async (id: string): Promise<void> => { deleteGift: async (id: string): Promise<void> => {

View File

@@ -15,6 +15,7 @@ namespace DB {
notes: string; notes: string;
created: Date; created: Date;
updated: Date; updated: Date;
years: string[];
gifts: string[]; gifts: string[];
}; };
@@ -28,6 +29,7 @@ namespace DB {
status: 'planned' | 'decided' | 'bought' | 'ready' | 'wrapped'; status: 'planned' | 'decided' | 'bought' | 'ready' | 'wrapped';
created: Date; created: Date;
updated: Date; updated: Date;
ceneo_id: string;
year: string; year: string;
person: string; person: string;

31
src/types/global.d.ts vendored
View File

@@ -1,11 +1,4 @@
type Year = { type Year = DB.Year & {
id: string;
year: number;
notes: string;
budgetLimit: number;
created: Date;
updated: Date;
gifts: string[];
expand: { expand: {
gifts: Gift[] & { gifts: Gift[] & {
expand: { expand: {
@@ -19,13 +12,7 @@ type Year = {
}; };
}; };
type Person = { type Person = DB.Person & {
id: string;
name: string;
notes: string;
created: Date;
updated: Date;
gifts: string[];
expand: { expand: {
gifts?: Gift[]; gifts?: Gift[];
year: { year: {
@@ -35,19 +22,7 @@ type Person = {
}; };
}; };
type Gift = { type Gift = DB.Gift & {
id: string;
title: string;
description: string;
link: string;
cost: number;
imageUrl: string;
status: 'planned' | 'decided' | 'bought' | 'ready' | 'wrapped';
created: Date;
updated: Date;
year: string;
person: string;
expand: { expand: {
year: { year: {
id: string; id: string;