Following up on the question of 2d array of different types: 2d array of different types?
How would one go about initializing a new array of structs (Vote) when creating a new struct (Project)
pragma solidity ^0.4.25;
contract Votinggame{
struct Vote {
address voter;
string comment;
}
struct Project{
int id;
string name;
int votes;
Vote[] vote;
}
Project[] public projects;
int idcounter=-1;
function addNewProject(string name) public {
Project memory newProject = Project({
id:idcounter+1,
name:name,
votes:0
// How to initialise new vote array of struct Vote inside Project struct ??
// vote: new Vote[](0) -> this is not working "UnimplementedFeatureError: Copying of type struct HotOrNotCrypto.Vote memory[] memory to storage not yet supported."
});
idcounter=newProject.id;
projects.push(newProject);
}
function vote(uint index , string comment) public {
Project storage project = projects[index];
project.votes++;
Vote memory v;
v.voter = msg.sender;
v.comment = comment;
project.voters.push(v);
}
}
function getProjects() public view returns (uint){
return (projects.length);
}
}
Also to extend on this how would one go about displaying vote Array of Vote struct in React. At the moment I'm displaying projects Array of a projects struct using .fill().map()
static async getInitialProps(){
const getProjects = await Contract.methods.getProjects().call();
const projectsList = await Promise.all(
Array(parseInt(getProjects))
.fill()
.map((element, index)=> {
return Contract.methods.projects(index).call()
}));
const sorted_projects = _.orderBy(projectsList, ['votes'], ['desc']);
return { sorted_projects};
}
and then render new table row for each struct element like so:
renderRow(){
return (
this.props.sorted_projects.map((sorted_projects,key)=>{
return <RequestRow
id= {key+1}
key ={key}
sorted_projects ={sorted_projects}
/>;
}));
I guess this will be sort of similar with the difference that I would have to provide which project index I want votes array to display