|
1 | | -import React, { useContext, useState } from 'react'; |
2 | | -import { SafeAreaView, ScrollView } from 'react-native'; |
3 | | -import { useNavigation } from '@react-navigation/native'; |
| 1 | +import React, { useState } from 'react'; |
4 | 2 | import { useStyleScheme } from '@/components/Themed'; |
5 | | -import ResultListView from '@/components/ResultsListView'; |
6 | | -import DatabaseContext from '@/providers/DatabaseContext'; |
7 | | -import useNavigationBarTitleResetOption from '@/hooks/useNavigationBarTitleResetOption'; |
8 | | -import DatabaseScopeCollectionForm from '@/components/DatabaseScopeCollectionForm'; |
9 | | -import HeaderView from '@/components/HeaderView'; |
10 | | -import DocumentIdActionForm from '@/components/DocumentIdActionForm'; |
11 | | -//import get from '@/service/document/get'; |
| 3 | +import { Collection, Blob } from 'cbl-reactnative'; |
| 4 | +import CBLDocumentIdCollectionActionContainer from '@/components/CBLDocumentIdCollectionActionContainer'; |
| 5 | +import { StyledTextInput } from '@/components/StyledTextInput'; |
| 6 | +import { Divider } from '@gluestack-ui/themed'; |
| 7 | +import setBlob from '@/service/document/setBlob'; |
12 | 8 |
|
13 | 9 | export default function DocumentSetBlobScreen() { |
14 | 10 | //database stuff |
15 | | - const { databases } = useContext(DatabaseContext)!; |
16 | | - const [databaseName, setDatabaseName] = useState<string>(''); |
17 | | - const [scopeName, setScopeName] = useState<string>(''); |
18 | | - const [collectionName, setCollectionName] = useState<string>(''); |
19 | | - const [documentId, setDocumentId] = useState<string>(''); |
20 | 11 | const [key, setKey] = useState<string>(''); |
| 12 | + const [blobText, setBlobText] = useState<string>(''); |
21 | 13 | //results |
22 | | - const [resultMessage, setResultsMessage] = useState<string[]>([]); |
23 | | - //drawing stuff |
24 | | - const navigation = useNavigation(); |
25 | 14 | const styles = useStyleScheme(); |
26 | | - useNavigationBarTitleResetOption('Get Document Text Blob', navigation, reset); |
27 | 15 |
|
28 | 16 | function reset() { |
29 | | - setDatabaseName(''); |
30 | | - setScopeName(''); |
31 | | - setCollectionName(''); |
32 | | - setDocumentId(''); |
33 | 17 | setKey(''); |
34 | | - setResultsMessage([]); |
35 | 18 | } |
36 | 19 |
|
37 | | - const update = async () => { |
38 | | - if (databaseName === '') { |
39 | | - setResultsMessage((prev) => [ |
40 | | - ...prev, |
41 | | - 'Error: Database name is required', |
42 | | - ]); |
43 | | - } else { |
44 | | - try { |
45 | | - if (documentId === '') { |
46 | | - setResultsMessage((prev) => [ |
47 | | - ...prev, |
48 | | - 'Error: Document ID is required', |
49 | | - ]); |
50 | | - return; |
51 | | - } |
52 | | - /* |
53 | | - const doc = await get( |
54 | | - databases, |
55 | | - databaseName, |
56 | | - scopeName, |
57 | | - collectionName, |
58 | | - documentId |
59 | | - ); |
60 | | - if (doc !== undefined && doc !== null) { |
61 | | - const json = JSON.stringify(doc.toDictionary()); |
62 | | - const resultsMessage = `Document <${documentId}> found with JSON: ${json}`; |
63 | | - setResultsMessage((prev) => [...prev, resultsMessage]); |
64 | | - } else { |
65 | | - setResultsMessage((prev) => [ |
66 | | - ...prev, |
67 | | - 'Error: Document could not be retrieved', |
68 | | - ]); |
69 | | - } |
70 | | - */ |
71 | | - } catch (error) { |
72 | | - // @ts-ignore |
73 | | - setResultsMessage((prev) => [...prev, error.message]); |
| 20 | + async function update( |
| 21 | + collection: Collection, |
| 22 | + documentId: string |
| 23 | + ): Promise<string[]> { |
| 24 | + try { |
| 25 | + const encoder = new TextEncoder(); |
| 26 | + const blob = new Blob('text/plain', encoder.encode(blobText)); |
| 27 | + const mutDoc = await setBlob(collection, documentId, key, blob); |
| 28 | + if ( |
| 29 | + mutDoc !== undefined && |
| 30 | + mutDoc !== null && |
| 31 | + mutDoc.getId() === documentId |
| 32 | + ) { |
| 33 | + return [ |
| 34 | + `Blob with key <${key}> set on Document <${documentId}> in Collection <${collection.fullName()}> Database <${collection.database.getName()}>`, |
| 35 | + ]; |
| 36 | + } else { |
| 37 | + return ['Error: Blob could not be set']; |
74 | 38 | } |
| 39 | + } catch (error) { |
| 40 | + // @ts-ignore |
| 41 | + return [error.message]; |
75 | 42 | } |
76 | | - }; |
| 43 | + } |
77 | 44 |
|
78 | 45 | return ( |
79 | | - <SafeAreaView style={styles.container}> |
80 | | - <ScrollView style={styles.container}> |
81 | | - <HeaderView name="Collection" iconName="bookshelf" /> |
82 | | - <DatabaseScopeCollectionForm |
83 | | - databaseName={databaseName} |
84 | | - setDatabaseName={setDatabaseName} |
85 | | - scopeName={scopeName} |
86 | | - setScopeName={setScopeName} |
87 | | - collectionName={collectionName} |
88 | | - setCollectionName={setCollectionName} |
89 | | - /> |
90 | | - <DocumentIdActionForm |
91 | | - documentId={documentId} |
92 | | - setDocumentId={setDocumentId} |
93 | | - handleUpdatePressed={update} |
94 | | - /> |
95 | | - <ResultListView messages={resultMessage} /> |
96 | | - </ScrollView> |
97 | | - </SafeAreaView> |
| 46 | + <CBLDocumentIdCollectionActionContainer |
| 47 | + screenTitle="Set Blob" |
| 48 | + handleUpdatePressed={update} |
| 49 | + handleResetPressed={reset} |
| 50 | + > |
| 51 | + <Divider style={{ marginTop: 5, marginBottom: 10, marginLeft: 8 }} /> |
| 52 | + <StyledTextInput |
| 53 | + style={{ marginBottom: 5 }} |
| 54 | + autoCapitalize="none" |
| 55 | + placeholder="Blob Key" |
| 56 | + onChangeText={(keyText) => setKey(keyText)} |
| 57 | + defaultValue={key} |
| 58 | + /> |
| 59 | + <Divider style={{ marginTop: 5, marginBottom: 10, marginLeft: 8 }} /> |
| 60 | + <StyledTextInput |
| 61 | + autoCapitalize="none" |
| 62 | + style={[ |
| 63 | + styles.textInput, |
| 64 | + { height: undefined, minHeight: 120, marginTop: 5, marginBottom: 15 }, |
| 65 | + ]} |
| 66 | + placeholder="Blob Text" |
| 67 | + onChangeText={(newBlobText) => setBlobText(newBlobText)} |
| 68 | + defaultValue={blobText} |
| 69 | + multiline={true} |
| 70 | + /> |
| 71 | + </CBLDocumentIdCollectionActionContainer> |
98 | 72 | ); |
99 | 73 | } |
0 commit comments