Skip to content

Commit

Permalink
fix: todo list template (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
skypesky authored Sep 23, 2024
1 parent 6c27d46 commit 5ba2196
Show file tree
Hide file tree
Showing 32 changed files with 102 additions and 75 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.5 (2024-9-23)

- fix: todo list template

## 0.9.4 (2024-8-29)

- fix(vite-plugin-blocklet): fix blockletPrefix without trailingSlash cause vite error
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "create-blocklet",
"private": true,
"version": "0.9.4",
"version": "0.9.5",
"description": "",
"keywords": [],
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-blocklet",
"version": "0.9.4",
"version": "0.9.5",
"exports": "./index.js",
"type": "module",
"repository": "[email protected]:blocklet/create-blocklet.git",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/templates/react-dapp-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"ts-node": "^10.9.2",
"typescript": "^5.5.2",
"vite": "^5.3.5",
"vite-plugin-blocklet": "^0.9.4",
"vite-plugin-blocklet": "^0.9.5",
"vite-plugin-svgr": "^4.2.0",
"zx": "^8.1.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/templates/react-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"rimraf": "^5.0.7",
"simple-git-hooks": "^2.11.1",
"vite": "^5.3.5",
"vite-plugin-blocklet": "^0.9.4",
"vite-plugin-blocklet": "^0.9.5",
"vite-plugin-svgr": "^4.2.0",
"zx": "^8.1.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/templates/react-gun-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"rimraf": "^5.0.7",
"simple-git-hooks": "^2.11.1",
"vite": "^5.3.5",
"vite-plugin-blocklet": "^0.9.4",
"vite-plugin-blocklet": "^0.9.5",
"vite-plugin-svgr": "^4.2.0",
"zx": "^8.1.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/templates/react-static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"rimraf": "^5.0.7",
"simple-git-hooks": "^2.11.1",
"vite": "^5.3.5",
"vite-plugin-blocklet": "^0.9.4",
"vite-plugin-blocklet": "^0.9.5",
"vite-plugin-svgr": "^4.2.0",
"zx": "^8.1.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/templates/solidjs-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"rimraf": "^5.0.7",
"simple-git-hooks": "^2.11.1",
"vite": "^5.3.5",
"vite-plugin-blocklet": "^0.9.4",
"vite-plugin-blocklet": "^0.9.5",
"vite-plugin-solid": "^2.10.2",
"zx": "^8.1.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/templates/solidjs-static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"simple-git-hooks": "^2.11.1",
"solid-js": "^1.8.18",
"vite": "^5.3.5",
"vite-plugin-blocklet": "^0.9.4",
"vite-plugin-blocklet": "^0.9.5",
"vite-plugin-solid": "^2.10.2",
"zx": "^8.1.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/templates/svelte-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"simple-git-hooks": "^2.11.1",
"svelte": "^4.2.18",
"vite": "^5.3.5",
"vite-plugin-blocklet": "^0.9.4",
"vite-plugin-blocklet": "^0.9.5",
"zx": "^8.1.4"
},
"lint-staged": {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/templates/svelte-static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"simple-git-hooks": "^2.11.1",
"svelte": "^4.2.18",
"vite": "^5.3.5",
"vite-plugin-blocklet": "^0.9.4",
"vite-plugin-blocklet": "^0.9.5",
"zx": "^8.1.4"
},
"lint-staged": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import 'express-async-errors';

import path from 'path';

import fallback from '@blocklet/sdk/lib/middlewares/fallback';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import dotenv from 'dotenv-flow';
import express, { ErrorRequestHandler } from 'express';
import fallback from '@blocklet/sdk/lib/middlewares/fallback';

import logger from './libs/logger';
import routes from './routes';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import path from 'path';

import AuthStorage from '@arcblock/did-auth-storage-nedb';
import { AuthService } from '@blocklet/sdk';
import getWallet from '@blocklet/sdk/lib/wallet';
import WalletAuthenticator from '@blocklet/sdk/lib/wallet-authenticator';
import WalletHandler from '@blocklet/sdk/lib/wallet-handler';

import { AuthService } from '@blocklet/sdk';
import env from './env';

export const wallet = getWallet();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import middleware from '@blocklet/sdk/lib/middlewares';
import { Router } from 'express';

import todoListRouter from './todo-list';

const router = Router();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Request, Response } from 'express';
import { GetObjectCommand, SpaceClient } from '@did-space/client';
import { streamToString } from '@did-space/core';
import type { Request, Response } from 'express';

import { authService, wallet } from '../../libs/auth';

export default async function $get(req: Request, res: Response) {
Expand All @@ -14,18 +15,20 @@ export default async function $get(req: Request, res: Response) {
// @ts-ignore
endpoint: user.didSpace.endpoint,
});
try {
const { data } = await spaceClient.send(
new GetObjectCommand({
key: 'todo-list.json',
}),
);
return res.json({ todoList: JSON.parse(await streamToString(data)) });
} catch (error) {
if (error.message.includes('404')) {
return res.json({ todoList: [] });
}
console.error(error);
return res.status(400).send(error.message);

const output = await spaceClient.send(
new GetObjectCommand({
key: 'todo-list.json',
}),
);

if (output.statusCode === 200) {
return res.json({ todoList: JSON.parse(await streamToString(output.data)) });
}

if (output.statusCode === 404) {
return res.json({ todoList: [] });
}

return res.status(output.statusCode).send(output.statusMessage);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { PutObjectCommand, SpaceClient } from '@did-space/client';
import type { Request, Response } from 'express';
import isArray from 'lodash/isArray';

import { authService, wallet } from '../../libs/auth';

export default async function $put(req: Request, res: Response) {
if (!isArray(req.body.todoList)) {
throw new Error('TodoList must be an array');
return res.status(400).send('TodoList must be an array');
}

const { user } = await authService.getUser(req.user?.did as string);
const { user } = await authService.getUser(req.user!.did as string);
if (!user?.didSpace?.endpoint) {
return res.status(404).send('DID Spaces endpoint does not exist. Log in again to complete the authorization');
}
Expand All @@ -18,12 +19,17 @@ export default async function $put(req: Request, res: Response) {
// @ts-ignore
endpoint: user.didSpace.endpoint,
});
await spaceClient.send(

const output = await spaceClient.send(
new PutObjectCommand({
key: 'todo-list.json',
data: JSON.stringify(req.body.todoList),
}),
);

if (output.statusCode !== 200) {
return res.status(output.statusCode).send(output.statusMessage);
}

return res.send();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import middleware from '@blocklet/sdk/lib/middlewares';
import { Router } from 'express';

import $get from './$get';
import $put from './$put';

Expand Down
54 changes: 27 additions & 27 deletions packages/create-app/templates/todo-list-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,64 +40,64 @@
"dependencies": {
"@arcblock/did-auth": "^1.18.135",
"@arcblock/did-auth-storage-nedb": "^1.7.1",
"@blocklet/sdk": "^1.16.30",
"@did-space/client": "^0.5.11",
"@did-space/core": "^0.5.11",
"@blocklet/sdk": "^1.16.31",
"@did-space/client": "^0.5.44",
"@did-space/core": "^0.5.44",
"@ocap/client": "^1.18.135",
"@ocap/mcrypto": "^1.18.135",
"@ocap/wallet": "^1.18.135",
"axios": "^1.7.2",
"axios": "^1.7.7",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv-flow": "^4.1.0",
"express": "^4.19.2",
"express": "^4.21.0",
"express-async-errors": "^3.1.1",
"lodash": "^4.17.21",
"nanoid": "^5.0.7"
},
"devDependencies": {
"@arcblock/did-connect": "^2.10.26",
"@arcblock/did-connect": "^2.10.33",
"@arcblock/eslint-config": "^0.3.2",
"@arcblock/eslint-config-ts": "^0.3.2",
"@arcblock/ux": "^2.10.26",
"@blocklet/js-sdk": "^1.16.30",
"@blocklet/ui-react": "^2.10.26",
"@emotion/react": "^11.13.0",
"@arcblock/ux": "^2.10.33",
"@blocklet/js-sdk": "^1.16.31",
"@blocklet/ui-react": "^2.10.33",
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"@mui/icons-material": "^5.15.21",
"@mui/material": "^5.15.21",
"@mui/icons-material": "^5.16.7",
"@mui/material": "^5.16.7",
"@types/cookie-parser": "^1.4.7",
"@types/cors": "^2.8.17",
"@types/dotenv-flow": "^3.3.3",
"@types/express": "^4.17.21",
"@types/lodash": "^4.17.7",
"@types/node": "^20.14.13",
"@types/react": "^18.3.3",
"@types/node": "^20.16.5",
"@types/react": "^18.3.8",
"@types/react-dom": "^18.3.0",
"@types/react-toastify": "^4.1.0",
"@vitejs/plugin-react": "^4.3.1",
"bumpp": "^9.4.1",
"bumpp": "^9.5.2",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"eslint": "^8.57.1",
"eslint-plugin-prettier": "^5.2.1",
"import-sort-style-module": "^6.0.0",
"lint-staged": "^15.2.7",
"nodemon": "^3.1.4",
"lint-staged": "^15.2.10",
"nodemon": "^3.1.7",
"prettier": "^3.3.3",
"prettier-plugin-import-sort": "^0.0.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^5.2.1",
"react-router-dom": "^6.24.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-router-dom": "^6.26.2",
"react-toastify": "^10.0.5",
"rimraf": "^5.0.7",
"rimraf": "^5.0.10",
"simple-git-hooks": "^2.11.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.2",
"vite": "^5.3.5",
"vite-plugin-blocklet": "^0.9.4",
"typescript": "^5.6.2",
"vite": "^5.4.7",
"vite-plugin-blocklet": "^0.9.5",
"vite-plugin-svgr": "^4.2.0",
"zx": "^8.1.4"
"zx": "^8.1.8"
},
"importSort": {
".js, .jsx, .mjs": {
Expand Down
5 changes: 3 additions & 2 deletions packages/create-app/templates/todo-list-example/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Route, BrowserRouter as Router, Routes } from 'react-router-dom';
import { LocaleProvider } from '@arcblock/ux/lib/Locale/context';
import TodoList from './pages/todo-list';
import { Route, BrowserRouter as Router, Routes } from 'react-router-dom';

import Layout from './components/layout';
import { SessionProvider } from './contexts/session';
import TodoList from './pages/todo-list';

function App() {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useContext } from 'react';
import { createAuthServiceSessionContext } from '@arcblock/did-connect/lib/Session';
import { useContext } from 'react';

const { SessionProvider, SessionContext, SessionConsumer, withSession } = createAuthServiceSessionContext();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Alert, Button, Grid } from '@mui/material';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';

import { useSessionContext } from '../contexts/session';

type RequiredLoginProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { useState, useEffect } from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { nanoid } from 'nanoid';
import isEmpty from 'lodash/isEmpty';
import type { AxiosResponse } from 'axios';

import './todo-list.css';

import type { AxiosResponse } from 'axios';
import isEmpty from 'lodash/isEmpty';
import { nanoid } from 'nanoid';
import { useEffect, useState } from 'react';
import { ToastContainer, toast } from 'react-toastify';

import { useSessionContext } from '../contexts/session';
import RequiredLogin from './required-login';
import axios from '../libs/api';
import RequiredLogin from './required-login';

type Todo = {
id: string;
Expand All @@ -30,18 +32,26 @@ function TodoList() {
const fetchTodoList = async () => {
try {
setLoading(true);

if (!session.user) {
setTodoList([]);
return;
}

const response: AxiosResponse<{ todoList: [] }, any> = await axios.get('/api/todo-list');
setTodoList(response.data.todoList);
} catch (error) {
console.error(error);
setTodoList([]);
toast.error(error.message);
} finally {
setLoading(false);
}
};

useEffect(() => {
fetchTodoList();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

// Step 4: write data to DID Spaces
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/templates/vue-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"rimraf": "^5.0.7",
"simple-git-hooks": "^2.11.1",
"vite": "^5.3.5",
"vite-plugin-blocklet": "^0.9.4",
"vite-plugin-blocklet": "^0.9.5",
"vue": "^3.4.31",
"zx": "^8.1.4"
},
Expand Down
Loading

0 comments on commit 5ba2196

Please sign in to comment.