From a72825894f2b729faa2267d6d43d3bd8330b2c6d Mon Sep 17 00:00:00 2001 From: seaerchin <44049504+seaerchin@users.noreply.github.com> Date: Wed, 6 Sep 2023 16:47:44 +0800 Subject: [PATCH 01/12] fix(homepage): placeholder inputs (#1451) * fix(homepage): fix typing * refactor(homepagepreview): merge with default values * refactor(edithomepage): empty sections on create * refactor(preview): give default values * fix(validators): fix wrong condition * refactor(validators): add simple validators that dun do extra work * refactor(hoomepage): clarify types * feat(utils): add method to compute error from state * refactor(homepage): add onblur * refactor(edithomepage): use new func to disable button * refactor(utils): rename * fix(validators): add = * refactor(edithomepaeg): remove extra prop * refactor(templates/hompage): updated props * refactor(utils): remove extra valueof * refactor(utils): refactor to return error messages --------- Co-authored-by: seaerchin Co-authored-by: seaerchin --- src/layouts/EditHomepage/EditHomepage.jsx | 76 ++-- src/layouts/EditHomepage/HomepagePreview.tsx | 34 +- src/layouts/EditHomepage/constants.ts | 1 + src/layouts/EditHomepage/utils.ts | 73 +++- .../Homepage/HeroDropdownSection.tsx | 3 + .../Homepage/HeroHighlightSection.tsx | 5 + .../components/Homepage/InfobarBody.tsx | 5 + .../components/Homepage/InfopicBody.tsx | 8 +- .../components/Homepage/ResourcesBody.tsx | 3 + src/templates/homepage/HeroSection.jsx | 79 ++-- src/templates/homepage/InfobarSection.jsx | 6 - src/templates/homepage/InfopicLeftSection.jsx | 20 +- .../homepage/InfopicRightSection.jsx | 20 +- src/types/homepage.ts | 32 +- src/utils/validators.js | 338 ++++++++---------- 15 files changed, 374 insertions(+), 329 deletions(-) diff --git a/src/layouts/EditHomepage/EditHomepage.jsx b/src/layouts/EditHomepage/EditHomepage.jsx index 27abc341e..e45ace788 100644 --- a/src/layouts/EditHomepage/EditHomepage.jsx +++ b/src/layouts/EditHomepage/EditHomepage.jsx @@ -56,7 +56,7 @@ import { RESOURCES_SECTION, } from "./constants" import { HomepagePreview } from "./HomepagePreview" -import { getErrorValues } from "./utils" +import { getDefaultValues, getErrorsFromHomepageState } from "./utils" /* eslint-disable react/no-array-index-key */ @@ -78,51 +78,23 @@ const getHasError = (errorArray) => _.some(err, (errorMessage) => errorMessage.length > 0) ) -const getHasErrors = (errors) => { - const hasSectionErrors = _.some(errors.sections, (section) => { - // Section is an object, e.g. { hero: {} } - // _.keys(section) produces an array with length 1 - // The 0th element of the array contains the sectionType - const sectionType = _.keys(section)[0] - return ( - _.some( - section[sectionType], - (errorMessage) => errorMessage.length > 0 - ) === true - ) - }) - - const hasHighlightErrors = getHasError(errors.highlights) - const hasDropdownElemErrors = getHasError(errors.dropdownElems) - - return hasSectionErrors || hasHighlightErrors || hasDropdownElemErrors -} - // Constants // Section constructors // TODO: Export all these as const and write wrapper for error... -const enumSection = (type, isError) => { +const enumSection = (type) => { switch (type) { case "resources": - return isError - ? { resources: getErrorValues(RESOURCES_SECTION) } - : { resources: RESOURCES_SECTION } + return { resources: getDefaultValues(RESOURCES_SECTION) } case "infobar": - return isError - ? { infobar: getErrorValues(INFOBAR_SECTION) } - : { infobar: INFOBAR_SECTION } + return { infobar: getDefaultValues(INFOBAR_SECTION) } case "infopic": - return isError - ? { infopic: getErrorValues(INFOPIC_SECTION) } - : { infopic: INFOPIC_SECTION } + return { infopic: getDefaultValues(INFOPIC_SECTION) } default: - return isError - ? { infobar: getErrorValues(INFOBAR_SECTION) } - : { infobar: INFOBAR_SECTION } + return { infobar: getDefaultValues(INFOBAR_SECTION) } } } @@ -188,6 +160,7 @@ const EditHomepage = ({ match }) => { setDisplayHighlights(displayHighlights) } const heroSection = frontMatter.sections.filter((section) => !!section.hero) + const errorMessages = getErrorsFromHomepageState(homepageState) const errorToast = useErrorToast() @@ -238,14 +211,14 @@ const EditHomepage = ({ match }) => { ) // Fill in dropdown elem errors array dropdownElemsErrors = _.map(dropdown.options, () => - getErrorValues(DROPDOWN_ELEMENT_SECTION) + getDefaultValues(DROPDOWN_ELEMENT_SECTION) ) } if (keyHighlights) { displayHighlights = _.fill(Array(keyHighlights.length), false) // Fill in highlights errors array highlightsErrors = _.map(keyHighlights, () => - getErrorValues(KEY_HIGHLIGHT_SECTION) + getDefaultValues(KEY_HIGHLIGHT_SECTION) ) } // Fill in sectionErrors for hero @@ -255,16 +228,16 @@ const EditHomepage = ({ match }) => { // Check if there is already a resources section if (section.resources) { sectionsErrors.push({ - resources: getErrorValues(RESOURCES_SECTION), + resources: getDefaultValues(RESOURCES_SECTION), }) } if (section.infobar) { - sectionsErrors.push({ infobar: getErrorValues(INFOBAR_SECTION) }) + sectionsErrors.push({ infobar: getDefaultValues(INFOBAR_SECTION) }) } if (section.infopic) { - sectionsErrors.push({ infopic: getErrorValues(INFOPIC_SECTION) }) + sectionsErrors.push({ infopic: getDefaultValues(INFOPIC_SECTION) }) } // Minimize all sections by default @@ -536,8 +509,8 @@ const EditHomepage = ({ match }) => { switch (elemType) { case "section": { - const val = enumSection(value, false) - const err = enumSection(value, true) + const val = enumSection(value) + const err = enumSection(value) const newScrollRefs = update(scrollRefs, { $push: [createRef()] }) @@ -553,8 +526,8 @@ const EditHomepage = ({ match }) => { break } case "dropdownelem": { - const val = DROPDOWN_ELEMENT_SECTION - const err = getErrorValues(DROPDOWN_ELEMENT_SECTION) + const val = getDefaultValues(DROPDOWN_ELEMENT_SECTION) + const err = getDefaultValues(DROPDOWN_ELEMENT_SECTION) const updatedHomepageState = onCreate( homepageState, @@ -569,8 +542,9 @@ const EditHomepage = ({ match }) => { case "highlight": { // depends on index to generate // If key highlights section exists - const val = KEY_HIGHLIGHT_SECTION - const err = getErrorValues(KEY_HIGHLIGHT_SECTION) + const val = getDefaultValues(KEY_HIGHLIGHT_SECTION) + const err = getDefaultValues(KEY_HIGHLIGHT_SECTION) + const updatedHomepageState = onCreate( homepageState, elemType, @@ -1119,8 +1093,18 @@ const EditHomepage = ({ match }) => { />