Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hero Image #51

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
6 changes: 3 additions & 3 deletions Application/Helper/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ fetchLandingPageWithRecords landingPageId = do

paragraphCtas <- fetch landingPage.paragraphCtasRefLandingPages

paragraphQuotes <- fetch landingPage.paragraphQuotes

paragraphHeroImages <- fetch landingPage.paragraphHeroImages
andreytroeglazov marked this conversation as resolved.
Show resolved Hide resolved

paragraphQuotes <- fetch landingPage.paragraphQuotes

return $ LandingPageWithRecords { .. }

getParagraphsCount :: (?modelContext::ModelContext) => Id LandingPage -> IO Int
getParagraphsCount landingPageId = do
landingPageWithRecords <- fetchLandingPageWithRecords landingPageId

pure $ length landingPageWithRecords.paragraphCtas
+ length landingPageWithRecords.paragraphQuotes
+ length landingPageWithRecords.paragraphHeroImages
andreytroeglazov marked this conversation as resolved.
Show resolved Hide resolved
+ length landingPageWithRecords.paragraphQuotes
+ 1

-- | The RSA public key, can be used to verify image style URLs that were signed.
Expand Down
67 changes: 30 additions & 37 deletions Web/Controller/ParagraphHeroImages.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,10 @@ instance Controller ParagraphHeroImagesController where
render EditView { .. }

action UpdateParagraphHeroImageAction { paragraphHeroImageId } = do
let uploadImage = uploadToStorageWithOptions $ def
{ preprocess = applyImageMagick "jpg" ["-resize", "1024x1024^", "-gravity", "north", "-extent", "1024x1024", "-quality", "85%", "-strip"] }

formStatus <- getAndClearFormStatus

paragraphHeroImage <- fetch paragraphHeroImageId
paragraphHeroImage
|> uploadImage #imageUrl
>>= buildParagraphHeroImage
>>= ifValid \case
Left paragraphHeroImage -> do
setFormStatus FormStatusError
render EditView { .. }
Right paragraphHeroImage -> do
paragraphHeroImage <- paragraphHeroImage |> updateRecord
setSuccessMessage "Hero Image updated"
-- We don't setFormStatus, since we redirect to a new page.
redirectTo EditLandingPageAction { landingPageId = paragraphHeroImage.landingPageId }
createOrUpdateParagraphHeroImageAction (Just paragraphHeroImageId)

action CreateParagraphHeroImageAction = do
let uploadImage = uploadToStorageWithOptions $ def
{ preprocess = applyImageMagick "jpg" ["-resize", "1024x1024^", "-gravity", "north", "-extent", "1024x1024", "-quality", "85%", "-strip"] }

let paragraphHeroImage = newRecord @ParagraphHeroImage
let formStatus = FormStatusNotSubmitted

paragraphHeroImage
|> uploadImage #imageUrl
>>= buildParagraphHeroImage
>>= ifValid \case
Left paragraphHeroImage -> do
setFormStatus FormStatusError
render NewView { .. }
Right paragraphHeroImage -> do
paragraphHeroImage <- paragraphHeroImage |> createRecord
setSuccessMessage "Hero Image created"
-- We don't setFormStatus, since we redirect to a new page.
redirectTo EditLandingPageAction { landingPageId = paragraphHeroImage.landingPageId }
createOrUpdateParagraphHeroImageAction Nothing

action DeleteParagraphHeroImageAction { paragraphHeroImageId } = do
paragraphHeroImage <- fetch paragraphHeroImageId
Expand All @@ -71,5 +37,32 @@ buildParagraphHeroImage paragraphHeroImage = paragraphHeroImage
|> fill @["landingPageId", "weight", "title", "subtitle", "link"]
|> validateField #title nonEmpty
|> validateField #imageUrl nonEmpty
|> return
|> pure

createOrUpdateParagraphHeroImageAction :: (?modelContext :: ModelContext, ?context :: ControllerContext) => Maybe (Id ParagraphHeroImage) -> IO ()
andreytroeglazov marked this conversation as resolved.
Show resolved Hide resolved
createOrUpdateParagraphHeroImageAction maybeParagraphHeroImageId = do
let uploadImage = uploadToStorageWithOptions $ def
{ preprocess = applyImageMagick "jpg" ["-resize", "1024x1024^", "-gravity", "north", "-extent", "1024x1024", "-quality", "85%", "-strip"] }

formStatus <- getAndClearFormStatus

paragraphHeroImage <- case maybeParagraphHeroImageId of
Just id -> fetch id
Nothing -> pure $ newRecord @ParagraphHeroImage

paragraphHeroImage
|> uploadImage #imageUrl
>>= buildParagraphHeroImage
>>= ifValid \case
Left paragraphHeroImage -> do
setFormStatus FormStatusError
if isJust maybeParagraphHeroImageId
then render EditView { .. }
else render NewView { .. }
Right paragraphHeroImage -> do
paragraphHeroImage <- case maybeParagraphHeroImageId of
Just _ -> paragraphHeroImage |> updateRecord
Nothing -> paragraphHeroImage |> createRecord
setSuccessMessage "Hero Image saved"
-- We don't setFormStatus, since we redirect to a new page.
redirectTo EditLandingPageAction { landingPageId = paragraphHeroImage.landingPageId }
3 changes: 2 additions & 1 deletion Web/Element/HeroImage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ render record@RenderHeroImage {title, subtitle, button, imageUrl} =
++ subTitleWrapped
++ buttonHtml
|> wrapVerticalSpacingBig AlignNone
-- 1536x466 - size of the image.
andreytroeglazov marked this conversation as resolved.
Show resolved Hide resolved
|> renderImageAndContent (pathTo $ RenderImageStyleAction 1536 466 imageUrl signed)
andreytroeglazov marked this conversation as resolved.
Show resolved Hide resolved
where
-- Sign the image URL to prevent tampering.
Expand All @@ -29,7 +30,7 @@ render record@RenderHeroImage {title, subtitle, button, imageUrl} =

buttonHtml = case button of
Just btn -> Web.Element.Button.render btn
Nothing -> mempty
Nothing -> ""


renderImageAndContent :: Text -> Html -> Html
Expand Down
2 changes: 1 addition & 1 deletion Web/View/ParagraphHeroImages/Edit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ instance View EditView where
html EditView { .. } = [hsx|
{breadcrumb}
<h1>Edit Paragraph Hero Image</h1>
{renderForm paragraphHeroImage True formStatus}
{renderForm paragraphHeroImage False formStatus}
|]
where
breadcrumb = renderBreadcrumb
Expand Down
Loading