mirror of
https://github.com/urbit/shrub.git
synced 2025-01-02 17:43:32 +03:00
commit
70fd944a25
@ -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 (
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user