Skip to content

Commit 4c86883

Browse files
oguzkocerjkmassel
andauthored
Standalone /navigation implementation (#946)
* Update navigation types * Use navigations specific endpoint in navigation tests * Fix NavigationWithEditContext title --------- Co-authored-by: Jeremy Massel <[email protected]>
1 parent 15bd5d6 commit 4c86883

File tree

6 files changed

+194
-241
lines changed

6 files changed

+194
-241
lines changed

native/swift/Example/Example/ListViewData.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,7 @@ extension PostStatusWithEditContext: ListViewDataConvertable {
185185

186186
extension NavigationWithEditContext: ListViewDataConvertable {
187187
var asListViewData: ListViewData {
188-
let title = switch self.title {
189-
case .object(let title): title.rendered
190-
case .string(let string): string
191-
}
192-
193-
return ListViewData(id: self.id.description, title: title ?? "Unknown", subtitle: self.slug, fields: [:])
188+
return ListViewData(id: self.id.description, title: self.title.rendered, subtitle: self.slug, fields: [:])
194189
}
195190
}
196191

wp_api/src/navigations.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct SparseNavigation {
4949
#[WpContext(edit, view)]
5050
pub date_gmt: Option<String>,
5151
#[WpContext(edit, view)]
52+
#[WpContextualField]
5253
pub guid: Option<SparseNavigationGuid>,
5354
#[WpContext(edit, embed, view)]
5455
pub id: Option<NavigationId>,
@@ -69,46 +70,47 @@ pub struct SparseNavigation {
6970
#[WpContextualOption]
7071
pub password: Option<String>,
7172
#[WpContext(edit, embed, view)]
72-
pub title: Option<SparseNavigationTitleWrapper>,
73+
#[WpContextualField]
74+
pub title: Option<SparseNavigationTitle>,
7375
#[WpContext(edit, embed, view)]
74-
pub content: Option<SparseNavigationContentWrapper>,
76+
#[WpContextualField]
77+
pub content: Option<SparseNavigationContent>,
7578
#[WpContext(edit, view)]
7679
#[WpContextualOption]
7780
pub template: Option<String>,
7881
// _meta field omitted
7982
}
8083

81-
#[derive(Debug, Serialize, PartialEq, PartialOrd, Eq, wp_derive::WpDeserialize, uniffi::Record)]
84+
#[derive(Debug, Serialize, Deserialize, WpContextual, uniffi::Record)]
8285
pub struct SparseNavigationGuid {
83-
pub rendered: Option<String>,
86+
#[WpContext(edit)]
87+
#[WpContextualOption]
8488
pub raw: Option<String>,
89+
#[WpContext(edit, view)]
90+
pub rendered: Option<String>,
8591
}
8692

87-
#[derive(Debug, Serialize, PartialEq, PartialOrd, Eq, Deserialize, uniffi::Enum)]
88-
#[serde(untagged)]
89-
pub enum SparseNavigationTitleWrapper {
90-
Object(SparseNavigationTitle),
91-
String(String),
92-
}
93-
94-
#[derive(Debug, Serialize, PartialEq, PartialOrd, Eq, wp_derive::WpDeserialize, uniffi::Record)]
93+
#[derive(Debug, Serialize, Deserialize, WpContextual, uniffi::Record)]
9594
pub struct SparseNavigationTitle {
95+
#[WpContext(edit)]
96+
#[WpContextualOption]
9697
pub raw: Option<String>,
98+
#[WpContext(edit, embed, view)]
9799
pub rendered: Option<String>,
98100
}
99101

100-
#[derive(Debug, Serialize, PartialEq, PartialOrd, Eq, Deserialize, uniffi::Enum)]
101-
#[serde(untagged)]
102-
pub enum SparseNavigationContentWrapper {
103-
Object(SparseNavigationContent),
104-
String(String),
105-
}
106-
107-
#[derive(Debug, Serialize, PartialEq, PartialOrd, Eq, wp_derive::WpDeserialize, uniffi::Record)]
102+
#[derive(Debug, Serialize, Deserialize, WpContextual, uniffi::Record)]
108103
pub struct SparseNavigationContent {
104+
#[WpContext(edit)]
105+
#[WpContextualOption]
109106
pub raw: Option<String>,
107+
#[WpContext(edit, embed, view)]
110108
pub rendered: Option<String>,
109+
#[WpContext(edit, embed, view)]
110+
#[WpContextualOption]
111111
pub protected: Option<bool>,
112+
#[WpContext(edit, embed, view)]
113+
#[WpContextualOption]
112114
pub block_version: Option<u32>,
113115
}
114116

@@ -151,7 +153,7 @@ pub struct NavigationListParams {
151153
/// Sort collection by post attribute.
152154
#[uniffi(default = None)]
153155
#[field_name("orderby")]
154-
pub order_by: Option<NavigationOrderBy>,
156+
pub order_by: Option<WpApiParamNavigationsOrderBy>,
155157
/// Array of column names to be searched.
156158
#[uniffi(default = [])]
157159
pub search_columns: Vec<String>,
@@ -180,7 +182,7 @@ pub struct NavigationListParams {
180182
)]
181183
#[serde(rename_all = "snake_case")]
182184
#[strum(serialize_all = "snake_case")]
183-
pub enum NavigationOrderBy {
185+
pub enum WpApiParamNavigationsOrderBy {
184186
Author,
185187
#[default]
186188
Date,
@@ -194,7 +196,7 @@ pub enum NavigationOrderBy {
194196
Title,
195197
}
196198

197-
impl_as_query_value_from_to_string!(NavigationOrderBy);
199+
impl_as_query_value_from_to_string!(WpApiParamNavigationsOrderBy);
198200

199201
#[derive(Debug, Default, PartialEq, Eq, uniffi::Record, WpDeriveParamsField)]
200202
#[supports_pagination(false)]

wp_api/src/request/endpoint/posts_endpoint.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ enum PostsRequest {
3434
pub enum PostEndpointType {
3535
Posts,
3636
Pages,
37-
Navigation,
3837
#[strum(default)]
3938
Custom(String),
4039
}

wp_api_integration_tests/tests/test_navigations_err.rs

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
use wp_api::{
2-
posts::{
3-
PostCreateParams, PostId, PostListParams, PostRetrieveParams, PostUpdateParams,
4-
WpApiParamPostsOrderBy,
5-
},
6-
request::endpoint::posts_endpoint::PostEndpointType,
1+
use wp_api::navigations::{
2+
NavigationCreateParams, NavigationId, NavigationListParams, NavigationRetrieveParams,
3+
NavigationUpdateParams, WpApiParamNavigationsOrderBy,
74
};
85
use wp_api_integration_tests::prelude::*;
96

107
#[tokio::test]
118
#[parallel]
129
async fn create_navigation_err_cannot_create() {
1310
api_client_as_subscriber()
14-
.posts()
15-
.create(
16-
&PostEndpointType::Navigation,
17-
&PostCreateParams {
18-
..Default::default()
19-
},
20-
)
11+
.navigations()
12+
.create(&NavigationCreateParams {
13+
..Default::default()
14+
})
2115
.await
2216
.assert_wp_error(WpErrorCode::CannotCreate);
2317
}
@@ -26,14 +20,11 @@ async fn create_navigation_err_cannot_create() {
2620
#[parallel]
2721
async fn create_navigation_err_cannot_create2() {
2822
api_client_as_subscriber()
29-
.posts()
30-
.create(
31-
&PostEndpointType::Navigation,
32-
&PostCreateParams {
33-
title: Some("foo".to_string()),
34-
..Default::default()
35-
},
36-
)
23+
.navigations()
24+
.create(&NavigationCreateParams {
25+
title: Some("foo".to_string()),
26+
..Default::default()
27+
})
3728
.await
3829
.assert_wp_error(WpErrorCode::CannotCreate);
3930
}
@@ -42,11 +33,8 @@ async fn create_navigation_err_cannot_create2() {
4233
#[parallel]
4334
async fn delete_navigation_err_cannot_delete() {
4435
api_client_as_subscriber()
45-
.posts()
46-
.delete(
47-
&PostEndpointType::Navigation,
48-
&PostId(TestCredentials::instance().navigation_id),
49-
)
36+
.navigations()
37+
.delete(&NavigationId(TestCredentials::instance().navigation_id))
5038
.await
5139
.assert_wp_error(WpErrorCode::CannotDelete);
5240
}
@@ -55,14 +43,11 @@ async fn delete_navigation_err_cannot_delete() {
5543
#[parallel]
5644
async fn list_err_no_search_term_defined() {
5745
api_client()
58-
.posts()
59-
.list_with_edit_context(
60-
&PostEndpointType::Navigation,
61-
&PostListParams {
62-
orderby: Some(WpApiParamPostsOrderBy::Relevance),
63-
..Default::default()
64-
},
65-
)
46+
.navigations()
47+
.list_with_edit_context(&NavigationListParams {
48+
order_by: Some(WpApiParamNavigationsOrderBy::Relevance),
49+
..Default::default()
50+
})
6651
.await
6752
.assert_wp_error(WpErrorCode::NoSearchTermDefined);
6853
}
@@ -71,14 +56,11 @@ async fn list_err_no_search_term_defined() {
7156
#[parallel]
7257
async fn list_err_order_by_include_missing_include() {
7358
api_client()
74-
.posts()
75-
.list_with_edit_context(
76-
&PostEndpointType::Navigation,
77-
&PostListParams {
78-
orderby: Some(WpApiParamPostsOrderBy::Include),
79-
..Default::default()
80-
},
81-
)
59+
.navigations()
60+
.list_with_edit_context(&NavigationListParams {
61+
order_by: Some(WpApiParamNavigationsOrderBy::Include),
62+
..Default::default()
63+
})
8264
.await
8365
.assert_wp_error(WpErrorCode::OrderbyIncludeMissingInclude);
8466
}
@@ -87,14 +69,11 @@ async fn list_err_order_by_include_missing_include() {
8769
#[parallel]
8870
async fn list_err_post_invalid_page_number() {
8971
api_client()
90-
.posts()
91-
.list_with_edit_context(
92-
&PostEndpointType::Navigation,
93-
&PostListParams {
94-
page: Some(99999999),
95-
..Default::default()
96-
},
97-
)
72+
.navigations()
73+
.list_with_edit_context(&NavigationListParams {
74+
page: Some(99999999),
75+
..Default::default()
76+
})
9877
.await
9978
.assert_wp_error(WpErrorCode::PostInvalidPageNumber);
10079
}
@@ -103,11 +82,10 @@ async fn list_err_post_invalid_page_number() {
10382
#[parallel]
10483
async fn retrieve_navigation_err_forbidden_context() {
10584
api_client_as_subscriber()
106-
.posts()
85+
.navigations()
10786
.retrieve_with_edit_context(
108-
&PostEndpointType::Navigation,
109-
&PostId(TestCredentials::instance().navigation_id),
110-
&PostRetrieveParams::default(),
87+
&NavigationId(TestCredentials::instance().navigation_id),
88+
&NavigationRetrieveParams::default(),
11189
)
11290
.await
11391
.assert_wp_error(WpErrorCode::ForbiddenContext);
@@ -117,11 +95,10 @@ async fn retrieve_navigation_err_forbidden_context() {
11795
#[parallel]
11896
async fn retrieve_navigation_err_post_invalid_id() {
11997
api_client()
120-
.posts()
98+
.navigations()
12199
.retrieve_with_edit_context(
122-
&PostEndpointType::Navigation,
123-
&PostId(99999999),
124-
&PostRetrieveParams::default(),
100+
&NavigationId(99999999),
101+
&NavigationRetrieveParams::default(),
125102
)
126103
.await
127104
.assert_wp_error(WpErrorCode::PostInvalidId);
@@ -131,11 +108,10 @@ async fn retrieve_navigation_err_post_invalid_id() {
131108
#[parallel]
132109
async fn update_navigation_err_cannot_edit() {
133110
api_client_as_author()
134-
.posts()
111+
.navigations()
135112
.update(
136-
&PostEndpointType::Navigation,
137-
&PostId(TestCredentials::instance().navigation_id),
138-
&PostUpdateParams::default(),
113+
&NavigationId(TestCredentials::instance().navigation_id),
114+
&NavigationUpdateParams::default(),
139115
)
140116
.await
141117
.assert_wp_error(WpErrorCode::CannotEdit);
@@ -145,11 +121,10 @@ async fn update_navigation_err_cannot_edit() {
145121
#[parallel]
146122
async fn update_navigation_err_invalid_template() {
147123
api_client()
148-
.posts()
124+
.navigations()
149125
.update(
150-
&PostEndpointType::Navigation,
151-
&PostId(TestCredentials::instance().navigation_id),
152-
&PostUpdateParams {
126+
&NavigationId(TestCredentials::instance().navigation_id),
127+
&NavigationUpdateParams {
153128
template: Some("foo".to_string()),
154129
..Default::default()
155130
},

0 commit comments

Comments
 (0)