-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and optimize collection update mechanism
- Enhanced PATCH endpoint in router to ensure comprehensive data integrity checks and preservation of key card attributes like quantity, priceHistory, and latestPrice. - Implemented useEffect in context to automatically update local and server state when cards are added, removed, or their quantity changes in a collection. - Improved cron job logic to trigger only when specific conditions are met, enhancing efficiency. - Fixed issue with disappearing header title in the PortfolioContent component during chart re-renders.
- Loading branch information
Showing
19 changed files
with
1,248 additions
and
689 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useEffect } from 'react'; | ||
import axios from 'axios'; | ||
import { useCollectionStore } from './CollectionContext'; | ||
|
||
const CardUpdateEffect = ({ allCollections, user, updateCollectionData }) => { | ||
const { updateCollection } = useCollectionStore(); | ||
useEffect(() => { | ||
const updateCardTotalPrice = async (card, collection) => { | ||
try { | ||
const updatedTotalPrice = card.price * card.quantity; | ||
const response = await axios.patch( | ||
`${process.env.REACT_APP_SERVER}/api/cards/ygopro/${card.id}`, | ||
{ | ||
id: card.id, | ||
user: user, | ||
totalPrice: updatedTotalPrice, | ||
// Include other fields as necessary | ||
} | ||
); | ||
|
||
if (response.data && response.data.data) { | ||
// Update card in the local state or context | ||
updateCollection(response.data.data, 'update', collection, user.id); | ||
} | ||
} catch (error) { | ||
console.error( | ||
`Error updating total price for card ID ${card.id}:`, | ||
error | ||
); | ||
} | ||
}; | ||
|
||
const processCollections = () => { | ||
allCollections.forEach((collection) => { | ||
collection.cards.forEach((card) => { | ||
const originalTotalPrice = card.price * card.quantity; | ||
if (card.totalPrice !== originalTotalPrice) { | ||
updateCardTotalPrice(card, collection); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
processCollections(); | ||
}, [allCollections, user, updateCollectionData]); | ||
|
||
return null; // Since this component doesn't render anything | ||
}; | ||
|
||
export default CardUpdateEffect; |
Oops, something went wrong.