Skip to content

Commit

Permalink
feat: can edit threads (notifier not working still) #298
Browse files Browse the repository at this point in the history
  • Loading branch information
bvlourenco committed Nov 19, 2022
1 parent eb1304a commit 64f3e4e
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 22 deletions.
37 changes: 23 additions & 14 deletions backend/src/mongodb/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type CreateThreadData struct {
}

type UpdateThreadData struct {
Meeting primitive.ObjectID `json:"meeting"`
Kind models.ThreadKind `json:"kind"`
Meeting *primitive.ObjectID `json:"meeting"`
Kind models.ThreadKind `json:"kind"`
}

func (utd *UpdateThreadData) ParseBody(body io.Reader) error {
Expand All @@ -39,9 +39,10 @@ func (utd *UpdateThreadData) ParseBody(body io.Reader) error {
return err
}

if len(utd.Meeting) == 0 {
return errors.New("invalid meeting")
}
// Putting meeting optional for now
// if len(utd.Meeting) == 0 {
// return errors.New("invalid meeting")
// }

if len(utd.Kind) == 0 {
return errors.New("invalid kind")
Expand Down Expand Up @@ -171,15 +172,23 @@ func (t *ThreadsType) UpdateThread(threadID primitive.ObjectID, data UpdateThrea
return nil, errors.New("Thread kind not valid")
}

if _, err := Meetings.GetMeeting(data.Meeting); err != nil {
return nil, err
}

var updateQuery = bson.M{
"$set": bson.M{
"meeting": data.Meeting,
"kind": data.Kind,
},
var updateQuery primitive.M
if data.Meeting != nil {
if _, err := Meetings.GetMeeting(*data.Meeting); err != nil {
return nil, err
}
updateQuery = bson.M{
"$set": bson.M{
"meeting": data.Meeting,
"kind": data.Kind,
},
}
} else {
updateQuery = bson.M{
"$set": bson.M{
"kind": data.Kind,
},
}
}

var optionsQuery = options.FindOneAndUpdate()
Expand Down
2 changes: 1 addition & 1 deletion backend/src/router/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func deleteThread(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Thread not found", http.StatusNotFound)
return
}

for _, commentID := range t.Comments {
_, err = mongodb.Posts.DeletePost(commentID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion backend/static/swagger.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/swagger/threads-id.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"type": "object",
"required": [
"kind",
"meeting"
"meeting (optional)"
],
"properties": {
"kind": {
Expand Down
124 changes: 124 additions & 0 deletions frontend/lib/components/threads/editThreadForm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import 'package:flutter/material.dart';
import 'package:frontend/models/post.dart';
import 'package:frontend/models/thread.dart';
import 'package:frontend/services/postService.dart';
import 'package:frontend/services/threadService.dart';

class EditThreadForm extends StatefulWidget {
final Thread? thread;
final Post? post;
EditThreadForm({Key? key, this.thread, this.post}) : super(key: key);

@override
_EditThreadFormState createState() => _EditThreadFormState();
}

class _EditThreadFormState extends State<EditThreadForm> {
final _formKey = GlobalKey<FormState>();
late TextEditingController _textController;
String kind = 'TEMPLATE';

@override
void initState() {
super.initState();
_textController = TextEditingController(text: widget.post!.text);
}

void _submit(BuildContext context) async {
if (_formKey.currentState!.validate()) {
var text = _textController.text;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Editing...')),
);

PostService _postService = PostService();
ThreadService _threadService = ThreadService();

Post? p = await _postService.updatePost(widget.post!.id, text);
Thread? t =
await _threadService.updateThread(id: widget.thread!.id, kind: kind);

if (p != null && t != null) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();

ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Done'),
duration: Duration(seconds: 2),
),
);

Navigator.pop(context);
} else {
ScaffoldMessenger.of(context).hideCurrentSnackBar();

ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('An error occured.')),
);

Navigator.pop(context);
}
}
}

@override
Widget build(BuildContext context) {
List<String> kinds = ["TEMPLATE", "TO", "FROM", "PHONE_CALL", "MEETING"];
return Form(
key: _formKey,
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
keyboardType: TextInputType.multiline,
textInputAction: TextInputAction.newline,
controller: _textController,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please add the content of communication';
}
return null;
},
decoration: const InputDecoration(
icon: const Icon(Icons.work),
labelText: "Content *",
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonFormField<String>(
icon: Icon(Icons.tag),
items: kinds
.map(
(e) => DropdownMenuItem<String>(value: e, child: Text(e)))
.toList(),
value: kinds[0],
selectedItemBuilder: (BuildContext context) {
return kinds.map((e) {
return Align(
alignment: AlignmentDirectional.centerStart,
child: Container(child: Text(e)),
);
}).toList();
},
onChanged: (next) {
setState(() {
kind = next!;
});
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () => _submit(context),
child: const Text('Submit'),
),
),
],
),
);
}
}
17 changes: 13 additions & 4 deletions frontend/lib/components/threads/threadCard/threadCardHeader.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import 'package:flutter/material.dart';
import 'package:frontend/components/blurryDialog.dart';
import 'package:frontend/components/threads/editThreadForm.dart';
import 'package:frontend/components/threads/threadCard/threadCard.dart';
import 'package:frontend/models/company.dart';
import 'package:frontend/models/meeting.dart';
import 'package:frontend/models/member.dart';
import 'package:frontend/models/post.dart';
import 'package:frontend/models/speaker.dart';
import 'package:frontend/models/thread.dart';
import 'package:frontend/routes/company/CompanyTableNotifier.dart';
import 'package:frontend/routes/meeting/MeetingsNotifier.dart';
import 'package:frontend/routes/speaker/speakerNotifier.dart';
import 'package:frontend/services/companyService.dart';
import 'package:frontend/services/meetingService.dart';
import 'package:frontend/services/speakerService.dart';
Expand Down Expand Up @@ -108,6 +106,17 @@ class ThreadCardHeader extends StatelessWidget {
);
}

void _editThreadModal(context) {
showModalBottomSheet(
context: context,
builder: (context) {
return Container(
child: EditThreadForm(thread: thread, post: p),
);
},
);
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
Expand Down Expand Up @@ -178,7 +187,7 @@ class ThreadCardHeader extends StatelessWidget {
padding: const EdgeInsets.all(8.0),
child: IconButton(
onPressed: () {
print("Edit thread");
_editThreadModal(context);
},
icon: Icon(Icons.edit)),
),
Expand Down
2 changes: 1 addition & 1 deletion frontend/lib/services/threadService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ThreadService extends Service {
}
}

Future<Thread?> updateThread(String id, String meetingId, String kind) async { //TODO are meetingId or kind nullable?
Future<Thread?> updateThread({required String id, String? meetingId, required String kind}) async {
var body = {
"meeting": meetingId,
"kind": kind,
Expand Down

0 comments on commit 64f3e4e

Please sign in to comment.