@@ -48,6 +48,12 @@ struct LongForm: View, QuestForm {
4848 @State private var showNotesBox = false
4949
5050 @State private var noteText = " "
51+
52+ @State private var alertMessage = " "
53+
54+ @State private var showCreateNoteMessage = false
55+
56+ @StateObject private var noteViewModel = NotesViewModel ( )
5157
5258 var body : some View {
5359 ZStack {
@@ -89,6 +95,17 @@ struct LongForm: View, QuestForm {
8995 . padding ( [ . trailing] , 20 )
9096 }
9197
98+ if showCreateNoteMessage {
99+ Text ( alertMessage)
100+ . foregroundColor ( alertMessage == " Note submitted successfully " ? Color . green : Color . red)
101+ . padding ( . horizontal, 20 )
102+ . onAppear {
103+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 3 ) {
104+ showCreateNoteMessage = false
105+ }
106+ }
107+ }
108+
92109 if showNotesBox {
93110 VStack ( alignment: . leading, spacing: 10 ) {
94111 TextEditor ( text: $noteText)
@@ -97,27 +114,44 @@ struct LongForm: View, QuestForm {
97114 . border ( Color ( red: 135 / 255 , green: 62 / 255 , blue: 242 / 255 ) )
98115
99116 HStack {
100- Button ( " Submit " ) {
101- submitNote ( )
102- showNotesBox = false
117+ Button ( action: {
118+ Task {
119+ do {
120+ try await submitNote ( )
121+ } catch {
122+ showCreateNoteMessage = true
123+ showNotesBox = false
124+
125+ }
126+ }
127+ } ) {
128+ if noteViewModel. isLoading {
129+ ProgressView ( )
130+ } else {
131+ Text ( " Submit " )
132+ . font ( . custom( " Lato-Bold " , size: 16 ) )
133+ . foregroundColor ( . white)
134+ . padding ( )
135+ . frame ( maxWidth: . infinity)
136+ . background ( noteText != " " ? Color ( red: 135 / 255 , green: 62 / 255 , blue: 242 / 255 ) : Color . gray)
137+ . cornerRadius ( 9 )
138+ }
103139 }
104- . padding ( )
105- . frame ( maxWidth: . infinity)
106- . background ( Color . blue)
107- . foregroundColor ( . white)
108- . cornerRadius ( 8 )
109- . padding ( . horizontal, 20 )
110-
111- Button ( " Cancel " ) {
140+ . disabled ( noteText == " " )
141+
142+ Button ( action: {
112143 showNotesBox = false
144+ } ) {
145+ Text ( " Cancel " )
146+ . font ( . custom( " Lato-Bold " , size: 16 ) )
147+ . foregroundColor ( . white)
148+ . padding ( )
149+ . frame ( maxWidth: . infinity)
150+ . background ( Color . red)
151+ . cornerRadius ( 9 )
113152 }
114- . padding ( )
115- . frame ( maxWidth: . infinity)
116- . background ( Color . red)
117- . foregroundColor ( . white)
118- . cornerRadius ( 8 )
119- . padding ( . horizontal, 20 )
120153 }
154+ . padding ( . horizontal, 20 )
121155 }
122156 . padding ( . top, 10 )
123157 }
@@ -138,6 +172,17 @@ struct LongForm: View, QuestForm {
138172 }
139173 }
140174 VStack {
175+ if showSubmitAlert {
176+ Text ( submitAlert)
177+ . font ( . custom( " Lato-Bold " , size: 16 ) )
178+ . foregroundColor ( . red)
179+ . background ( Color . white)
180+ . onAppear {
181+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 2 ) {
182+ showSubmitAlert = false
183+ }
184+ }
185+ }
141186 Button ( action: {
142187 if !viewModel. answersToBeSubmitted. isEmpty {
143188 if let action = action {
@@ -219,8 +264,31 @@ struct LongForm: View, QuestForm {
219264 }
220265 }
221266
222- func submitNote( ) {
267+ func submitNote( ) async throws {
223268 print ( " Note to be submitted: \( noteText) " )
269+
270+ do {
271+ let notesResult = try await noteViewModel. createNote ( note: noteText, lat: 0.0 , long: 0.0 )
272+
273+ if notesResult {
274+ print ( " Notes composed successfully " )
275+ alertMessage = " Note submitted successfully "
276+ showCreateNoteMessage = true
277+
278+ } else {
279+ alertMessage = " Error creating note "
280+ showCreateNoteMessage = true
281+ }
282+ } catch {
283+ alertMessage = " Error creating note: \( error. localizedDescription) "
284+ print ( " Error creating note: \( error. localizedDescription) " )
285+ throw error
286+ }
287+
288+ await MainActor . run {
289+ noteViewModel. isLoading = false
290+ showNotesBox = false
291+ }
224292 }
225293
226294 func uploadImageToKartaView( ) {
0 commit comments