-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommentEditor.swift
126 lines (123 loc) · 4.9 KB
/
CommentEditor.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// CommentEditor.swift
// wasteof.money
//
// Created by Oren Lindsey on 10/27/23.
//
import SwiftUI
import MarkupEditor
struct CommentEditor: View, MarkupDelegate {
@EnvironmentObject var session: Session
let id: String
let color: Color
let parent: Optional<String>
let poster: Optional<CommentPoster>
let type: String
@State var currenthtml: String = ""
@State private var startHtml: String = ""
@State var editor = false
func markupInput(_ view: MarkupWKWebView) {
MarkupEditor.selectedWebView?.getHtml { html in
currenthtml = html!
}
}
var body: some View {
//ScrollView {
Button {
editor = true
} label: {
if type == "comment" {
HStack {
Image(systemName: "text.bubble")
Text("New Comment")
}
} else if type == "reply" {
HStack {
Image(systemName: "arrow.turn.down.right")
Text("Reply")
}
} else {
VStack {
Image(systemName: "text.bubble")
Text("\(type)")
}.tint(color).buttonStyle(.bordered)
}
}.buttonStyle(.bordered).tint(color).popover(isPresented: $editor, arrowEdge: .top) {
VStack {
HStack {
Button() {
editor.toggle()
} label: {
Text("Cancel").padding(3)
}.tint(color)
Spacer()
Button {
postComment(_id: id, token: session.token, content: currenthtml, parent: parent)
editor = false
} label: {
HStack {
Text("Post")
Image(systemName: "arrow.up.circle.fill")
}.padding(3)
}.tint(color)
}
MarkupEditorView(markupDelegate: self, html: $startHtml)
}.padding(5)
}.padding(5)//id: _id).environmentObject(session)
}
func postComment(_id: String, token: String, content: String, parent: Optional<String>) {
print(content)
//var result: LoginResponse = LoginResponse(ok: "false", new: New(isLoving: false, loves: 0))
let url = URL(string: "https://api.wasteof.money/posts/\(_id)/comments")
guard let requestUrl = url else { fatalError() }
var request = URLRequest(url: requestUrl)
request.httpMethod = "POST"
struct BodyData: Codable {
// Define your data model here.
// This struct should conform to Codable if you want to send it in the request body.
let content: String
let parent: Optional<String>?
}
struct CommentResponse: Codable {
// Define your data model here.
// This struct should conform to Codable if you want to send it in the request body.
let ok: String
let id: String
}
//let postString = "username=\(username)&password=\(password)"
/*struct LoginData: Hashable, Codable {
let username: String
let password: String
}*/
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(token, forHTTPHeaderField: "Authorization")
let body = BodyData(content: content, parent: parent)
let finalBody = try? JSONEncoder().encode(body)
request.httpBody = finalBody
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Error took place \(error)")
//result = LoginResponse(ok: "error.localizedDescription", new: New(isLoving: false, loves: 0))
} else {
if let data = data, let dataString = String(data: data, encoding: .utf8) {
let jsonData = dataString.data(using: .utf8)!
var response: CommentResponse = CommentResponse(ok: "no", id: "")
//print(response)
do {
response = try JSONDecoder().decode(CommentResponse.self, from: jsonData)
if response.ok == "made comment" {
print(response.id)
}
} catch DecodingError.keyNotFound(_, _) {
print("something went wrong, key not found")
print("error: \(String(decoding: jsonData, as: UTF8.self))")
} catch {
print("something went wrong")
print(String(decoding: jsonData, as: UTF8.self))
}
}
}
}
task.resume()
}
}