Files
sori.studio/lib/admin-post-settings-section.js

89 lines
3.2 KiB
JavaScript

import {
DEFAULT_FEATURED_POST_LIMIT,
DEFAULT_HOME_LATEST_POST_LIMIT,
DEFAULT_PAGE_SECTION_ITEM_LIMIT,
DEFAULT_POST_LIST_PAGE_SIZE,
DEFAULT_TAG_POST_LIST_PAGE_SIZE,
normalizeDisplayCount
} from './display-count-settings.js'
import {
DEFAULT_POST_TAG_LIMIT,
normalizePostTagLimit
} from './post-tag-limit.js'
import {
DEFAULT_UPLOAD_MAX_FILE_COUNT,
DEFAULT_UPLOAD_MAX_TOTAL_SIZE,
normalizeUploadMaxFileCount,
normalizeUploadMaxTotalSize
} from './upload-size-limit.js'
export const POST_SETTINGS_FIELDS = [
'showPostUpdatedAt',
'postTagLimit',
'homeLatestPostLimit',
'postListPageSize',
'tagPostListPageSize',
'featuredPostLimit',
'pageSectionItemLimit',
'uploadMaxFileCount',
'uploadMaxTotalSize'
]
/**
* POST 설정 기본 스냅샷을 만든다.
* @returns {Object} 기본 POST 설정 값
*/
export const createDefaultPostSettingsSnapshot = () => ({
showPostUpdatedAt: false,
postTagLimit: DEFAULT_POST_TAG_LIMIT,
homeLatestPostLimit: DEFAULT_HOME_LATEST_POST_LIMIT,
postListPageSize: DEFAULT_POST_LIST_PAGE_SIZE,
tagPostListPageSize: DEFAULT_TAG_POST_LIST_PAGE_SIZE,
featuredPostLimit: DEFAULT_FEATURED_POST_LIMIT,
pageSectionItemLimit: DEFAULT_PAGE_SECTION_ITEM_LIMIT,
uploadMaxFileCount: DEFAULT_UPLOAD_MAX_FILE_COUNT,
uploadMaxTotalSize: DEFAULT_UPLOAD_MAX_TOTAL_SIZE
})
/**
* POST 설정 값을 저장·비교 가능한 형태로 정규화한다.
* @param {Object} source - 원본 설정 값
* @returns {Object} 정규화된 POST 설정 값
*/
export const normalizePostSettingsSection = (source = {}) => ({
showPostUpdatedAt: Boolean(source.showPostUpdatedAt),
postTagLimit: normalizePostTagLimit(source.postTagLimit),
homeLatestPostLimit: normalizeDisplayCount(source.homeLatestPostLimit, DEFAULT_HOME_LATEST_POST_LIMIT),
postListPageSize: normalizeDisplayCount(source.postListPageSize, DEFAULT_POST_LIST_PAGE_SIZE),
tagPostListPageSize: normalizeDisplayCount(source.tagPostListPageSize, DEFAULT_TAG_POST_LIST_PAGE_SIZE),
featuredPostLimit: normalizeDisplayCount(source.featuredPostLimit, DEFAULT_FEATURED_POST_LIMIT),
pageSectionItemLimit: normalizeDisplayCount(source.pageSectionItemLimit, DEFAULT_PAGE_SECTION_ITEM_LIMIT),
uploadMaxFileCount: normalizeUploadMaxFileCount(source.uploadMaxFileCount),
uploadMaxTotalSize: normalizeUploadMaxTotalSize(source.uploadMaxTotalSize)
})
/**
* POST 설정 필드만 대상 객체에 복사한다.
* @param {Object} target - 값을 받을 객체
* @param {Object} source - 값을 제공할 객체
* @returns {void}
*/
export const assignPostSettingsSection = (target, source = {}) => {
const normalized = normalizePostSettingsSection(source)
POST_SETTINGS_FIELDS.forEach((field) => {
target[field] = normalized[field]
})
}
/**
* 두 POST 설정 값이 다른지 확인한다.
* @param {Object} current - 현재 설정 값
* @param {Object} snapshot - 비교할 스냅샷
* @returns {boolean} 변경 여부
*/
export const hasPostSettingsSectionChanges = (current, snapshot) => {
const normalizedCurrent = normalizePostSettingsSection(current)
const normalizedSnapshot = normalizePostSettingsSection(snapshot)
return POST_SETTINGS_FIELDS.some((field) => normalizedCurrent[field] !== normalizedSnapshot[field])
}