diff --git a/Web/Controller/LandingPages.hs b/Web/Controller/LandingPages.hs index 5ae740f..f642ecc 100644 --- a/Web/Controller/LandingPages.hs +++ b/Web/Controller/LandingPages.hs @@ -4,6 +4,7 @@ import Web.Controller.Prelude import Web.View.LandingPages.Index import Web.View.LandingPages.New import Web.View.LandingPages.Edit +import Web.View.LandingPages.Order import Web.View.LandingPages.Show instance Controller LandingPagesController where @@ -51,42 +52,6 @@ instance Controller LandingPagesController where Right landingPage -> do landingPage <- landingPage |> updateRecord - -- After we update the Landing page, we can set the order of the paragraphs. - let params = paramListOrNothing @UUID "paragraphId" - - case catMaybes params of - [] -> do - -- No paragraphs to update. - pure () - - uuids -> do - -- We need to update the weight of the paragraphs, - -- So load them. - landingPageWithRecords <- fetchLandingPageWithRecords landingPageId - - -- Iterate over all paragraphs, and update the weight. - forEach landingPageWithRecords.paragraphCtas updateParagraph - forEach landingPageWithRecords.paragraphQuotes updateParagraph - - where - updateParagraph :: forall record. - ( HasField "id" record (Id record) - , SetField "weight" record Int - , CanUpdate record - , PrimaryKey (GetTableName record) ~ UUID - , ?modelContext :: ModelContext - ) => record -> IO () - updateParagraph paragraph = do - let uuid :: UUID = unpackId paragraph.id - let weight = elemIndex uuid uuids |> fromMaybe 0 - - paragraph - |> set #weight weight - |> updateRecord - - - pure () - setSuccessMessage "LandingPage updated" setFormStatus FormStatusSuccess redirectTo EditLandingPageAction { .. } @@ -112,6 +77,51 @@ instance Controller LandingPagesController where setSuccessMessage "LandingPage deleted" redirectTo LandingPagesAction + action ShowOrderLandingPageParagraphsAction { .. } = do + landingPageWithRecords <- fetchLandingPageWithRecords landingPageId + + render OrderParagraphsView { .. } + + action UpdateOrderLandingPageParagraphsAction { .. } = do + landingPageWithRecords <- fetchLandingPageWithRecords landingPageId + + let params = paramListOrNothing @UUID "paragraphId" + + case catMaybes params of + [] -> do + -- No paragraphs to update. + pure () + + uuids -> do + -- We need to update the weight of the paragraphs, + -- So load them. + landingPageWithRecords <- fetchLandingPageWithRecords landingPageId + + -- Iterate over all paragraphs, and update the weight. + forEach landingPageWithRecords.paragraphCtas updateParagraph + forEach landingPageWithRecords.paragraphQuotes updateParagraph + + setSuccessMessage "Paragraphs updated" + redirectTo ShowOrderLandingPageParagraphsAction { .. } + + where + updateParagraph :: forall record. + ( HasField "id" record (Id record) + , SetField "weight" record Int + , CanUpdate record + , PrimaryKey (GetTableName record) ~ UUID + , ?modelContext :: ModelContext + ) => record -> IO () + updateParagraph paragraph = do + let uuid :: UUID = unpackId paragraph.id + let weight = elemIndex uuid uuids |> fromMaybe 0 + + paragraph + |> set #weight weight + |> updateRecord + + pure () + buildLandingPage landingPage = landingPage |> fill @'["title"] |> validateField #title nonEmpty diff --git a/Web/Element/SubmitButton.hs b/Web/Element/SubmitButton.hs index 584fd9e..ce93aea 100644 --- a/Web/Element/SubmitButton.hs +++ b/Web/Element/SubmitButton.hs @@ -6,8 +6,8 @@ import Application.Helper.Icons import Web.Element.ElementWrap import Web.Element.Types -renderSubmitButtonwithFormStatus :: SubmitButton -> FormStatus -> Html -renderSubmitButtonwithFormStatus submitButton formStatus = [hsx| +renderSubmitButtonWithFormStatus :: SubmitButton -> FormStatus -> Html +renderSubmitButtonWithFormStatus submitButton formStatus = [hsx| {submitButton} {- We show only one of these messages -} diff --git a/Web/Types.hs b/Web/Types.hs index 5878b64..8881b2b 100644 --- a/Web/Types.hs +++ b/Web/Types.hs @@ -46,6 +46,9 @@ data LandingPagesController | EditLandingPageAction { landingPageId :: !(Id LandingPage) } | UpdateLandingPageAction { landingPageId :: !(Id LandingPage) } | DeleteLandingPageAction { landingPageId :: !(Id LandingPage) } + -- Order of paragraphs, and update of it. + | ShowOrderLandingPageParagraphsAction { landingPageId :: !(Id LandingPage) } + | UpdateOrderLandingPageParagraphsAction { landingPageId :: !(Id LandingPage) } deriving (Eq, Show, Data) data ParagraphCtasController @@ -80,6 +83,7 @@ data SessionsController | CreateSessionAction | DeleteSessionAction deriving (Eq, Show, Data) + data UsersController = UsersAction | NewUserAction diff --git a/Web/View/LandingPages/Edit.hs b/Web/View/LandingPages/Edit.hs index 45d1e4f..e461c89 100644 --- a/Web/View/LandingPages/Edit.hs +++ b/Web/View/LandingPages/Edit.hs @@ -16,15 +16,13 @@ data EditView = EditView instance View EditView where html EditView { .. } = [ header - , renderForm landingPage paragraphCtas paragraphQuotes formStatus + , renderForm landingPageWithRecords formStatus ] |> mconcat |> wrapVerticalSpacing AlignNone |> wrapContainerWide where landingPage = landingPageWithRecords.landingPage - paragraphCtas = landingPageWithRecords.paragraphCtas - paragraphQuotes = landingPageWithRecords.paragraphQuotes breadcrumb = renderBreadcrumb [ breadcrumbLink "Landing Pages" LandingPagesAction @@ -46,36 +44,33 @@ instance View EditView where |> mconcat |> wrapHorizontalSpacingTiny AlignBaseline -renderForm :: LandingPage -> [ParagraphCta] -> [ParagraphQuote] -> FormStatus -> Html -renderForm landingPage paragraphCtas paragraphQuotes formStatus = formFor landingPage body +renderForm :: LandingPageWithRecords -> FormStatus -> Html +renderForm landingPageWithRecords formStatus = formFor landingPage body where - body :: (?formContext :: FormContext LandingPage) => Html - body = [hsx| - {(textField #title)} + landingPage = landingPageWithRecords.landingPage + paragraphCtas = landingPageWithRecords.paragraphCtas + paragraphQuotes = landingPageWithRecords.paragraphQuotes -