Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup ipfs connector #108

Merged
merged 7 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"preview": "vite preview"
},
"dependencies": {
"@solidjs/testing-library": "^0.8.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.21.3",
Expand All @@ -26,6 +27,7 @@
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"typescript": "^5.2.2",
"vite": "^5.0.12"
"vite": "^5.0.12",
"vitest": "^1.5.0"
}
}
60 changes: 30 additions & 30 deletions examples/react/src/routes/__tests__/todo-list.test.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { render, fireEvent } from '@solidjs/testing-library';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { render, fireEvent } from '@solidjs/testing-library'

import TodoList from '../Todo';
import TodoList from '../Todo'

import { expect, describe, test } from 'vitest';
import { expect, describe, test } from 'vitest'

describe('<TodoList />', () => {
test('it will render an text input and a button', () => {
const { getByPlaceholderText, getByText } = render(() => <TodoList />);
expect(getByPlaceholderText('new todo here')).toBeInTheDocument();
expect(getByText('Add Todo')).toBeInTheDocument();
});
const { getByPlaceholderText, getByText } = render(() => <TodoList />)
expect(getByPlaceholderText('new todo here')).toBeInTheDocument()
expect(getByText('Add Todo')).toBeInTheDocument()
})

test('it will add a new todo', async () => {
const { getByPlaceholderText, getByText } = render(() => <TodoList />);
const input = getByPlaceholderText('new todo here') as HTMLInputElement;
const button = getByText('Add Todo');
input.value = 'test new todo';
fireEvent.click(button as HTMLInputElement);
expect(input.value).toBe('');
expect(getByText(/test new todo/)).toBeInTheDocument();
});
const { getByPlaceholderText, getByText } = render(() => <TodoList />)
const input = getByPlaceholderText('new todo here') as HTMLInputElement
const button = getByText('Add Todo')
input.value = 'test new todo'
fireEvent.click(button as HTMLInputElement)
expect(input.value).toBe('')
expect(getByText(/test new todo/)).toBeInTheDocument()
})

test('it will mark a todo as completed', async () => {
const { getByPlaceholderText, findByRole, getByText } = render(() => (
<TodoList />
));
const input = getByPlaceholderText('new todo here') as HTMLInputElement;
const button = getByText('Add Todo') as HTMLButtonElement;
input.value = 'mark new todo as completed';
fireEvent.click(button);
const completed = (await findByRole('checkbox')) as HTMLInputElement;
expect(completed?.checked).toBe(false);
fireEvent.click(completed);
expect(completed?.checked).toBe(true);
const text = getByText('mark new todo as completed') as HTMLSpanElement;
expect(text).toHaveStyle({ 'text-decoration': 'line-through' });
});
});
const { getByPlaceholderText, findByRole, getByText } = render(() => <TodoList />)
const input = getByPlaceholderText('new todo here') as HTMLInputElement
const button = getByText('Add Todo') as HTMLButtonElement
input.value = 'mark new todo as completed'
fireEvent.click(button)
const completed = (await findByRole('checkbox')) as HTMLInputElement
expect(completed?.checked).toBe(false)
fireEvent.click(completed)
expect(completed?.checked).toBe(true)
const text = getByText('mark new todo as completed') as HTMLSpanElement
expect(text).toHaveStyle({ 'text-decoration': 'line-through' })
})
})
3 changes: 2 additions & 1 deletion examples/todomvc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"build": "echo 'todomvc example needs update'",
"build:tsc": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
Expand Down
20 changes: 12 additions & 8 deletions examples/todomvc/src/components/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ import { useNavigate, useParams, useLoaderData, useRevalidator } from 'react-rou
import Footer from './Footer'
import InputArea from './InputArea'
import TodoItem from './TodoItem'
import { FireproofCtx, FireproofCtxValue } from 'use-fireproof'
import { useFireproof } from 'use-fireproof'
import { TimeTravel } from './TimeTravel'
import { UploadManager } from '../hooks/useUploader'

import { ListLoaderData, TodoDoc } from '../interfaces'
import { makeQueryFunctions } from '../makeQueryFunctions'

const sleep = async (t: number) => new Promise((resolve) => setTimeout(resolve, t))
const sleep = async (t: number) => new Promise(resolve => setTimeout(resolve, t))

export function List(): JSX.Element {
// first data stuff
const { ready, database, addSubscriber } = useContext(FireproofCtx) as FireproofCtxValue
const { addTodo, toggle, destroy, clearCompleted, updateTitle } = makeQueryFunctions({ ready, database })
const { database } = useFireproof()
const { addTodo, toggle, destroy, clearCompleted, updateTitle } = makeQueryFunctions({
ready: true,
database
})
let { list, todos } = useLoaderData() as ListLoaderData
const [editing, setEditing] = useState('')
const revalidator = useRevalidator()
addSubscriber('List', async () => {
database.subscribe(async () => {
revalidator.revalidate()
})

Expand All @@ -37,8 +40,8 @@ export function List(): JSX.Element {
const routeFilter = filter || ''
const filteredTodos = {
all: todos,
active: todos.filter((todo) => !todo.completed),
completed: todos.filter((todo) => todo.completed),
active: todos.filter(todo => !todo.completed),
completed: todos.filter(todo => todo.completed)
}
const shownTodos = filteredTodos[nowShowing]
// now action stuff
Expand All @@ -54,7 +57,8 @@ export function List(): JSX.Element {

<ul className="todo-list">
{shownTodos.map((todo: TodoDoc) => {
const handle = (fn: (arg0: TodoDoc, arg1: string) => any) => (val: string) => fn(todo, val)
const handle = (fn: (arg0: TodoDoc, arg1: string) => any) => (val: string) =>
fn(todo, val)
return (
<TodoItem
key={todo._id}
Expand Down
5 changes: 3 additions & 2 deletions examples/todomvc/src/components/TimeTravel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const shortLink = (l: string) => `${String(l).slice(0, 4)}..${String(l).slice(-4)}`
const clockLog = new Set<string>()
import { Fireproof } from 'use-fireproof'


export const TimeTravel = ({ database }) => {
database.clock && database.clock.length && clockLog.add(database.clock.toString())
Expand All @@ -27,7 +27,8 @@ export const TimeTravel = ({ database }) => {
<li key={entry}>
<button
onClick={async () => {
await Fireproof.zoom(database, [entry])
// await Fireproof.zoom(database, [entry])
console.log('todo zoom', entry)
}}
>
{shortLink(entry)}
Expand Down
45 changes: 26 additions & 19 deletions examples/todomvc/src/makeQueryFunctions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Fireproof } from "use-fireproof"

export function makeQueryFunctions({ ready, database }): {
fetchAllLists: () => Promise<any>
fetchListWithTodos: (_id: any) => Promise<{ list: any; todos: any }>
Expand All @@ -12,32 +10,40 @@ export function makeQueryFunctions({ ready, database }): {
} {
const fetchAllLists = async () => {
const lists =
ready && database.allLists ? await database.allLists.query({ range: ['list', 'listx'] }) : { rows: [] }
ready && database.allLists
? await database.allLists.query({ range: ['list', 'listx'] })
: { rows: [] }
return lists.rows.map(({ value }) => value)
}

const fetchListWithTodos = async (_id) => {
const fetchListWithTodos = async _id => {
if (!ready || !database.todosByList)
return Promise.resolve({ list: { title: '', type: 'list', _id: '' }, todos: [] })

const list = await database.get(_id)
const todos = await database.todosByList.query({
range: [
[_id, '0'],
[_id, '9'],
],
[_id, '9']
]
})
return { list, todos: todos.rows.map((row) => row.value) }
return { list, todos: todos.rows.map(row => row.value) }
}

const addList = async (title) => {
const addList = async title => {
return ready && (await database.put({ title, type: 'list' }))
}

const addTodo = async (listId, title) => {
return (
ready &&
(await database.put({ completed: false, title, listId, type: 'todo', createdAt: new Date().toISOString() }))
(await database.put({
completed: false,
title,
listId,
type: 'todo',
createdAt: new Date().toISOString()
}))
)
}

Expand All @@ -54,17 +60,18 @@ export function makeQueryFunctions({ ready, database }): {
return ready && (await database.put(doc))
}

const clearCompleted = async (listId) => {
const result = ready &&
await database.todosByList.query({
const clearCompleted = async listId => {
const result =
ready &&
(await database.todosByList.query({
range: [
[listId, '1'],
[listId, 'x'],
],
})
const todos = result.rows.map((row) => row.value)
const todosToDelete = todos.filter((todo) => todo.completed)
[listId, 'x']
]
}))

const todos = result.rows.map(row => row.value)
const todosToDelete = todos.filter(todo => todo.completed)
for (const todoToDelete of todosToDelete) {
await database.del(todoToDelete._id)
}
Expand All @@ -77,6 +84,6 @@ export function makeQueryFunctions({ ready, database }): {
toggle,
destroy,
updateTitle,
clearCompleted,
clearCompleted
}
}
11 changes: 6 additions & 5 deletions packages/connect-ipfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"scripts": {
"prepublishOnly": "npm run build",
"build:clean": "rm -rf dist",
"build:watch": "tsc -w",
"build:tsc": "npm run build:clean && tsc && mkdir dist/tsc && mv dist/*.js dist/tsc/ && node ./scripts/types.js",
"build:script": "node ./scripts/build.js",
"build": "npm run build:tsc && npm run build:script && cp dist/browser/index.iife.js ../fireproof/test/www/ipfs.iife.js",
Expand All @@ -53,12 +54,12 @@
"@fireproof/connect": "workspace:^",
"@fireproof/core": "workspace:^",
"@fireproof/encrypted-blockstore": "workspace:^",
"@ucanto/core": "^8.2.0",
"@ucanto/interface": "^8.1.0",
"@web3-storage/clock": "^0.3.0",
"@ucanto/core": "^10.0.1",
"@ucanto/interface": "^10.0.1",
"@web3-storage/clock": "^0.4.1",
"@web3-storage/pail": "^0.5.0",
"@web3-storage/w3up-client": "^8.0.1",
"@web3-storage/w3up-client": "^12.5.2",
"cross-fetch": "^4.0.0",
"multiformats": "^12.0.1"
"multiformats": "^13.1.0"
}
}
6 changes: 3 additions & 3 deletions packages/connect-ipfs/scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ console.log('cjs/node build');
...commonSettings,
outfile: `dist/browser/${filename}.iife.js`,
format: 'iife',
globalName: 'FireproofConnect',
globalName: 'FireproofConnectIPFS',
platform: 'browser',
target: 'es2020',
target: 'esnext',
entryPoints: [entryPoint],
minify: false,
banner: bannerLog`
console.log('browser/es2015 build');
console.log('browser/esnext build');
`,
plugins: [
// alias(
Expand Down
Loading
Loading