From b06087d7005fb0f2488a0f2ced78d9d80ce557b9 Mon Sep 17 00:00:00 2001 From: M Fahru Date: Tue, 3 Jan 2023 15:33:31 -0700 Subject: [PATCH] standardize the naming for `rootDocId`. - Remove the `scope` variable suffix - change `rootDoc_id` to `rootDocId` GitOrigin-RevId: d8e646ef5f99ba0dbce84ff63c588e63b956a4ec --- .../components/settings/settings-document.tsx | 4 +-- .../context/project-settings-context.tsx | 2 +- .../hooks/use-project-wide-settings.tsx | 6 ++--- ...et-root-doc-id.tsx => use-root-doc-id.tsx} | 26 +++++++++++-------- .../js/features/editor-left-menu/utils/api.ts | 24 ++--------------- 5 files changed, 23 insertions(+), 39 deletions(-) rename services/web/frontend/js/features/editor-left-menu/hooks/{use-set-root-doc-id.tsx => use-root-doc-id.tsx} (54%) diff --git a/services/web/frontend/js/features/editor-left-menu/components/settings/settings-document.tsx b/services/web/frontend/js/features/editor-left-menu/components/settings/settings-document.tsx index 41e4ea9207..0478b9432d 100644 --- a/services/web/frontend/js/features/editor-left-menu/components/settings/settings-document.tsx +++ b/services/web/frontend/js/features/editor-left-menu/components/settings/settings-document.tsx @@ -12,7 +12,7 @@ export default function SettingsDocument() { const { t } = useTranslation() const { permissionsLevel } = useEditorContext() const [docs] = useScopeValue('docs') - const { rootDoc_id: rootDocId, setRootDocId } = useProjectSettingsContext() + const { rootDocId, setRootDocId } = useProjectSettingsContext() const validDocsOptions = useMemo(() => { const filteredDocs = @@ -38,7 +38,7 @@ export default function SettingsDocument() { value={rootDocId} options={validDocsOptions} label={t('main_document')} - name="rootDoc_id" + name="rootDocId" /> ) } diff --git a/services/web/frontend/js/features/editor-left-menu/context/project-settings-context.tsx b/services/web/frontend/js/features/editor-left-menu/context/project-settings-context.tsx index 0dcb72de6a..bdf4d5f806 100644 --- a/services/web/frontend/js/features/editor-left-menu/context/project-settings-context.tsx +++ b/services/web/frontend/js/features/editor-left-menu/context/project-settings-context.tsx @@ -8,7 +8,7 @@ import type { ProjectSettingsScope, UserSettingsScope } from '../utils/api' type ProjectSettingsSetterContextValue = { setCompiler: (compiler: ProjectSettingsScope['compiler']) => void setImageName: (imageName: ProjectSettingsScope['imageName']) => void - setRootDocId: (rootDocId: ProjectSettingsScope['rootDoc_id']) => void + setRootDocId: (rootDocId: ProjectSettingsScope['rootDocId']) => void setSpellCheckLanguage: ( spellCheckLanguage: ProjectSettingsScope['spellCheckLanguage'] ) => void diff --git a/services/web/frontend/js/features/editor-left-menu/hooks/use-project-wide-settings.tsx b/services/web/frontend/js/features/editor-left-menu/hooks/use-project-wide-settings.tsx index 1e051d253a..12db02f968 100644 --- a/services/web/frontend/js/features/editor-left-menu/hooks/use-project-wide-settings.tsx +++ b/services/web/frontend/js/features/editor-left-menu/hooks/use-project-wide-settings.tsx @@ -2,7 +2,7 @@ import { useCallback } from 'react' import { useProjectContext } from '../../../shared/context/project-context' import useScopeValue from '../../../shared/hooks/use-scope-value' import { type ProjectSettingsScope, saveProjectSettings } from '../utils/api' -import useSetRootDocId from './use-set-root-doc-id' +import useRootDocId from './use-root-doc-id' import useSetSpellCheckLanguage from './use-set-spell-check-language' export default function useProjectWideSettings() { @@ -37,7 +37,7 @@ export default function useProjectWideSettings() { [projectId, project, setProject] ) - const setRootDocId = useSetRootDocId() + const { setRootDocId, rootDocId } = useRootDocId() const setSpellCheckLanguage = useSetSpellCheckLanguage() return { @@ -45,7 +45,7 @@ export default function useProjectWideSettings() { setCompiler, imageName: project?.imageName, setImageName, - rootDocId: project?.rootDoc_id, + rootDocId, setRootDocId, spellCheckLanguage: project?.spellCheckLanguage, setSpellCheckLanguage, diff --git a/services/web/frontend/js/features/editor-left-menu/hooks/use-set-root-doc-id.tsx b/services/web/frontend/js/features/editor-left-menu/hooks/use-root-doc-id.tsx similarity index 54% rename from services/web/frontend/js/features/editor-left-menu/hooks/use-set-root-doc-id.tsx rename to services/web/frontend/js/features/editor-left-menu/hooks/use-root-doc-id.tsx index 720a8b76ab..78754758bb 100644 --- a/services/web/frontend/js/features/editor-left-menu/hooks/use-set-root-doc-id.tsx +++ b/services/web/frontend/js/features/editor-left-menu/hooks/use-root-doc-id.tsx @@ -4,29 +4,33 @@ import { useProjectContext } from '../../../shared/context/project-context' import useScopeValue from '../../../shared/hooks/use-scope-value' import { type ProjectSettingsScope, saveProjectSettings } from '../utils/api' -export default function useSetRootDocId() { - const [rootDocIdScope, setRootDocIdScope] = - useScopeValue('project.rootDoc_id') +export default function useRootDocId() { + const [rootDocId, setRootDocId] = + useScopeValue('project.rootDoc_id') const { permissionsLevel } = useEditorContext() const { _id: projectId } = useProjectContext() - const setRootDocId = useCallback( - async (rootDocId: ProjectSettingsScope['rootDoc_id']) => { + const setRootDocIdFunc = useCallback( + async (newRootDocId: ProjectSettingsScope['rootDocId']) => { const allowUpdate = - typeof rootDocIdScope !== 'undefined' && + typeof rootDocId !== 'undefined' && permissionsLevel !== 'readOnly' && - rootDocIdScope !== rootDocId + rootDocId !== newRootDocId if (allowUpdate) { try { - await saveProjectSettings({ projectId, rootDoc_id: rootDocId }) - setRootDocIdScope(rootDocId) + await saveProjectSettings({ projectId, rootDocId: newRootDocId }) + setRootDocId(newRootDocId) } catch (err) { // TODO: retry mechanism (max 10x before failed completely and rollback the old value) } } }, - [permissionsLevel, projectId, rootDocIdScope, setRootDocIdScope] + [permissionsLevel, projectId, rootDocId, setRootDocId] ) - return setRootDocId + + return { + rootDocId, + setRootDocId: setRootDocIdFunc, + } } diff --git a/services/web/frontend/js/features/editor-left-menu/utils/api.ts b/services/web/frontend/js/features/editor-left-menu/utils/api.ts index fe24a795d2..512d5fab31 100644 --- a/services/web/frontend/js/features/editor-left-menu/utils/api.ts +++ b/services/web/frontend/js/features/editor-left-menu/utils/api.ts @@ -26,7 +26,7 @@ export type UserSettingsScope = { export type ProjectSettingsScope = { compiler: ProjectCompiler imageName: string - rootDoc_id: string + rootDocId: string spellCheckLanguage: string } @@ -45,13 +45,6 @@ export function saveUserSettings(data: SaveUserSettings) { }) } -// server asks for "rootDocId" but client has "rootDoc_id" -type ProjectSettingsRequestBody = Partial< - Omit & { - rootDocId: string - } -> - type SaveProjectSettings = { projectId: string } & Partial @@ -60,23 +53,10 @@ export const saveProjectSettings = async ({ projectId, ...data }: SaveProjectSettings) => { - let reqData: ProjectSettingsRequestBody = {} - - if (data.rootDoc_id) { - const val = data.rootDoc_id - delete data.rootDoc_id - reqData = { - ...data, - rootDocId: val, - } - } else { - reqData = data - } - await postJSON(`/project/${projectId}/settings`, { body: { _csrf: window.csrfToken, - ...reqData, + ...data, }, }) }