Skip to content

Commit a5a490e

Browse files
committed
Add Comment function -> Dapp Finished
1 parent 1870faf commit a5a490e

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

src/App.svelte

+26-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
//convert ipfsHash to bytes32 to fit the SC
3434
const hashToSend = hashToBytes32(ipfsHash);
3535
etherGram.methods.upload(hashToSend).send({from: '0x519Ff9BEFa4127688900C31922350103aA5495e6'});
36+
getPostsFromSC(e);
3637
}
3738
3839
const clap = (e) => {
@@ -41,6 +42,12 @@
4142
etherGram.methods.clap(imageHash).send({from: '0x519Ff9BEFa4127688900C31922350103aA5495e6'})
4243
}
4344
45+
const comment = (e) => {
46+
const commentHash = web3.utils.asciiToHex(e.detail.elements[0].value);
47+
const imageHash = hashToBytes32(e.detail.name);
48+
etherGram.methods.comment(imageHash, commentHash).send({from: '0x519Ff9BEFa4127688900C31922350103aA5495e6'});
49+
}
50+
4451
const getPostsFromSC = async (e) => {
4552
e.preventDefault()
4653
const result = await etherGram.methods.getAllPosts().call({from: '0x519Ff9BEFa4127688900C31922350103aA5495e6'});
@@ -50,10 +57,14 @@
5057
const hex = "1220" + result[i].slice(2);
5158
const hashBytes = Buffer.from(hex, 'hex');
5259
const image = bs58.encode(hashBytes);
60+
5361
const clapCounts = await getClapCountFromSC(result[i]);
62+
const comments = await getComment(result[i]);
63+
5464
postsFromSC.push({
5565
image: image,
56-
clapCounts: clapCounts
66+
clapCounts: clapCounts,
67+
comment: comments
5768
});
5869
}
5970
console.log(postsFromSC);
@@ -64,6 +75,15 @@
6475
return etherGram.methods.getClapCount(imageHash).call({from: '0x519Ff9BEFa4127688900C31922350103aA5495e6'})
6576
}
6677
78+
const getComment = async (imageHash) => {
79+
const result = await etherGram.methods.getComments(imageHash).call({from: '0x519Ff9BEFa4127688900C31922350103aA5495e6'})
80+
var processedComments=[];
81+
result.forEach(commentHash => {
82+
processedComments.push(web3.utils.toAscii(commentHash));
83+
});
84+
return processedComments;
85+
}
86+
6787
function startApp() {
6888
etherGram = new web3.eth.Contract(ethergramABI,scAddress);
6989
}
@@ -89,18 +109,22 @@
89109
<style>
90110
h1 {
91111
color: rgb(59, 70, 168);
112+
font-size: 80px;
113+
text-align: center;
114+
background-color: azure
92115
}
93116
</style>
94117

95118
<h1>Picture board DApp by Jason</h1>
96119

120+
97121
{#if pages.postImage}
98122
<Upload
99123
on:upload={uploadToSC}
100124
on:viewImage={getPostsFromSC}
101125
/>
102126

103127
{:else if pages.viewImage}
104-
<ViewImage on:clap={clap} {postsFromSC} />
128+
<ViewImage on:clap={clap} on:comment={comment} {postsFromSC} />
105129

106130
{/if}

src/Upload.svelte

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@
4040
4141
</script>
4242

43+
<h2>Upload your Images to Picture Board</h2>
44+
4345
<img src={filePreview} alt="filepreview" />
4446

4547
<form on:submit={onSubmit}>
4648
<input type="file" on:change={fileChange} />
4749
<input type="submit">
4850
</form>
4951

50-
<input type="submit" on:click={() => dispatch("viewImage")} value="Uploaded Images" />
52+
<input type="submit" on:click={() => dispatch("viewImage")} value="View Picture Board" />

src/ViewImage.svelte

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,37 @@
22
import {createEventDispatcher} from "svelte";
33
export let postsFromSC;
44
5-
let comment = "";
65
const dispatch = createEventDispatcher();
76
87
const clap = (e) => {
98
e.preventDefault();
109
dispatch("clap",e.target.alt);
1110
}
11+
12+
const commentOnPost = (e) => {
13+
e.preventDefault();
14+
dispatch("comment", e.target);
15+
}
1216
</script>
1317

14-
<p>Images</p>
18+
<h2>Picture Board</h2>
19+
20+
<p>Double-click on image to give it a like. <br/>
21+
And, feel free to drop your comments and submit them.</p>
22+
1523

1624
<ul>
1725
{#each postsFromSC as post}
1826
<li>
19-
<form on:dblclick={clap}>
27+
<form on:dblclick={clap} on:submit={commentOnPost} name={post.image}>
2028
<img src="https://ipfs.infura.io/ipfs/{post.image}" alt={post.image}/>
29+
<input type="text" placeholder="Comment your thoughts about this image" maxlength="32"/> <input type="submit">
2130
</form>
2231
<p>{post.clapCounts} Like(s) </p>
32+
<p><b>Comments ({post.comment.length})</b></p>
33+
{#each post.comment as comment}
34+
<p>{comment} <br /> </p>
35+
{/each}
2336
</li>
2437
{/each}
2538
</ul>

0 commit comments

Comments
 (0)