4. Klaystagram 스마트 컨트랙트 작성에서 PhotoData 구조체를 작성하고 이 구조체를 _photoList 맵핑 내에 위치시켰습니다. Feed 컴포넌트의 역할은 다음과 같습니다. 1. Read PhotoData via calling Klaystagram contract method (redux/actions/photos.js) 2. Show PhotoData(feed) with its owner information (components/Feed.js)
2) Read data from contract: getPhoto method
컨트랙트 메서드를 호출합니다: getTotalPhotoCount()
사진이 없는 경우 빈 배열과 함께 setFeed 액션을 호출합니다.
컨트랙트 메서드를 호출합니다: getPhoto(id)
사진이 있으면 각 사진 데이터를 프로미스로 가져와 feed 배열에 넣습니다. 모든 프로미스를 배열에 넣으면 feed 배열을 반환합니다.
리덕스 액션을 호출합니다: setFeed(feed)
feed 배열을 가져와 리덕스 스토어에 저장합니다.
// src/redux/actions/photos.js
const setFeed = (feed) => ({
type: SET_FEED,
payload: { feed },
})
export const getFeed = () => (dispatch) => {
// 1. 컨트랙트 메서드(READ) 호출: `getTotalPhotoCount()`
//사진이 없는 경우 빈 배열과 함께 'setFeed' 액션을 호출합니다.
KlaystagramContract.methods.getTotalPhotoCount().call()
.then((totalPhotoCount) => {
if (!totalPhotoCount) return []
const feed = []
for (let i = totalPhotoCount; i > 0; i--) {
// 2. 컨트랙트 메서드(READ) 호출: `getPhoto(id)`
// 사진이 있으면 모두 호출합니다.
const photo = KlaystagramContract.methods.getPhoto(i).call()
feed.push(photo)
}
return Promise.all(feed)
})
.then((feed) => {
// 3. 액션 호출: `setFeed(feed)`
// 사진 데이터(피드)를 리덕스 스토어에 저장합니다.
dispatch(setFeed(feedParser(feed))
})
}
3) Save data to store: setFeed action
After we successfully fetch photo data (feed) from the Klaystagram contract, we call setFeed(feed) action. 이 액션은 사진 데이터를 페이로드로 가져와 리덕스 스토어에 저장합니다.
At the first time, you can only see the text "No photo :D" because there is no photo data in contract yet.
Let's make a UploadPhoto component to send photo data to contract!