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:
@@ -10,16 +10,7 @@ type Props = {
|
||||
gift?: Gift;
|
||||
yearId: string;
|
||||
personId?: string;
|
||||
onSave: (data: {
|
||||
title: string;
|
||||
description: string;
|
||||
link: string;
|
||||
cost: number;
|
||||
imageUrl: string;
|
||||
status: Gift['status'];
|
||||
year: string;
|
||||
person: string;
|
||||
}) => Promise<void>;
|
||||
onSave: (data: Omit<DB.Gift, 'id' | 'created' | 'updated'>) => Promise<void>;
|
||||
};
|
||||
|
||||
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 [status, setStatus] = useState<Gift['status']>(gift?.status || 'planned');
|
||||
const [selectedPersonId, setSelectedPersonId] = useState(personId || '');
|
||||
const [ceneoId, setCeneoId] = useState(gift?.ceneo_id || '');
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -41,6 +33,7 @@ export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId,
|
||||
setImageUrl(gift.imageUrl || '');
|
||||
setStatus(gift.status);
|
||||
setSelectedPersonId(gift.person);
|
||||
setCeneoId(gift.ceneo_id || '');
|
||||
} else {
|
||||
setTitle('');
|
||||
setDescription('');
|
||||
@@ -49,6 +42,7 @@ export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId,
|
||||
setImageUrl('');
|
||||
setStatus('planned');
|
||||
setSelectedPersonId(personId || '');
|
||||
setCeneoId('');
|
||||
}
|
||||
}, [gift, personId, isOpen]);
|
||||
|
||||
@@ -63,6 +57,7 @@ export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId,
|
||||
cost,
|
||||
imageUrl,
|
||||
status,
|
||||
ceneo_id: ceneoId,
|
||||
year: yearId,
|
||||
person: selectedPersonId,
|
||||
});
|
||||
@@ -166,6 +161,23 @@ export const GiftModal: FC<Props> = ({ isOpen, onClose, gift, yearId, personId,
|
||||
disabled={isLoading}
|
||||
/>
|
||||
</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>
|
||||
<label htmlFor='imageUrl' className='block text-sm font-medium text-gray-700 mb-2'>
|
||||
URL obrazu
|
||||
|
||||
@@ -8,7 +8,7 @@ type Props = {
|
||||
onClose: () => void;
|
||||
year: string;
|
||||
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 }) => {
|
||||
@@ -30,7 +30,7 @@ export const PersonModal: FC<Props> = ({ isOpen, onClose, year, person, onSave }
|
||||
e.preventDefault();
|
||||
setIsLoading(true);
|
||||
try {
|
||||
await onSave({ name, notes, year });
|
||||
await onSave({ name, notes, years: [year] });
|
||||
onClose();
|
||||
} catch (error) {
|
||||
console.error('Error saving person:', error);
|
||||
|
||||
@@ -14,13 +14,13 @@ export const DB = {
|
||||
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}"`);
|
||||
},
|
||||
createPerson: async (data: { name: string; notes: string; year: string }): Promise<Person> => {
|
||||
return await pb.collection('gifts_person').create({ ...data, years: data.year });
|
||||
createPerson: async (data: Pick<DB.Person, 'name' | 'notes' | 'years'>): Promise<DB.Person> => {
|
||||
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);
|
||||
},
|
||||
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',
|
||||
});
|
||||
},
|
||||
createGift: async (data: {
|
||||
title: string;
|
||||
description: string;
|
||||
link: string;
|
||||
cost: number;
|
||||
imageUrl: string;
|
||||
status: Gift['status'];
|
||||
year: string;
|
||||
person: string;
|
||||
}): Promise<Gift> => {
|
||||
createGift: async (data: Omit<DB.Gift, 'id' | 'created' | 'updated'>): Promise<DB.Gift> => {
|
||||
const gift = await pb.collection('gifts_items').create(data);
|
||||
console.log(gift);
|
||||
|
||||
pb.collection('gifts_person').update(data.person, {
|
||||
'gifts+': gift.id,
|
||||
@@ -63,19 +53,7 @@ export const DB = {
|
||||
|
||||
return gift as unknown as Gift;
|
||||
},
|
||||
updateGift: async (
|
||||
id: string,
|
||||
data: {
|
||||
title: string;
|
||||
description: string;
|
||||
link: string;
|
||||
cost: number;
|
||||
imageUrl: string;
|
||||
status: Gift['status'];
|
||||
year: string;
|
||||
person: string;
|
||||
},
|
||||
): Promise<Gift> => {
|
||||
updateGift: async (id: string, data: Omit<DB.Gift, 'id' | 'created' | 'updated'>): Promise<DB.Gift> => {
|
||||
return await pb.collection('gifts_items').update(id, data);
|
||||
},
|
||||
deleteGift: async (id: string): Promise<void> => {
|
||||
|
||||
2
src/lib/db/types.d.ts
vendored
2
src/lib/db/types.d.ts
vendored
@@ -15,6 +15,7 @@ namespace DB {
|
||||
notes: string;
|
||||
created: Date;
|
||||
updated: Date;
|
||||
years: string[];
|
||||
gifts: string[];
|
||||
};
|
||||
|
||||
@@ -28,6 +29,7 @@ namespace DB {
|
||||
status: 'planned' | 'decided' | 'bought' | 'ready' | 'wrapped';
|
||||
created: Date;
|
||||
updated: Date;
|
||||
ceneo_id: string;
|
||||
|
||||
year: string;
|
||||
person: string;
|
||||
|
||||
31
src/types/global.d.ts
vendored
31
src/types/global.d.ts
vendored
@@ -1,11 +1,4 @@
|
||||
type Year = {
|
||||
id: string;
|
||||
year: number;
|
||||
notes: string;
|
||||
budgetLimit: number;
|
||||
created: Date;
|
||||
updated: Date;
|
||||
gifts: string[];
|
||||
type Year = DB.Year & {
|
||||
expand: {
|
||||
gifts: Gift[] & {
|
||||
expand: {
|
||||
@@ -19,13 +12,7 @@ type Year = {
|
||||
};
|
||||
};
|
||||
|
||||
type Person = {
|
||||
id: string;
|
||||
name: string;
|
||||
notes: string;
|
||||
created: Date;
|
||||
updated: Date;
|
||||
gifts: string[];
|
||||
type Person = DB.Person & {
|
||||
expand: {
|
||||
gifts?: Gift[];
|
||||
year: {
|
||||
@@ -35,19 +22,7 @@ type Person = {
|
||||
};
|
||||
};
|
||||
|
||||
type 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;
|
||||
type Gift = DB.Gift & {
|
||||
expand: {
|
||||
year: {
|
||||
id: string;
|
||||
|
||||
Reference in New Issue
Block a user