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 d266ae8
Show file tree
Hide file tree
Showing 3 changed files with 53 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
2 changes: 2 additions & 0 deletions examples/vite-example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VITE_DITTO_APP_ID="12e5e73c-68af-4af8-9322-5fd262ff5c7d"
VITE_DITTO_TOKEN='762bce2f-c27a-4a83-9152-00fd7c8da763'

0 comments on commit d266ae8

Please sign in to comment.