Skip to content

Commit ca3c6fc

Browse files
committed
pass in fula client to Photo element and call receiveFile to display content in img tag (#60)
1 parent 5a87d04 commit ca3c6fc

4 files changed

Lines changed: 56 additions & 14 deletions

File tree

examples/react-gallery/src/App.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,8 @@ h1, h3,h4 {
212212
transform: translate(24px, 0);
213213
}
214214
}
215+
216+
.photo {
217+
width:100%;
218+
padding: 10px;
219+
}

examples/react-gallery/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function App() {
9191
return (
9292
<div className='app'>
9393
<FulaProvider fula={fulaClient}>
94-
{connectionStatus ? <Gallery /> : <div className='connect-container'>
94+
{(connectionStatus===true && fulaClient!==undefined)? <Gallery fulaClient={fulaClient} /> : <div className='connect-container'>
9595
<div className='app-header'>
9696
{!connecting ? <h1>Connect to BOX!</h1> : null}
9797
{connecting ? <div className='lds-ellipsis'><div></div><div></div><div></div><div></div></div> : null}

examples/react-gallery/src/components/Gallery.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import React, { useEffect, useState } from 'react';
22
import Photo from './Photo';
33
import type { PHOTO } from './Photo';
4+
import { Fula } from '@functionland/fula'
45

5-
function Gallery() {
6-
const [photos, setPhotos] = useState<PHOTO[]>([]);
7-
useEffect(() => {
8-
setPhotos([{cid:'a'}, {cid:'b'}, {cid:'c'}])
9-
});
106

7+
interface Props {
8+
fulaClient: Fula
9+
}
10+
11+
const Gallery = ({ fulaClient }: Props): JSX.Element => {
12+
13+
const [photos, setPhotos] = useState<PHOTO[]>([{cid:'a'}, {cid:'b'}, {cid:'c'}]);
1114

1215
return (
1316
<>
1417
<h1>Functionland Sample Gallery</h1>
1518
{
1619
photos.map((photo, index) => (
17-
<div key={index} className='photo'>
18-
<Photo photo={photo} />
20+
<div key={index} >
21+
<Photo photo={photo} fulaClient={fulaClient} />
1922
</div>
2023
))
2124
}

examples/react-gallery/src/components/Photo.tsx

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useEffect } from 'react';
2+
import { Fula } from '@functionland/fula'
3+
24

35
export type PHOTO = {
46
cid: string,
57
}
68

79
interface Props {
8-
photo: PHOTO
10+
photo: PHOTO,
11+
fulaClient: Fula
912
}
1013

11-
const Photo = ({ photo, }: Props): JSX.Element => {
12-
return <>
13-
{photo.cid}
14-
</>
14+
const Photo = ({ photo, fulaClient }: Props): JSX.Element => {
15+
16+
const [fileId, setFileId] = useState("QmYJt7141ZG2Kut37W2ZHYcyjYTJ36aX22KYu7einNevPD")
17+
const [content, setContent] = useState("")
18+
const [output, setOutput] = useState("");
19+
20+
21+
useEffect(() => {
22+
(async () => {
23+
receiveFile();
24+
})()
25+
}, []);
26+
27+
28+
const receiveFile = async () => {
29+
if (!fulaClient) {
30+
console.log("fula not connected")
31+
return
32+
}
33+
try {
34+
const data = await fulaClient.receiveFile(fileId);
35+
console.log(data)
36+
let reader = new FileReader();
37+
reader.readAsDataURL(data);
38+
// @ts-ignore
39+
reader.onloadend = (e) => setContent(reader.result)
40+
}catch (e) {
41+
console.log(e)
42+
}
43+
44+
}
45+
46+
return <div className='photo'>
47+
<img width="100%" src={content} />
48+
</div>
1549

1650
};
1751

0 commit comments

Comments
 (0)