Add publish to API with handler for new local drafts

This commit is contained in:
Angelo Stavrow 2020-08-25 16:59:38 -04:00
parent 904bd0f0f5
commit 62a2de6cad
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 35 additions and 0 deletions

View File

@ -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 {

View File

@ -32,6 +32,7 @@ struct PostEditorView: View {
}
ToolbarItem(placement: .primaryAction) {
Button(action: {
model.publish(post: post)
post.status = .published
}, label: {
Image(systemName: "paperplane")