This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples from developer.tbd.website repo
- Loading branch information
1 parent
bd315da
commit e4dfc42
Showing
152 changed files
with
5,254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FROM node:18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
// These tasks will run in order when initializing your CodeSandbox project. | ||
"setupTasks": [ | ||
{ | ||
"name": "Install Dependencies", | ||
"command": "npm install" | ||
} | ||
], | ||
|
||
// These tasks can be run from CodeSandbox. Running one will open a log in the app. | ||
"tasks": { | ||
"node book-reviews.js": { | ||
"name": "node book-reviews.js", | ||
"command": "node book-reviews.js", | ||
"runAtStart": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
DATA/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Book Reviews Example | ||
|
||
Build a book review app using Web5. | ||
|
||
*Tutorial:* https://developer.tbd.website/docs/web5/build/apps/book-reviews-tutorial | ||
|
||
See it in action 👇 | ||
|
||
[![Edit in CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/TBD54566975/developer.tbd.website/tree/main/examples/tutorials/book-reviews) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
import { Web5 } from "@web5/api"; | ||
|
||
//Node 18 users: the following 3 lines are needed | ||
import { webcrypto } from "node:crypto"; | ||
// @ts-ignore | ||
if (!globalThis.crypto) globalThis.crypto = webcrypto; | ||
|
||
|
||
|
||
const { web5, did: userDid } = await Web5.connect(); | ||
|
||
//Schema we'll use for Book Reviews | ||
const schema = { | ||
context: "https://schema.org/", | ||
type: "Review", | ||
get uri() { | ||
return this.context + this.type; | ||
}, | ||
}; | ||
|
||
//Book Reviews | ||
let reviews = [ | ||
{ | ||
"@context": schema.context, | ||
"@type": schema.type, | ||
itemReviewed: { | ||
"@type": "Book", | ||
name: "The Great Gatsby", | ||
author: { | ||
"@type": "Person", | ||
name: "F. Scott Fitzgerald", | ||
}, | ||
datePublished: "1925", | ||
genre: "Fiction", | ||
identifier: "978-1982149482", | ||
}, | ||
author: { | ||
"@type": "Person", | ||
identifier: userDid, | ||
}, | ||
datePublished: "2023-09-18", | ||
reviewRating: { | ||
"@type": "Rating", | ||
ratingValue: "4.5", | ||
}, | ||
reviewBody: | ||
"A classic novel with timeless themes and memorable characters. Fitzgerald's prose is simply enchanting.", | ||
}, | ||
{ | ||
"@context": schema.context, | ||
"@type": schema.type, | ||
itemReviewed: { | ||
"@type": "Book", | ||
name: "To Kill a Mockingbird", | ||
author: { | ||
"@type": "Person", | ||
name: "Harper Lee", | ||
}, | ||
datePublished: "1960", | ||
genre: "Fiction", | ||
identifier: "978-0446310789", | ||
}, | ||
author: { | ||
"@type": "Person", | ||
identifier: userDid, | ||
}, | ||
datePublished: "2023-09-18", | ||
reviewRating: { | ||
"@type": "Rating", | ||
ratingValue: "5.0", | ||
}, | ||
reviewBody: | ||
"A powerful exploration of morality, justice, and the human condition. Truly a must-read.", | ||
}, | ||
{ | ||
"@context": schema.context, | ||
"@type": schema.type, | ||
itemReviewed: { | ||
"@type": "Book", | ||
name: "1984", | ||
author: { | ||
"@type": "Person", | ||
name: "George Orwell", | ||
}, | ||
datePublished: "1949", | ||
genre: "Dystopian", | ||
identifier: "978-0451524935", | ||
}, | ||
author: { | ||
"@type": "Person", | ||
identifier: userDid, | ||
}, | ||
datePublished: "2023-09-18", | ||
reviewRating: { | ||
"@type": "Rating", | ||
ratingValue: "4.7", | ||
}, | ||
reviewBody: | ||
"A disturbing vision of a totalitarian future. Orwell's work is as relevant today as it was when it was first published.", | ||
}, | ||
{ | ||
"@context": schema.context, | ||
"@type": schema.type, | ||
itemReviewed: { | ||
"@type": "Book", | ||
name: "Brave New World", | ||
author: { | ||
"@type": "Person", | ||
name: "Aldous Huxley", | ||
}, | ||
datePublished: "1932", | ||
genre: "Dystopian", | ||
identifier: "978-0060850524", | ||
}, | ||
author: { | ||
"@type": "Person", | ||
identifier: userDid, | ||
}, | ||
datePublished: "2023-09-18", | ||
reviewRating: { | ||
"@type": "Rating", | ||
ratingValue: "4.8", | ||
}, | ||
reviewBody: | ||
"A deeply disturbing yet essential read. Huxley's vision of a future driven by technology and hedonism serves as a potent warning for society.", | ||
}, | ||
]; | ||
|
||
//Query book review (search for DWN records) | ||
async function getReviews() { | ||
let { records } = await web5.dwn.records.query({ | ||
message: { | ||
filter: { | ||
schema: schema.uri, | ||
}, | ||
}, | ||
dateSort: "createdAscending", | ||
}); | ||
return records; | ||
} | ||
|
||
//checks if review already exists (you may not want duplicate data in the DWN) | ||
async function isReviewPresent(review) { | ||
for (let record of existingReviews) { | ||
let bookData = await record.data.json(); | ||
let isbn = bookData.itemReviewed.identifier; | ||
|
||
if (isbn === review.itemReviewed.identifier) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
//Create book review (write record to DWN) | ||
async function addReviews() { | ||
for (const review of reviews) { | ||
let reviewExists = await isReviewPresent(review); | ||
if (reviewExists) { | ||
console.log(`Review for ${review.itemReviewed.name} already exists`); | ||
} else { | ||
const response = await web5.dwn.records.create({ | ||
data: review, | ||
message: { | ||
schema: schema.uri, | ||
dataFormat: "application/json", | ||
published: true, | ||
}, | ||
}); | ||
|
||
if (response.status.code === 202) { | ||
console.log(`Review for ${review.itemReviewed.name} added successfully`); | ||
} else { | ||
console.log(`${response.status}. Error adding review for ${review.itemReviewed.name}`); | ||
} | ||
} | ||
} | ||
existingReviews = await getReviews(); | ||
} | ||
|
||
//Update book review rating | ||
async function updateReviewRating(review, newRating) { | ||
let bookData = await review.data.json(); | ||
console.log(`old rating for ${bookData.itemReviewed.name}`, bookData.reviewRating.ratingValue); | ||
|
||
//Update the value within the JSON then send the entire JSON to update | ||
bookData.reviewRating.ratingValue = newRating; | ||
let response = await review.update({ | ||
data: bookData, | ||
}); | ||
|
||
if (response.status.code === 202) { | ||
//Obtain the updated record | ||
const { record: updatedReview } = await web5.dwn.records.read({ | ||
message: { | ||
filter: { | ||
recordId: review.id | ||
} | ||
} | ||
}); | ||
|
||
const updatedData = await updatedReview.data.json(); | ||
console.log(`updated rating for ${bookData.itemReviewed.name}`, updatedData.reviewRating.ratingValue); | ||
} | ||
else console.log(`${response.status}. Error updating rating for ${bookData.itemReviewed.name}`); | ||
} | ||
|
||
//Delete all book reviews | ||
async function deleteReviews() { | ||
let records = await getReviews(); | ||
|
||
for (const record of records) { | ||
let title = (await record.data.json()).itemReviewed.name; | ||
let response = await web5.dwn.records.delete({ | ||
message: { | ||
recordId: record.id, | ||
}, | ||
}); | ||
console.log(`deleted ${title}. status: ${response.status.code}`); | ||
} | ||
} | ||
|
||
let existingReviews = await getReviews(); | ||
await addReviews(); | ||
await updateReviewRating(existingReviews[1], "4.2"); | ||
await deleteReviews(); | ||
|
||
process.exit(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "book-reviews", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "book-reviews.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node book-reviews.js" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@web5/api": "0.9.4" | ||
}, | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FROM node:18 | ||
RUN npm i -g pnpm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"$schema": "https://codesandbox.io/schemas/tasks.json", | ||
"setupTasks": [ | ||
{ | ||
"name": "Install Dependencies", | ||
"command": "pnpm install" | ||
} | ||
], | ||
|
||
"tasks": { | ||
"dev": { | ||
"name": "pnpm run dev", | ||
"command": "pnpm run dev", | ||
"runAtStart": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Web5 Chat App - Dinger - Completed | ||
|
||
This is a self-sovereign, modern day pager that allows users to send short text-based messages or "dings" to each other. It is a demo application that demonstrates peer-to-peer communication in the Web5 ecosystem. | ||
|
||
[![Edit in CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/TBD54566975/developer.tbd.website/tree/main/examples/tutorials/dinger-completed) | ||
|
||
Technologies: | ||
- Next.js | ||
- CSS | ||
- React | ||
- Web5.js | ||
|
||
## Setup | ||
|
||
Install dependencies: | ||
|
||
```bash | ||
# npm | ||
npm install | ||
|
||
# pnpm | ||
pnpm install | ||
|
||
# yarn | ||
yarn install | ||
``` | ||
|
||
## Run Development Server | ||
|
||
Start the development server on `http://localhost:8080`: | ||
|
||
```bash | ||
# npm | ||
npm run dev | ||
|
||
# pnpm | ||
pnpm run dev | ||
|
||
# yarn | ||
yarn dev | ||
``` | ||
|
||
## Want to learn how to build this on your own? | ||
Check out the [tutorial](https://developer.tbd.website/docs/web5/build/apps/dinger-tutorial) and [starter code](https://codesandbox.io/p/sandbox/github/TBD54566975/developer.tbd.website/tree/main/examples/tutorials/dinger-starter). |
Oops, something went wrong.