Skip to content

Commit

Permalink
Fixed missing validators throwing, added non-zero requirement to full…
Browse files Browse the repository at this point in the history
… name validator, fixed test hello-world.personName setting
  • Loading branch information
tjcouch-sil committed Apr 17, 2024
1 parent f4908e4 commit 2986aaf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
6 changes: 6 additions & 0 deletions extensions/src/hello-world/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ export async function activate(context: ExecutionActivationContext): Promise<voi
openHelloWorldProjectWebView,
);

const helloWorldPersonNamePromise = papi.settings.registerValidator(
'hello-world.personName',
async (newValue) => typeof newValue === 'string',
);

const helloWorldProjectWebViewProviderPromise = papi.webViewProviders.register(
helloWorldProjectWebViewProvider.webViewType,
helloWorldProjectWebViewProvider,
Expand Down Expand Up @@ -249,6 +254,7 @@ export async function activate(context: ExecutionActivationContext): Promise<voi
await helloWorldPdpefPromise,
await helloWorldProjectWebViewProviderPromise,
await openHelloWorldProjectPromise,
await helloWorldPersonNamePromise,
await htmlWebViewProviderPromise,
await reactWebViewProviderPromise,
await reactWebView2ProviderPromise,
Expand Down
28 changes: 26 additions & 2 deletions extensions/src/hello-world/src/web-views/hello-world.web-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import type { WebViewProps } from '@papi/core';
import { Key, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import type { HelloWorldEvent } from 'hello-world';
import { debounce } from 'platform-bible-utils';
import Clock from './components/clock.component';
import Logo from '../../assets/offline.svg';

Expand Down Expand Up @@ -172,7 +173,30 @@ globalThis.webViewComponent = function HelloWorld({
),
);

const [name, setName] = useSetting('hello-world.personName', 'Kathy');
const [name, setNameInternal] = useSetting('hello-world.personName', 'Kathy');

// Name used for display and editing in the input field while debouncing the actual setting change
const [nameTemp, setNameTemp] = useState(name);

useEffect(() => {
setNameTemp(name);
}, [name]);

const debouncedSetName = useMemo(
() =>
debounce((newName) => {
setNameInternal(newName);
}, 300),
[setNameInternal],
);

const setName = useCallback(
(newName: string) => {
setNameTemp(newName);
debouncedSetName(newName);
},
[debouncedSetName],
);

const peopleDataProvider = useDataProvider('helloSomeone.people');

Expand Down Expand Up @@ -232,7 +256,7 @@ globalThis.webViewComponent = function HelloWorld({
<div>{latestVerseText}</div>
<Clock />
<div>
<input value={name} onChange={(e) => setName(e.target.value)} />
<input value={nameTemp} onChange={(e) => setName(e.target.value)} />
<Button onClick={() => peopleDataProvider?.deletePerson(name)}>Delete {name}</Button>
</div>
<div>{personGreeting}</div>
Expand Down
2 changes: 1 addition & 1 deletion src/extension-host/data/core-project-settings-info.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const platformProjectSettings: ProjectSettingsContribution = {
};

const fullNameValidator: ProjectSettingValidator<'platform.fullName'> = async (newValue) => {
return typeof newValue === 'string';
return typeof newValue === 'string' && newValue.length > 0;
};

// TODO: Validate that strings in the array to match BCP 47 values once the i18n code is ready
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function isValid<ProjectSettingName extends ProjectSettingNames>(
return true;
}
try {
return networkService.request(
return await networkService.request(
serializeRequestType(CATEGORY_EXTENSION_PROJECT_SETTING_VALIDATOR, key),
newValue,
currentValue,
Expand Down
2 changes: 1 addition & 1 deletion src/extension-host/services/settings.service-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async function validateSetting<SettingName extends SettingNames>(
return true;
}
try {
return networkService.request(
return await networkService.request(
serializeRequestType(CATEGORY_EXTENSION_SETTING_VALIDATOR, key),
newValue,
currentValue,
Expand Down

0 comments on commit 2986aaf

Please sign in to comment.