Skip to content

Commit

Permalink
WIP update example app
Browse files Browse the repository at this point in the history
  • Loading branch information
pvditto committed Sep 20, 2024
1 parent 62c6d85 commit d987ae4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
60 changes: 45 additions & 15 deletions examples/dql-create-react-app-typescript-example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import './App.css'

import { useMutations, useQuery } from '@dittolive/react-ditto'
import { useExecuteQuery, useQuery } from '@dittolive/react-ditto'
import React, { useState } from 'react'

type Task = {
_id: string
body: string
isCompleted: boolean
isDeleted: boolean
}

const styles: Record<string, React.CSSProperties> = {
app: {
maxWidth: '600px',
Expand All @@ -22,17 +29,42 @@ type Props = {
path: string
}

const App: React.FC<Props> = ({ path }) => {
function isError(error: unknown): error is Error {
return error instanceof Error
}

const App: React.FC<Props> = ({ path: persistenceDirectory }) => {
const [newBodyText, setNewBodyText] = useState<string>('')
const {
items: tasks,
error,
storeObserver,
isLoading,
} = useQuery('select * from tasks')
const { upsert, removeByID, updateByID } = useMutations({
collection: 'tasks',
path: path,
} = useQuery<Task>('select * from tasks where isDeleted = false', {
queryArguments: { val: false },
persistenceDirectory,
})

const [upsert] = useExecuteQuery<unknown, { value: Partial<Task> }>(
'insert into tasks documents (:value) on id conflict do update',
{
queryArguments: { value: { isDeleted: false } },
persistenceDirectory,
},
)

const [removeByID] = useExecuteQuery<unknown, { id: string }>(
'update tasks set isDeleted = true where _id = :id',
{
persistenceDirectory,
},
)

const [setCompletedByID] = useExecuteQuery<
unknown,
Pick<Task, '_id' | 'isCompleted'>
>('update tasks set isCompleted = :isCompleted where _id = :_id', {
persistenceDirectory,
})

if (isLoading) {
Expand All @@ -43,12 +75,13 @@ const App: React.FC<Props> = ({ path }) => {
<div className="App" style={styles.app}>
<div className="header" style={styles.header}>
<p>
Using Ditto with path &ldquo;{path}&ldquo; and query &ldquo;
Using Ditto with persistence directory &ldquo;{persistenceDirectory}
&ldquo; and query &ldquo;
{storeObserver.queryString}&ldquo;
</p>
<span>Number of tasks {tasks?.length}</span>

{error && <p style={styles.error}>Error: {error.message}</p>}
{isError(error) && <p style={styles.error}>Error: {error.message}</p>}

<div style={{ marginTop: '1rem', marginBottom: '1rem' }}>
<input
Expand All @@ -64,6 +97,7 @@ const App: React.FC<Props> = ({ path }) => {
value: {
body: newBodyText,
isCompleted: false,
isDeleted: false,
},
})
setNewBodyText('')
Expand All @@ -85,20 +119,16 @@ const App: React.FC<Props> = ({ path }) => {
</p>
<button
onClick={() => {
removeByID({ _id: task.value._id })
removeByID({ id: task.value._id })
}}
>
Remove
</button>
<button
onClick={() => {
updateByID({
setCompletedByID({
_id: task.value._id,
updateClosure: (mutableDoc) => {
mutableDoc
.at('isCompleted')
.set(!mutableDoc.value.isCompleted)
},
isCompleted: !task.value.isCompleted,
})
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ const AppContainer: React.FC = () => {
useOnlineIdentity()
const [currentPath, setCurrentPath] = useState('/path-development')

const handleCreateDittoInstances = () => {
const handleCreateDittoInstances = async () => {
// Example of how to create a development instance
const dittoDevelopment = new Ditto(
createDevelopment({ appID: 'live.ditto.example', siteID: 1234 }),
createDevelopment({
appID: '12e5e73c-68af-4af8-9322-5fd262ff5c7d',
siteID: 1234,
}),
'/path-development',
)
await dittoDevelopment.disableSyncWithV3()

// Example of how to create an online instance with authentication enabled
const dittoOnline = new Ditto(
Expand Down

0 comments on commit d987ae4

Please sign in to comment.