chore: code factorization

This commit is contained in:
Grégoire Cutzach 2024-10-09 19:06:57 +02:00
parent 6750cfd31d
commit 324bab1a34
No known key found for this signature in database
GPG Key ID: AA4155BE23C375E6

View File

@ -29,7 +29,17 @@ import {commonUrls, GristLoadConfig} from 'app/common/gristUrls';
import {not, propertyCompare} from 'app/common/gutil'; import {not, propertyCompare} from 'app/common/gutil';
import {getCurrency, locales} from 'app/common/Locales'; import {getCurrency, locales} from 'app/common/Locales';
import {isOwner, isOwnerOrEditor} from 'app/common/roles'; import {isOwner, isOwnerOrEditor} from 'app/common/roles';
import {Computed, Disposable, dom, fromKo, IDisposableOwner, makeTestId, Observable, styled} from 'grainjs'; import {
Computed,
Disposable,
dom,
DomElementMethod,
fromKo,
IDisposableOwner,
makeTestId,
Observable,
styled
} from 'grainjs';
import * as moment from 'moment-timezone'; import * as moment from 'moment-timezone';
import {DocumentType} from 'app/common/UserAPI'; import {DocumentType} from 'app/common/UserAPI';
@ -341,46 +351,56 @@ export class DocSettingsPage extends Disposable {
})); }));
}; };
const docTypeOption = (
{
type,
label,
description,
itemTestId
}: {
type: DocTypeOption,
label: string | any,
description: string,
itemTestId: DomElementMethod | null
}) => {
return radioCheckboxOption(selected, type, dom('div',
dom('div',
dom('strong', label),
),
dom('div',
dom.style('margin-top', '8px'),
dom('span', description)
),
itemTestId,
));
};
const documentTypeOptions = () => [ const documentTypeOptions = () => [
cssRadioCheckboxOptions( cssRadioCheckboxOptions(
dom.style('max-width', '400px'), dom.style('max-width', '400px'),
radioCheckboxOption(selected, DocTypeOption.Regular, dom('div', docTypeOption({
dom('div', type: DocTypeOption.Regular,
dom('strong', t('Regular document')), label: t('Regular document'),
description: t('Regular document behavior, all users work on the same copy of the document.'),
itemTestId: testId('doctype-modal-option-regular'),
}),
docTypeOption({
type: DocTypeOption.Template,
label: t('Template'),
description: t('Document automatically opens in {{fiddleModeDocUrl}}. ' +
'Any edit (open to anybody) will create a new unsaved copy.',
{
fiddleModeDocUrl: cssLink({href: commonUrls.helpAPI, target: '_blank'}, t('fiddle mode'))
}
), ),
dom('div', itemTestId: testId('doctype-modal-option-template'),
dom.style('margin-top', '8px'), }),
dom('span', t('Regular document behavior, all users work on the same copy of the document.')) docTypeOption({
), type: DocTypeOption.Tutorial,
testId('doctype-modal-option-regular'), label: t('Tutorial'),
)), description: t('Document automatically opens with a new copy.'),
radioCheckboxOption(selected, DocTypeOption.Template, dom('div', itemTestId: testId('doctype-modal-option-tutorial'),
dom('div', }),
dom('strong', t('Template')),
),
dom('div',
dom.style('margin-top', '8px'),
dom('span',
t('Document automatically opens in {{fiddleModeDocUrl}}. ' +
'Any edit (open to anybody) will create a new unsaved copy.',
{
fiddleModeDocUrl: cssLink({href: commonUrls.helpAPI, target: '_blank'}, t('fiddle mode'))
}
)
),
),
testId('doctype-modal-option-template'),
)),
radioCheckboxOption(selected, DocTypeOption.Tutorial, dom('div',
dom('div',
dom('strong', t('Tutorial')),
),
dom('div',
dom.style('margin-top', '8px'),
dom('span', t('Document automatically opens with a new copy.')),
),
testId('doctype-modal-option-tutorial'),
))
), ),
cssModalButtons( cssModalButtons(
bigBasicButton(t('Cancel'), dom.on('click', () => ctl.close()), testId('doctype-modal-cancel')), bigBasicButton(t('Cancel'), dom.on('click', () => ctl.close()), testId('doctype-modal-cancel')),
@ -409,12 +429,12 @@ export class DocSettingsPage extends Disposable {
function persistType(type: string|null, docId: string|undefined){ function persistType(type: string|null, docId: string|undefined){
docId = docId?.split("~")[0]; docId = docId?.split("~")[0];
return fetch(`/o/docs/api/docs/${docId}`, return fetch(`/o/docs/api/docs/${docId}`, {
{ method:'PATCH', method: 'PATCH',
headers: {"Content-Type": "application/json"}, headers: {"Content-Type": "application/json"},
credentials: 'include', credentials: 'include',
body:JSON.stringify({type}) body: JSON.stringify({type})
}); });
} }
function getApiConsoleLink(docPageModel: DocPageModel) { function getApiConsoleLink(docPageModel: DocPageModel) {
@ -468,8 +488,7 @@ const typeList: DocumentTypeItem[] = [{
}, { }, {
label: t('Template'), label: t('Template'),
type: 'template' type: 'template'
}, }, {
{
label: t('Tutorial'), label: t('Tutorial'),
type: 'tutorial' type: 'tutorial'
}].map((el) => ({ }].map((el) => ({
@ -483,7 +502,7 @@ function displayCurrentType(
type: Observable<DocumentType|null>, type: Observable<DocumentType|null>,
) { ) {
const typeObs = Computed.create(owner, use => { const typeObs = Computed.create(owner, use => {
const typeCode = use(type)??""; const typeCode = use(type) ?? "";
const typeName = typeList.find(ty => ty.type === typeCode)?.label || typeCode; const typeName = typeList.find(ty => ty.type === typeCode)?.label || typeCode;
return typeName; return typeName;
}); });