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

Posts & Pages: Allow setting a posts page back to a regular page #21963

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions WordPress/Classes/Utility/Analytics/WPAnalyticsEvent.swift
Original file line number Diff line number Diff line change
@@ -258,6 +258,7 @@ import Foundation
case postListCommentsAction
case postListSetAsPostsPageAction
case postListSetHomePageAction
case postListSetAsRegularPageAction

// Page List
case pageListEditHomepageTapped
@@ -967,6 +968,8 @@ import Foundation
return "post_list_button_pressed"
case .postListSetHomePageAction:
return "post_list_button_pressed"
case .postListSetAsRegularPageAction:
return "post_list_button_pressed"

// Page List
case .pageListEditHomepageTapped:
@@ -1424,6 +1427,8 @@ import Foundation
return ["button": "set_posts_page"]
case .postListSetHomePageAction:
return ["button": "set_homepage"]
case .postListSetAsRegularPageAction:
return ["button": "set_regular_page"]
default:
return nil
}
Original file line number Diff line number Diff line change
@@ -66,12 +66,20 @@ extension PageListViewController: InteractivePostViewDelegate {

func setHomepage(for apost: AbstractPost) {
guard let page = apost as? Page else { return }
WPAnalytics.track(.postListSetAsPostsPageAction)
setPageAsHomepage(page)
}

func setPostsPage(for apost: AbstractPost) {
guard let page = apost as? Page else { return }
setPageAsPostsPage(page)
WPAnalytics.track(.postListSetHomePageAction)
togglePageAsPostsPage(page)
}

func setRegularPage(for apost: AbstractPost) {
guard let page = apost as? Page else { return }
WPAnalytics.track(.postListSetAsRegularPageAction)
togglePageAsPostsPage(page)
}

// MARK: - Helpers
23 changes: 11 additions & 12 deletions WordPress/Classes/ViewRelated/Pages/PageListViewController.swift
Original file line number Diff line number Diff line change
@@ -431,11 +431,9 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
}

func setPageAsHomepage(_ page: Page) {
guard let pageID = page.postID?.intValue else { return }

guard let homePageID = page.postID?.intValue else { return }
beginRefreshingManually()
WPAnalytics.track(.postListSetHomePageAction)
homepageSettingsService?.setHomepageType(.page, homePageID: pageID, success: { [weak self] in
homepageSettingsService?.setHomepageType(.page, homePageID: homePageID, success: { [weak self] in
self?.refreshAndReload()
self?.handleHomepageSettingsSuccess()
}, failure: { [weak self] error in
@@ -444,14 +442,13 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
})
}

func setPageAsPostsPage(_ page: Page) {
guard let pageID = page.postID?.intValue else { return }

func togglePageAsPostsPage(_ page: Page) {
let newValue = !page.isSitePostsPage
let postsPageID = page.isSitePostsPage ? 0 : (page.postID?.intValue ?? 0)
beginRefreshingManually()
WPAnalytics.track(.postListSetAsPostsPageAction)
homepageSettingsService?.setHomepageType(.page, withPostsPageID: pageID, success: { [weak self] in
homepageSettingsService?.setHomepageType(.page, withPostsPageID: postsPageID, success: { [weak self] in
self?.refreshAndReload()
self?.handleHomepagePostsPageSettingsSuccess()
self?.handleHomepagePostsPageSettingsSuccess(isPostsPage: newValue)
}, failure: { [weak self] error in
self?.refreshControl.endRefreshing()
self?.handleHomepageSettingsFailure()
@@ -463,8 +460,9 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
ActionDispatcher.global.dispatch(NoticeAction.post(notice))
}

private func handleHomepagePostsPageSettingsSuccess() {
let notice = Notice(title: HomepageSettingsText.updatePostsPageSuccessTitle, feedbackType: .success)
private func handleHomepagePostsPageSettingsSuccess(isPostsPage: Bool) {
let title = isPostsPage ? HomepageSettingsText.updatePostsPageSuccessTitle : HomepageSettingsText.updatePageSuccessTitle
let notice = Notice(title: title, feedbackType: .success)
ActionDispatcher.global.dispatch(NoticeAction.post(notice))
}

@@ -483,6 +481,7 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
struct HomepageSettingsText {
static let updateErrorTitle = NSLocalizedString("Unable to update homepage settings", comment: "Error informing the user that their homepage settings could not be updated")
static let updateErrorMessage = NSLocalizedString("Please try again later.", comment: "Prompt for the user to retry a failed action again later")
static let updatePageSuccessTitle = NSLocalizedString("pages.updatePage.successTitle", value: "Page successfully updated", comment: "Message informing the user that their static homepage page was set successfully")
static let updateHomepageSuccessTitle = NSLocalizedString("Homepage successfully updated", comment: "Message informing the user that their static homepage page was set successfully")
static let updatePostsPageSuccessTitle = NSLocalizedString("Posts page successfully updated", comment: "Message informing the user that their static homepage for posts was set successfully")
}
Original file line number Diff line number Diff line change
@@ -106,10 +106,9 @@ final class PageMenuViewModelTests: CoreDataTestCase {
.map { $0.buttons }
let expectedButtons: [[AbstractPostButton]] = [
[.view],
[.moveToDraft, .duplicate],
[.duplicate],
[.blaze],
[.setParent, .setPostsPage],
[.trash]
[.setParent, .setPostsPage]
]
expect(buttons).to(equal(expectedButtons))
}
@@ -136,7 +135,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
[.view],
[.moveToDraft, .duplicate],
[.blaze],
[.setParent, .setHomepage],
[.setParent, .setHomepage, .setRegularPage],
[.trash]
]
expect(buttons).to(equal(expectedButtons))
Original file line number Diff line number Diff line change
@@ -94,6 +94,7 @@ extension AbstractPostButton: AbstractPostMenuAction {
case .setParent: return UIImage(systemName: "text.append")
case .setHomepage: return UIImage(systemName: "house")
case .setPostsPage: return UIImage(systemName: "text.word.spacing")
case .setRegularPage: return UIImage(systemName: "arrow.uturn.backward")
case .pageAttributes: return UIImage(systemName: "doc")
}
}
@@ -123,6 +124,7 @@ extension AbstractPostButton: AbstractPostMenuAction {
case .setParent: return Strings.setParent
case .setHomepage: return Strings.setHomepage
case .setPostsPage: return Strings.setPostsPage
case .setRegularPage: return Strings.setRegularPage
case .pageAttributes: return Strings.pageAttributes
}
}
@@ -157,6 +159,8 @@ extension AbstractPostButton: AbstractPostMenuAction {
delegate.setHomepage(for: post)
case .setPostsPage:
delegate.setPostsPage(for: post)
case .setRegularPage:
delegate.setRegularPage(for: post)
case .pageAttributes:
break
}
@@ -178,6 +182,7 @@ extension AbstractPostButton: AbstractPostMenuAction {
static let setParent = NSLocalizedString("posts.setParent.actionTitle", value: "Set parent", comment: "Set the parent page for the selected page.")
static let setHomepage = NSLocalizedString("posts.setHomepage.actionTitle", value: "Set as homepage", comment: "Set the selected page as the homepage.")
static let setPostsPage = NSLocalizedString("posts.setPostsPage.actionTitle", value: "Set as posts page", comment: "Set the selected page as a posts page.")
static let setRegularPage = NSLocalizedString("posts.setRegularPage.actionTitle", value: "Set as regular page", comment: "Set the selected page as a regular page.")
static let pageAttributes = NSLocalizedString("posts.pageAttributes.actionTitle", value: "Page attributes", comment: "Opens a submenu for page attributes.")
}
}
Original file line number Diff line number Diff line change
@@ -32,4 +32,5 @@ enum AbstractPostButton: Equatable {
case setParent
case setHomepage
case setPostsPage
case setRegularPage
}
Original file line number Diff line number Diff line change
@@ -16,12 +16,14 @@ protocol InteractivePostViewDelegate: AnyObject {
func setParent(for post: AbstractPost)
func setHomepage(for post: AbstractPost)
func setPostsPage(for post: AbstractPost)
func setRegularPage(for post: AbstractPost)
}

extension InteractivePostViewDelegate {
func setParent(for post: AbstractPost) {}
func setHomepage(for post: AbstractPost) {}
func setPostsPage(for post: AbstractPost) {}
func setRegularPage(for post: AbstractPost) {}

func trash(_ post: AbstractPost) {
self.trash(post, completion: {})
16 changes: 13 additions & 3 deletions WordPress/Classes/ViewRelated/Post/PageMenuViewModel.swift
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ final class PageMenuViewModel: AbstractPostMenuViewModel {
private func createSecondarySection() -> AbstractPostButtonSection {
var buttons = [AbstractPostButton]()

if page.status != .draft {
if page.status != .draft && !isSiteHomepage {
buttons.append(.moveToDraft)
}

@@ -88,18 +88,28 @@ final class PageMenuViewModel: AbstractPostMenuViewModel {

buttons.append(.setParent)

if page.status == .publish, !isSiteHomepage {
guard page.status == .publish else {
return AbstractPostButtonSection(buttons: buttons)
}

if !isSiteHomepage {
buttons.append(.setHomepage)
}

if page.status == .publish, !isSitePostsPage {
if !isSitePostsPage {
buttons.append(.setPostsPage)
} else {
buttons.append(.setRegularPage)
}

return AbstractPostButtonSection(buttons: buttons, submenuButton: .pageAttributes)
}

private func createTrashSection() -> AbstractPostButtonSection {
guard !isSiteHomepage else {
return AbstractPostButtonSection(buttons: [])
}

return AbstractPostButtonSection(buttons: [.trash])
}
}