Option 1: Getting started with a Social Feed
Step 1: Create a post
/** Import Orbis SDK */
import { Orbis } from "@orbisclub/orbis-sdk";
/**
* Initialize the Orbis class object:
* - You can make this object available on other components by passing it as
* a prop or by using a context.
*/
let orbis = new Orbis();
export default function App() {
/** The user object returned by the connect function can be stored in state */
const [user, setUser] = useState();
/** Calls the Orbis SDK and handle the results */
async function connect() {
let res = await orbis.connect();
/** Check if connection is successful or not */
if(res.status == 200) {
console.log("Connected to Ceramic with: ", res.did);
setUser(res.did);
} else {
console.log("Error connecting to Ceramic: ", res);
alert("Error connecting to Ceramic.");
}
}
return(
<div>
{user ?
<>
<p>Connected with: {user}</p>
<Share orbis={orbis} />
</>
:
<button onClick={() => connect()}>Connect</button>
}
</div>
);
}
/**
* If the user is connected we show a textarea and submit button
* to allow users to share content on Orbis.
* The 'orbis' object must be passed as a parameter to maintain the connection.
*/
function Share({orbis}) {
const [loading, setLoading] = useState(false);
const [text, setText] = useState();
/** We are calling the Orbis SDK to share a new post from this user */
async function share() {
setLoading(true);
/**
* The createPost() function accept a JSON object that must contain a body
* which is the actual content of the post being shared.
*/
let res = await orbis.createPost({body: text});
/** Check if post was shared with success or not */
if(res.status == 200) {
console.log("Shared post with stream_id: ", res.doc);
} else {
console.log("Error sharing post: ", res);
alert("Error sharing post.");
}
setLoading(false);
}
return(
<div>
<textarea placeholder="Share your post here..." value={text} onChange={(e) => setText(e.target.value)} />
{loading ?
<button>Loading...</button>
:
<button onClick={() => share()}>Share</button>
}
</div>
);
}Step 2: Retrieving posts
Last updated