Skip to content

Commit

Permalink
fix: update config settings and footer settings separately (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
gweiying authored Dec 4, 2020
1 parent 530c64f commit d03d29b
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions classes/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,27 @@ class Settings {
const footerContent = footer.content
const navigationContent = navigation.content

const settingsObj = {
config: {
const settingsObj = {}

if (configSettings) {
settingsObj.config = {
payload: configSettings,
currentData: configContent,
},
footer: {
}
}

if (footerSettings) {
settingsObj.footer = {
payload: footerSettings,
currentData: footerContent,
},
navigation: {
}
}

if (navigationSettings) {
settingsObj.navigation = {
payload: navigationSettings,
currentData: navigationContent,
},
}
}

const updatedSettingsObjArr = Object.keys(settingsObj).map((settingsObjKey) => {
Expand All @@ -168,27 +176,35 @@ class Settings {

const { configSettingsObj, footerSettingsObj, navigationSettingsObj } = updatedSettingsObj

// update files
const newConfigContent = Base64.encode(yaml.safeDump(configSettingsObj))
const newFooterContent = Base64.encode(yaml.safeDump(footerSettingsObj))
const newNavigationContent = Base64.encode(yaml.safeDump(navigationSettingsObj))

// To-do: use Git Tree to speed up operations
await configResp.update(newConfigContent, config.sha)
await FooterFile.update(FOOTER_PATH, newFooterContent, footer.sha)
await NavigationFile.update(NAVIGATION_PATH, newNavigationContent, navigation.sha)
if (configSettings) {
const newConfigContent = Base64.encode(yaml.safeDump(configSettingsObj))
await configResp.update(newConfigContent, config.sha)

// Update title in homepage as well if it's changed
if (configContent.title !== configSettingsObj.title) {
const { content: homepageContentObj, sha } = homepage;

homepageContentObj.title = configSettings.title;
const homepageFrontMatter = yaml.safeDump(homepageContentObj);

// Update title in homepage as well if it's changed
if (configContent.title !== updatedSettingsObjArr[0].title) {
const { content: homepageContentObj, sha } = homepage;
homepageContentObj.title = configSettings.title;
const homepageFrontMatter = yaml.safeDump(homepageContentObj);
const homepageContent = ['---\n', homepageFrontMatter, '---'].join('') ;
const newHomepageContent = Base64.encode(homepageContent)

const homepageContent = ['---\n', homepageFrontMatter, '---'].join('') ;
const newHomepageContent = Base64.encode(homepageContent)
await HomepageFile.update(HOMEPAGE_INDEX_PATH, newHomepageContent, sha)
}
}

if (footerSettings) {
const newFooterContent = Base64.encode(yaml.safeDump(footerSettingsObj))
await FooterFile.update(FOOTER_PATH, newFooterContent, footer.sha)
}

await HomepageFile.update(HOMEPAGE_INDEX_PATH, newHomepageContent, sha)
if (navigationSettings) {
const newNavigationContent = Base64.encode(yaml.safeDump(navigationSettingsObj))
await NavigationFile.update(NAVIGATION_PATH, newNavigationContent, navigation.sha)
}

return
}
}
Expand Down

0 comments on commit d03d29b

Please sign in to comment.