standardize the naming for rootDocId.

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

GitOrigin-RevId: d8e646ef5f99ba0dbce84ff63c588e63b956a4ec
This commit is contained in:
M Fahru 2023-01-03 15:33:31 -07:00 committed by Copybot
parent bdf3b18d4f
commit 4548f504f7
5 changed files with 23 additions and 39 deletions

View file

@ -12,7 +12,7 @@ export default function SettingsDocument() {
const { t } = useTranslation()
const { permissionsLevel } = useEditorContext()
const [docs] = useScopeValue<MainDocument[] | undefined>('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"
/>
)
}

View file

@ -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

View file

@ -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,

View file

@ -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<ProjectSettingsScope['rootDoc_id']>('project.rootDoc_id')
export default function useRootDocId() {
const [rootDocId, setRootDocId] =
useScopeValue<ProjectSettingsScope['rootDocId']>('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,
}
}

View file

@ -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<ProjectSettingsScope, 'rootDoc_id'> & {
rootDocId: string
}
>
type SaveProjectSettings = {
projectId: string
} & Partial<ProjectSettingsScope>
@ -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<never>(`/project/${projectId}/settings`, {
body: {
_csrf: window.csrfToken,
...reqData,
...data,
},
})
}