Kaynağa Gözat

standardize the naming for `rootDocId`.

- Remove the `scope` variable suffix
- change `rootDoc_id` to `rootDocId`

GitOrigin-RevId: d8e646ef5f99ba0dbce84ff63c588e63b956a4ec
M Fahru 3 yıl önce
ebeveyn
işleme
4548f504f7

+ 2 - 2
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 { t } = useTranslation()
   const { permissionsLevel } = useEditorContext()
   const { permissionsLevel } = useEditorContext()
   const [docs] = useScopeValue<MainDocument[] | undefined>('docs')
   const [docs] = useScopeValue<MainDocument[] | undefined>('docs')
-  const { rootDoc_id: rootDocId, setRootDocId } = useProjectSettingsContext()
+  const { rootDocId, setRootDocId } = useProjectSettingsContext()
 
 
   const validDocsOptions = useMemo(() => {
   const validDocsOptions = useMemo(() => {
     const filteredDocs =
     const filteredDocs =
@@ -38,7 +38,7 @@ export default function SettingsDocument() {
       value={rootDocId}
       value={rootDocId}
       options={validDocsOptions}
       options={validDocsOptions}
       label={t('main_document')}
       label={t('main_document')}
-      name="rootDoc_id"
+      name="rootDocId"
     />
     />
   )
   )
 }
 }

+ 1 - 1
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 = {
 type ProjectSettingsSetterContextValue = {
   setCompiler: (compiler: ProjectSettingsScope['compiler']) => void
   setCompiler: (compiler: ProjectSettingsScope['compiler']) => void
   setImageName: (imageName: ProjectSettingsScope['imageName']) => void
   setImageName: (imageName: ProjectSettingsScope['imageName']) => void
-  setRootDocId: (rootDocId: ProjectSettingsScope['rootDoc_id']) => void
+  setRootDocId: (rootDocId: ProjectSettingsScope['rootDocId']) => void
   setSpellCheckLanguage: (
   setSpellCheckLanguage: (
     spellCheckLanguage: ProjectSettingsScope['spellCheckLanguage']
     spellCheckLanguage: ProjectSettingsScope['spellCheckLanguage']
   ) => void
   ) => void

+ 3 - 3
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 { useProjectContext } from '../../../shared/context/project-context'
 import useScopeValue from '../../../shared/hooks/use-scope-value'
 import useScopeValue from '../../../shared/hooks/use-scope-value'
 import { type ProjectSettingsScope, saveProjectSettings } from '../utils/api'
 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'
 import useSetSpellCheckLanguage from './use-set-spell-check-language'
 
 
 export default function useProjectWideSettings() {
 export default function useProjectWideSettings() {
@@ -37,7 +37,7 @@ export default function useProjectWideSettings() {
     [projectId, project, setProject]
     [projectId, project, setProject]
   )
   )
 
 
-  const setRootDocId = useSetRootDocId()
+  const { setRootDocId, rootDocId } = useRootDocId()
   const setSpellCheckLanguage = useSetSpellCheckLanguage()
   const setSpellCheckLanguage = useSetSpellCheckLanguage()
 
 
   return {
   return {
@@ -45,7 +45,7 @@ export default function useProjectWideSettings() {
     setCompiler,
     setCompiler,
     imageName: project?.imageName,
     imageName: project?.imageName,
     setImageName,
     setImageName,
-    rootDocId: project?.rootDoc_id,
+    rootDocId,
     setRootDocId,
     setRootDocId,
     spellCheckLanguage: project?.spellCheckLanguage,
     spellCheckLanguage: project?.spellCheckLanguage,
     setSpellCheckLanguage,
     setSpellCheckLanguage,

+ 15 - 11
services/web/frontend/js/features/editor-left-menu/hooks/use-set-root-doc-id.tsx → 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 useScopeValue from '../../../shared/hooks/use-scope-value'
 import { type ProjectSettingsScope, saveProjectSettings } from '../utils/api'
 import { type ProjectSettingsScope, saveProjectSettings } from '../utils/api'
 
 
-export default function useSetRootDocId() {
-  const [rootDocIdScope, setRootDocIdScope] =
-    useScopeValue<ProjectSettingsScope['rootDoc_id']>('project.rootDoc_id')
+export default function useRootDocId() {
+  const [rootDocId, setRootDocId] =
+    useScopeValue<ProjectSettingsScope['rootDocId']>('project.rootDoc_id')
   const { permissionsLevel } = useEditorContext()
   const { permissionsLevel } = useEditorContext()
   const { _id: projectId } = useProjectContext()
   const { _id: projectId } = useProjectContext()
 
 
-  const setRootDocId = useCallback(
-    async (rootDocId: ProjectSettingsScope['rootDoc_id']) => {
+  const setRootDocIdFunc = useCallback(
+    async (newRootDocId: ProjectSettingsScope['rootDocId']) => {
       const allowUpdate =
       const allowUpdate =
-        typeof rootDocIdScope !== 'undefined' &&
+        typeof rootDocId !== 'undefined' &&
         permissionsLevel !== 'readOnly' &&
         permissionsLevel !== 'readOnly' &&
-        rootDocIdScope !== rootDocId
+        rootDocId !== newRootDocId
 
 
       if (allowUpdate) {
       if (allowUpdate) {
         try {
         try {
-          await saveProjectSettings({ projectId, rootDoc_id: rootDocId })
-          setRootDocIdScope(rootDocId)
+          await saveProjectSettings({ projectId, rootDocId: newRootDocId })
+          setRootDocId(newRootDocId)
         } catch (err) {
         } catch (err) {
           // TODO: retry mechanism (max 10x before failed completely and rollback the old value)
           // 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,
+  }
 }
 }

+ 2 - 22
services/web/frontend/js/features/editor-left-menu/utils/api.ts

@@ -26,7 +26,7 @@ export type UserSettingsScope = {
 export type ProjectSettingsScope = {
 export type ProjectSettingsScope = {
   compiler: ProjectCompiler
   compiler: ProjectCompiler
   imageName: string
   imageName: string
-  rootDoc_id: string
+  rootDocId: string
   spellCheckLanguage: 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<ProjectSettingsScope, 'rootDoc_id'> & {
-    rootDocId: string
-  }
->
-
 type SaveProjectSettings = {
 type SaveProjectSettings = {
   projectId: string
   projectId: string
 } & Partial<ProjectSettingsScope>
 } & Partial<ProjectSettingsScope>
@@ -60,23 +53,10 @@ export const saveProjectSettings = async ({
   projectId,
   projectId,
   ...data
   ...data
 }: SaveProjectSettings) => {
 }: 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<never>(`/project/${projectId}/settings`, {
   await postJSON<never>(`/project/${projectId}/settings`, {
     body: {
     body: {
       _csrf: window.csrfToken,
       _csrf: window.csrfToken,
-      ...reqData,
+      ...data,
     },
     },
   })
   })
 }
 }