mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Add publish to API with handler for new local drafts
This commit is contained in:
parent
904bd0f0f5
commit
62a2de6cad
@ -62,6 +62,25 @@ extension WriteFreelyModel {
|
||||
guard let loggedInClient = client else { return }
|
||||
loggedInClient.getPosts(completion: fetchUserPostsHandler)
|
||||
}
|
||||
|
||||
func publish(post: Post) {
|
||||
guard let loggedInClient = client else { return }
|
||||
if post.wfPost == nil {
|
||||
// This is a new local draft.
|
||||
post.wfPost = WFPost(
|
||||
body: post.body,
|
||||
title: post.title,
|
||||
createdDate: post.createdDate
|
||||
)
|
||||
loggedInClient.createPost(
|
||||
post: post.wfPost!, in: post.collection.wfCollection?.alias, completion: publishHandler
|
||||
)
|
||||
} else {
|
||||
// This is an existing post.
|
||||
// 1. Update post.wfPost properties from post properties
|
||||
// 2. Call loggedInClient.updatePost(postId:, updatedPost:, completion: publishHandler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension WriteFreelyModel {
|
||||
@ -176,6 +195,21 @@ private extension WriteFreelyModel {
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
|
||||
func publishHandler(result: Result<WFPost, Error>) {
|
||||
do {
|
||||
let wfPost = try result.get()
|
||||
let foundPostIndex = store.posts.firstIndex(where: {
|
||||
$0.title == wfPost.title && $0.body == wfPost.body
|
||||
})
|
||||
guard let index = foundPostIndex else { return }
|
||||
DispatchQueue.main.async {
|
||||
self.store.posts[index].wfPost = wfPost
|
||||
}
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension WriteFreelyModel {
|
||||
|
@ -32,6 +32,7 @@ struct PostEditorView: View {
|
||||
}
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(action: {
|
||||
model.publish(post: post)
|
||||
post.status = .published
|
||||
}, label: {
|
||||
Image(systemName: "paperplane")
|
||||
|
Loading…
Reference in New Issue
Block a user