Merge pull request #5018 from urbit/lf/benzo

CalmEngine: Even calmer
This commit is contained in:
matildepark 2021-06-15 18:28:00 -05:00 committed by GitHub
commit 70fd944a25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 13 deletions

View File

@ -3,14 +3,13 @@ import {
Text
} from '@tlon/indigo-react';
import { Form, Formik, FormikHelpers } from 'formik';
import { Form } from 'formik';
import React, { useCallback } from 'react';
import GlobalApi from '~/logic/api/global';
import useSettingsState, { selectSettingsState, SettingsState } from '~/logic/state/settings';
import { AsyncButton } from '~/views/components/AsyncButton';
import useSettingsState, { SettingsState } from '~/logic/state/settings';
import { BackButton } from './BackButton';
import _ from 'lodash';
import {FormikOnBlur} from '~/views/components/FormikOnBlur';
import { FormikOnBlur } from '~/views/components/FormikOnBlur';
interface FormSchema {
hideAvatars: boolean;
@ -36,23 +35,19 @@ const settingsSel = (s: SettingsState): FormSchema => ({
audioShown: !s.remoteContentPolicy.audioShown
});
export function CalmPrefs(props: {
api: GlobalApi;
}) {
const { api } = props;
const initialValues = useSettingsState(settingsSel);
const onSubmit = useCallback(async (v: FormSchema, actions: FormikHelpers<FormSchema>) => {
let promises: Promise<any>[] = [];
const onSubmit = useCallback(async (v: FormSchema) => {
_.forEach(v, (bool, key) => {
const bucket = ['imageShown', 'videoShown', 'audioShown', 'oembedShown'].includes(key) ? 'remoteContentPolicy' : 'calm';
if(initialValues[key] !== bool) {
promises.push(api.settings.putEntry(bucket, key, bool));
api.settings.putEntry(bucket, key, bool);
}
})
await Promise.all(promises);
actions.setStatus({ success: null });
});
}, [api]);
return (

View File

@ -11,14 +11,14 @@ export function FormikOnBlur<
useEffect(() => {
if (
Object.keys(formikBag.errors || {}).length === 0 &&
formikBag.dirty &&
formikBag.dirty &&
!formikBag.isSubmitting &&
!submitting
) {
setSubmitting(true);
const { values } = formikBag;
formikBag.submitForm().then(() => {
formikBag.resetForm({ values })
formikBag.resetForm({ values });
setSubmitting(false);
});
}
@ -29,6 +29,10 @@ export function FormikOnBlur<
formikBag.isSubmitting
]);
useEffect(() => {
formikBag.resetForm({ values: props.initialValues });
}, [props.initialValues]);
const { children, innerRef } = props;
useImperativeHandle(innerRef, () => formikBag);