From d987ae4e8707dac3c40608bbe1bc3ac11f6634e9 Mon Sep 17 00:00:00 2001 From: Vincent Ahrend Date: Fri, 20 Sep 2024 15:52:41 +0200 Subject: [PATCH] WIP update example app --- .../src/App.tsx | 60 ++++++++++++++----- .../src/AppContainer.tsx | 8 ++- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/examples/dql-create-react-app-typescript-example/src/App.tsx b/examples/dql-create-react-app-typescript-example/src/App.tsx index a4d8e61..ce8ba05 100644 --- a/examples/dql-create-react-app-typescript-example/src/App.tsx +++ b/examples/dql-create-react-app-typescript-example/src/App.tsx @@ -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 = { app: { maxWidth: '600px', @@ -22,17 +29,42 @@ type Props = { path: string } -const App: React.FC = ({ path }) => { +function isError(error: unknown): error is Error { + return error instanceof Error +} + +const App: React.FC = ({ path: persistenceDirectory }) => { const [newBodyText, setNewBodyText] = useState('') const { items: tasks, error, storeObserver, isLoading, - } = useQuery('select * from tasks') - const { upsert, removeByID, updateByID } = useMutations({ - collection: 'tasks', - path: path, + } = useQuery('select * from tasks where isDeleted = false', { + queryArguments: { val: false }, + persistenceDirectory, + }) + + const [upsert] = useExecuteQuery }>( + 'insert into tasks documents (:value) on id conflict do update', + { + queryArguments: { value: { isDeleted: false } }, + persistenceDirectory, + }, + ) + + const [removeByID] = useExecuteQuery( + 'update tasks set isDeleted = true where _id = :id', + { + persistenceDirectory, + }, + ) + + const [setCompletedByID] = useExecuteQuery< + unknown, + Pick + >('update tasks set isCompleted = :isCompleted where _id = :_id', { + persistenceDirectory, }) if (isLoading) { @@ -43,12 +75,13 @@ const App: React.FC = ({ path }) => {

- Using Ditto with path “{path}“ and query “ + Using Ditto with persistence directory “{persistenceDirectory} + “ and query “ {storeObserver.queryString}“

Number of tasks {tasks?.length} - {error &&

Error: {error.message}

} + {isError(error) &&

Error: {error.message}

}
= ({ path }) => { value: { body: newBodyText, isCompleted: false, + isDeleted: false, }, }) setNewBodyText('') @@ -85,20 +119,16 @@ const App: React.FC = ({ path }) => {