Skip to content

Commit

Permalink
feat(tabmoney): Adicionado interação de tabcoins para post e comentários
Browse files Browse the repository at this point in the history
  • Loading branch information
avuenja committed Dec 10, 2022
1 parent 920bd75 commit 7a3b8aa
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 8 deletions.
17 changes: 17 additions & 0 deletions lib/src/services/content.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:tabnews/src/controllers/app.dart';
import 'package:tabnews/src/enviroment_vars.dart';
import 'package:tabnews/src/models/comment.dart';

Expand Down Expand Up @@ -118,4 +119,20 @@ class ContentService {

return HttpResponse<Content>(response.statusCode, response.body);
}

Future<HttpResponse> postTabcoins(String slug, String type) async {
final response = await http.post(
Uri.parse('$apiUrl/$slug/tabcoins'),
headers: {
'Set-Cookie': 'session_id=${AppController.auth.value}',
'Cookie': 'session_id=${AppController.auth.value}',
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode({
'transaction_type': type,
}),
);

return HttpResponse(response.statusCode, response.body);
}
}
48 changes: 41 additions & 7 deletions lib/src/ui/pages/content.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:tabnews/src/ui/widgets/tabcoins.dart';
import 'package:timeago/timeago.dart' as timeago;

import 'package:tabnews/src/controllers/favorites.dart';
Expand Down Expand Up @@ -51,6 +52,29 @@ class _ContentPageState extends State<ContentPage> {
});
}

_tabcoins(String vote) async {
var tabcoinsResp = await _contentService.postTabcoins(
'${widget.username}/${widget.slug}',
vote == 'upvote' ? 'credit' : 'debit',
);

if (tabcoinsResp.ok) {
setState(() {
content.tabcoins = tabcoinsResp.data['tabcoins'];
});
} else {
_onResponse(tabcoinsResp.message);
}
}

void _onResponse(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
),
);
}

@override
Widget build(BuildContext context) {
timeago.setLocaleMessages('pt-BR', timeago.PtBrMessages());
Expand Down Expand Up @@ -89,13 +113,23 @@ class _ContentPageState extends State<ContentPage> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${content.ownerUsername} · ${timeago.format(DateTime.parse(content.publishedAt!), locale: "pt-BR")}',
style: const TextStyle().copyWith(
color: context.isDarkMode
? Colors.grey.shade400
: Colors.grey.shade700,
),
Row(
children: [
Text(
'${content.ownerUsername} · ${timeago.format(DateTime.parse(content.publishedAt!), locale: "pt-BR")}',
style: const TextStyle().copyWith(
color: context.isDarkMode
? Colors.grey.shade400
: Colors.grey.shade700,
),
),
const Spacer(),
Tabcoins(
upvote: () => _tabcoins('upvote'),
tabcoins: '${content.tabcoins}',
downvote: () => _tabcoins('downvote'),
),
],
),
const SizedBox(height: 10.0),
Text(
Expand Down
43 changes: 42 additions & 1 deletion lib/src/ui/widgets/item_comment.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import 'package:flutter/material.dart';
import 'package:tabnews/src/services/content.dart';
import 'package:tabnews/src/ui/pages/profile_user.dart';
import 'package:tabnews/src/ui/widgets/comments_children.dart';
import 'package:tabnews/src/ui/widgets/tabcoins.dart';
import 'package:tabnews/src/utils/navigation.dart';
import 'package:timeago/timeago.dart' as timeago;

import 'package:tabnews/src/extensions/dark_mode.dart';
import 'package:tabnews/src/models/comment.dart';
import 'package:tabnews/src/ui/widgets/markdown.dart';

class ItemComment extends StatelessWidget {
class ItemComment extends StatefulWidget {
final Comment comment;
final ScrollController controller;

Expand All @@ -18,6 +20,39 @@ class ItemComment extends StatelessWidget {
required this.controller,
});

@override
State<ItemComment> createState() => _ItemCommentState();
}

class _ItemCommentState extends State<ItemComment> {
Comment get comment => widget.comment;
ScrollController get controller => widget.controller;

final _contentService = ContentService();

_tabcoins(String vote) async {
var tabcoinsResp = await _contentService.postTabcoins(
'${comment.ownerUsername}/${comment.slug}',
vote == 'upvote' ? 'credit' : 'debit',
);

if (tabcoinsResp.ok) {
setState(() {
comment.tabcoins = tabcoinsResp.data['tabcoins'];
});
} else {
_onResponse(tabcoinsResp.message);
}
}

void _onResponse(String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
),
);
}

@override
Widget build(BuildContext context) {
timeago.setLocaleMessages('pt-BR', timeago.PtBrMessages());
Expand Down Expand Up @@ -45,6 +80,12 @@ class ItemComment extends StatelessWidget {
: Colors.grey.shade700,
),
),
const Spacer(),
Tabcoins(
upvote: () => _tabcoins('upvote'),
tabcoins: '${comment.tabcoins}',
downvote: () => _tabcoins('downvote'),
),
],
),
MarkedownReader(
Expand Down
36 changes: 36 additions & 0 deletions lib/src/ui/widgets/tabcoins.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:flutter/material.dart';

class Tabcoins extends StatefulWidget {
final String tabcoins;
final void Function() upvote;
final void Function() downvote;

const Tabcoins({
super.key,
required this.tabcoins,
required this.upvote,
required this.downvote,
});

@override
State<Tabcoins> createState() => _TabcoinsState();
}

class _TabcoinsState extends State<Tabcoins> {
@override
Widget build(BuildContext context) {
return Row(
children: [
IconButton(
onPressed: widget.upvote,
icon: const Icon(Icons.expand_less),
),
Text(widget.tabcoins),
IconButton(
onPressed: widget.downvote,
icon: const Icon(Icons.expand_more),
),
],
);
}
}

0 comments on commit 7a3b8aa

Please sign in to comment.