7-2. UploadPhoto Component

UploadPhotocomponent's roleComponent code
Interact with contract
Update data to store:
updateFeedfunction
1) UploadPhoto component's role
UploadPhoto component's roleUploadPhoto component handles the photo upload request to the Klaytn blockchain. The process is as follows:
Invoke
uploadPhotomethod of the smart contract by sending a transaction. Inside theuploadPhotocontract method, a new ERC-721 token is minted.After sending a transaction, show the progress along the transaction life cycle using the
Toastcomponent.When the transaction gets into a block, update the new
PhotoDatain the local redux store.
Limiting content size
The maximum size of a single transaction is 32KB. So we restrict the input data (photo and descriptions) not to exceed 30KB to send it over safely.
The string data size is restricted to
2KBPhoto is compressed to be less than
28KBusingimageCompression()function.
2) Component code
3) Interact with contract
Let's make a function to write photo data on Klaytn. Send transaction to contract: uploadPhoto
Unlike read-only function calls, writing data incurs a transaction fee. The transaction fee is determined by the amount of used gas. gas is a measuring unit representing how much calculation is needed to process the transaction.
For these reasons, sending a transaction needs two property from and gas.
Convert photo file as a bytes string to load on transaction
(In Klaystagram contract, we defined photo fotmat as bytes in
PhotoDatastruct)Read photo data as an ArrayBuffer using
FileReaderConvert ArrayBuffer to hex string
Add Prefix
0xto satisfy bytes format
Invoke the contract method:
uploadPhotofrom: An account that sends this transaction and pays the transaction fee.gas: The maximum amount of gas that thefromaccount is willing to pay for this transaction.
After sending the transaction, show progress along the transaction lifecycle using
Toastcomponent.If the transaction successfully gets into a block, call
updateFeedfunction to add the new photo into the feed page.
cf) Transaction life cycle
After sending transaction, you can get transaction life cycle (transactionHash, receipt, error).
transactionHashevent is fired once your signed transaction instance is properly constructed. You can get the transaction hash before sending the transaction over the network.receiptevent is fired when you get a transaction receipt. It means your transaction is included in a block. You can check the block number byreceipt.blockNumber.errorevent is fired when something goes wrong.
4. Update photo in the feed page: updateFeed
updateFeedAfter successfully sending the transaction to the contract, FeedPage needs to be updated.
In order to update the photo feed, we need to get the new photo data we've just uploaded. Let's call getPhoto() with tokenId. tokenId can be retrieved from the transaction receipt. Then add the new photo data in the local redux store.
Last updated