|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Collection } from 'cbl-reactnative'; |
| 3 | +import CBLCollectionActionContainer from '@/components/CBLCollectionActionContainer'; |
| 4 | +import HeaderView from '@/components/HeaderView'; |
| 5 | +import { Divider, Switch } from '@gluestack-ui/themed'; |
| 6 | +import { StyledTextInput } from '@/components/StyledTextInput'; |
| 7 | +import createFts from '@/service/indexes/createFts'; |
| 8 | +import { View } from 'react-native'; |
| 9 | +import { Text } from '@/components/Themed'; |
| 10 | + |
| 11 | +export default function IndexFtsCreateScreen() { |
| 12 | + const [indexName, setIndexName] = useState<string>(''); |
| 13 | + const [ignoreAccents, setIgnoreAccents] = useState<boolean>(false); |
| 14 | + const [indexProperties, setIndexProperties] = useState<string>(''); |
| 15 | + |
| 16 | + function reset() { |
| 17 | + setIgnoreAccents(false); |
| 18 | + setIndexName(''); |
| 19 | + setIndexProperties(''); |
| 20 | + } |
| 21 | + |
| 22 | + async function update(collection: Collection): Promise<string[]> { |
| 23 | + try { |
| 24 | + await createFts(collection, indexName, indexProperties, ignoreAccents); |
| 25 | + return [`Index ${indexName} was created successfully`]; |
| 26 | + } catch (error) { |
| 27 | + // @ts-ignore |
| 28 | + return [error.message]; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return ( |
| 33 | + <CBLCollectionActionContainer |
| 34 | + handleUpdatePressed={update} |
| 35 | + handleResetPressed={reset} |
| 36 | + screenTitle="Create FTS Index" |
| 37 | + > |
| 38 | + <HeaderView name="Index" iconName="magnify" /> |
| 39 | + <StyledTextInput |
| 40 | + autoCapitalize="none" |
| 41 | + placeholder="IndexName" |
| 42 | + onChangeText={(newText) => setIndexName(newText)} |
| 43 | + defaultValue={indexName} |
| 44 | + /> |
| 45 | + <Divider style={{ marginLeft: 8, marginTop: 10, marginBottom: 10 }} /> |
| 46 | + <StyledTextInput |
| 47 | + style={{ |
| 48 | + height: 120, |
| 49 | + minHeight: 20, |
| 50 | + marginBottom: 10, |
| 51 | + }} |
| 52 | + autoCapitalize="none" |
| 53 | + placeholder="Index Properties (comma separated)" |
| 54 | + onChangeText={(newText) => setIndexProperties(newText)} |
| 55 | + defaultValue={indexProperties} |
| 56 | + multiline={true} |
| 57 | + /> |
| 58 | + <Divider style={{ marginTop: 10, marginBottom: 10 }} /> |
| 59 | + <View |
| 60 | + style={{ |
| 61 | + flexDirection: 'row', |
| 62 | + justifyContent: 'space-between', |
| 63 | + alignItems: 'center', |
| 64 | + marginBottom: 16, |
| 65 | + }} |
| 66 | + > |
| 67 | + <Text style={{ paddingLeft: 6, fontSize: 16 }}>Ignore Accents</Text> |
| 68 | + <Switch |
| 69 | + style={{ paddingRight: 16 }} |
| 70 | + value={ignoreAccents} |
| 71 | + onValueChange={setIgnoreAccents} |
| 72 | + /> |
| 73 | + </View> |
| 74 | + </CBLCollectionActionContainer> |
| 75 | + ); |
| 76 | +} |
0 commit comments