-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathadd-word.web-view.tsx
More file actions
70 lines (64 loc) · 2.51 KB
/
Copy pathadd-word.web-view.tsx
File metadata and controls
70 lines (64 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { NetworkObject } from '@papi/core';
import papi, { logger } from '@papi/frontend';
import { useLocalizedStrings } from '@papi/frontend/react';
import type { IEntryService, LexiconWebViewProps, PartialEntry } from 'lexicon';
import { useCallback, useEffect, useState } from 'react';
import AddNewEntry from '../components/add-new-entry';
import { LOCALIZED_STRING_KEYS } from '../types/localized-string-keys';
globalThis.webViewComponent = function LexiconAddWord({
analysisLanguage,
projectId,
vernacularLanguage,
word,
}: LexiconWebViewProps) {
const [localizedStrings] = useLocalizedStrings(LOCALIZED_STRING_KEYS);
const [lexiconNetworkObject, setLexiconNetworkObject] = useState<
NetworkObject<IEntryService> | undefined
>();
const [isSubmitting, setIsSubmitting] = useState(false);
const [isSubmitted, setIsSubmitted] = useState(false);
useEffect(() => {
papi.networkObjects
.get<IEntryService>('lexicon.entryService')
// eslint-disable-next-line promise/always-return
.then((networkObject) => {
logger.info('Got network object:', networkObject);
setLexiconNetworkObject(networkObject);
})
.catch((e) => logger.error(`${localizedStrings['%lexicon_error_gettingNetworkObject%']}`, e));
}, [localizedStrings]);
const addEntry = useCallback(
async (entry: PartialEntry) => {
if (!projectId || !lexiconNetworkObject) {
const errMissingParam = localizedStrings['%lexicon_error_missingParam%'];
if (!projectId) logger.warn(`${errMissingParam}projectId`);
if (!lexiconNetworkObject) logger.warn(`${errMissingParam}lexiconNetworkObject`);
return;
}
setIsSubmitted(false);
setIsSubmitting(true);
logger.info(`Adding entry: ${JSON.stringify(entry)}`);
const entryId = (await lexiconNetworkObject.addEntry(projectId, entry))?.id;
setIsSubmitting(false);
if (entryId) {
setIsSubmitted(true);
await papi.commands.sendCommand('lexicon.displayEntry', projectId, entryId);
} else {
logger.error(`${localizedStrings['%lexicon_error_failedToAddEntry%']}`);
}
},
[lexiconNetworkObject, localizedStrings, projectId],
);
return (
<div className="tw:p-4">
<AddNewEntry
addEntry={addEntry}
analysisLanguage={analysisLanguage ?? ''}
headword={word}
vernacularLanguage={vernacularLanguage ?? ''}
/>
{isSubmitting && <p>Adding entry to lexicon...</p>}
{isSubmitted && <p>Entry added!</p>}
</div>
);
};