Skip to content
代码片段 群组 项目
提交 351fec85 编辑于 作者: Raymond Liao's avatar Raymond Liao :nerd:
浏览文件

feat: #500 Should update community post succeed.

上级 ddd03e2c
分支
未找到相关标签
无相关合并请求
显示 77 个添加38 个删除
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:jihu_gitlab_app/core/net/authorized_denied_exception.dart';
import 'package:jihu_gitlab_app/core/net/interceptor/refresh_token_interceptor.dart';
import 'package:jihu_gitlab_app/core/connection_provider/connection.dart';
import 'package:jihu_gitlab_app/core/connection_provider/connection_provider.dart';
import 'package:jihu_gitlab_app/core/net/authorized_denied_exception.dart';
import 'package:jihu_gitlab_app/core/net/interceptor/refresh_token_interceptor.dart';
import 'response.dart' as r;
......@@ -69,9 +69,9 @@ class HttpClient {
});
}
Future<r.Response<T>> put<T>(String path, data) async {
Future<r.Response<T>> put<T>(String path, data, {Connection? connection}) async {
return _request(() async {
var dio = initNewDio(ConnectionProvider.connection);
var dio = initNewDio(connection ?? ConnectionProvider.connection);
var response = await dio.put(path, data: data);
return r.Response(response.data);
});
......
......@@ -83,8 +83,7 @@ class _CommunityPageState extends State<CommunityPage> with TickerProviderStateM
icon: const Icon(Icons.add_box_outlined),
color: const Color(0xFF87878C),
onPressed: () async {
Navigator.of(context).pushNamed(CommunityPostCreationPage.routeName,
arguments: {"projectId": CommunityProject.communityProjectId, "fullPath": CommunityProject.communityRelativePath}).then((value) {
Navigator.of(context).pushNamed(CommunityPostCreationPage.routeName, arguments: {"projectId": CommunityProject.id, "fullPath": CommunityProject.relativePath}).then((value) {
if (value != null && value is bool && value == true) {
_refreshKey.currentState?.onRefresh();
setState(() {});
......
import 'package:flutter/material.dart';
import 'package:jihu_gitlab_app/core/connection_provider/connection_provider.dart';
import 'package:jihu_gitlab_app/core/loader.dart';
import 'package:jihu_gitlab_app/core/widgets/common_app_bar.dart';
import 'package:jihu_gitlab_app/core/widgets/input_field_with_title.dart';
import 'package:jihu_gitlab_app/core/widgets/markdown/markdown_input_box.dart';
import 'package:jihu_gitlab_app/core/widgets/toast.dart';
import 'package:jihu_gitlab_app/l10n/jihu_localizations.dart';
// import 'package:jihu_gitlab_app/modules/issues/manage/models/issue_update_model.dart';
import 'package:jihu_gitlab_app/modules/issues/manage/models/issue_update_model.dart';
import 'models/community_project.dart';
......@@ -20,11 +22,13 @@ class CommunityPostEditPage extends StatefulWidget {
}
class _CommunityPostEditPageState extends State<CommunityPostEditPage> {
// final IssueUpdateModel _model = IssueUpdateModel();
final IssueUpdateModel _model = IssueUpdateModel();
final TextEditingController _issueTitleController = TextEditingController();
final TextEditingController _issueDescriptionController = TextEditingController();
bool _enableUpdate = false;
String _title = '';
String _description = '';
@override
void initState() {
......@@ -34,9 +38,9 @@ class _CommunityPostEditPageState extends State<CommunityPostEditPage> {
}
void onValueChanged() {
String title = _issueTitleController.text;
String description = _issueDescriptionController.text;
_enableUpdate = (title != widget.title || description != widget.description);
_title = _issueTitleController.text;
_description = _issueDescriptionController.text;
_enableUpdate = (_title != widget.title || _description != widget.description);
}
@override
......@@ -51,7 +55,25 @@ class _CommunityPostEditPageState extends State<CommunityPostEditPage> {
Container(
transform: Matrix4.translationValues(-12, 2, 0),
child: TextButton(
onPressed: _enableUpdate ? () {} : null,
onPressed: _enableUpdate
? () async {
Loader.showProgress(context);
_model
.update(CommunityProject.id, widget.postIid,
title: _title != widget.title ? _title : null,
description: _description != widget.description ? _description : null,
connection: ConnectionProvider().communityConnection)
.then((value) {
Loader.hideProgress(context);
if (value) {
Toast.success(context, JiHuLocalizations.dictionary().updateSuccessful);
Navigator.pop(context);
} else {
Toast.error(context, JiHuLocalizations.dictionary().updateFailed);
}
});
}
: null,
child: Text(JiHuLocalizations.dictionary().update, style: TextStyle(color: Theme.of(context).primaryColor.withOpacity(_enableUpdate ? 1 : 0.5)))),
),
]),
......@@ -72,7 +94,7 @@ class _CommunityPostEditPageState extends State<CommunityPostEditPage> {
Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
child: MarkdownInputBox(
projectId: CommunityProject.communityProjectId,
projectId: CommunityProject.id,
controller: _issueDescriptionController,
issueSelectable: false,
memberSelectable: false,
......
......@@ -53,8 +53,7 @@ class CommunityModel {
_selectedCommunityPostTypeName = '';
if (post == null) return;
var labels = post.labels;
CommunityType type =
labels.map((e) => CommunityType.fromJson(e.toJson())).where((e) => e.name.isNotEmpty).where((e) => e.name.startsWith(CommunityProject.communityTypeLabelPrefix)).toSet().toList().first;
CommunityType type = labels.map((e) => CommunityType.fromJson(e.toJson())).where((e) => e.name.isNotEmpty).where((e) => e.name.startsWith(CommunityProject.typeLabelPrefix)).toSet().toList().first;
_selectedCommunityPostTypeName = type.typeName(locale);
}
......
......@@ -56,6 +56,6 @@ class CommunityPost {
}
List<Label> get listLabels {
return labels.where((e) => !e.name.startsWith(CommunityProject.communityTypeLabelPrefix) && !e.name.startsWith(CommunityProject.communityLanguageLabelPrefix)).toList();
return labels.where((e) => !e.name.startsWith(CommunityProject.typeLabelPrefix) && !e.name.startsWith(CommunityProject.languageLabelPrefix)).toList();
}
}
......@@ -33,8 +33,8 @@ class CommunityPostDetailsModel {
config(int postId, int postIid) {
_postId = postId;
_postIid = postIid;
_communityProjectId = CommunityProject.communityProjectId;
_fullPath = CommunityProject.communityRelativePath;
_communityProjectId = CommunityProject.id;
_fullPath = CommunityProject.relativePath;
_dataProvider = DiscussionProvider(
issueId: _postId,
issueIid: _postIid,
......
......@@ -90,7 +90,7 @@ class CommunityPostListModel {
List<String> labelNames = [locale.languageCode == "zh" ? CommunityProject.zhLabelName : CommunityProject.enLabelName];
if (_labelName != null) labelNames.add(_labelName!);
var response = await HttpClient.instance().postWithConnection(
Api.graphql(), getCommunityPostListGraphQLRequestBody(CommunityProject.communityRelativePath, labelNames, _afterCursor, _search, _pageSize), ConnectionProvider().communityConnection);
Api.graphql(), getCommunityPostListGraphQLRequestBody(CommunityProject.relativePath, labelNames, _afterCursor, _search, _pageSize), ConnectionProvider().communityConnection);
var errors = response.body()["errors"];
if (errors != null) throw Exception(errors);
var nodes = response.body()['data']?['project']?['issues']?['nodes'] ?? [];
......
......@@ -10,10 +10,10 @@ import 'package:jihu_gitlab_app/modules/issues/manage/widgets/label.dart';
import 'community_gq_request_body.dart';
class CommunityProject {
static const int communityProjectId = 89335;
static const String communityRelativePath = 'ultimate-plan/jihu-gitlab-app/faq';
static const String communityTypeLabelPrefix = 'type';
static const String communityLanguageLabelPrefix = 'lang';
static const int id = 89335;
static const String relativePath = 'ultimate-plan/jihu-gitlab-app/faq';
static const String typeLabelPrefix = 'type';
static const String languageLabelPrefix = 'lang';
static const String enLabelName = "lang::en";
static const String zhLabelName = "lang::zh";
static const String geekLabelName = "for::geek";
......@@ -29,14 +29,10 @@ class CommunityProject {
Future<bool> refresh() async {
try {
var response = await HttpClient.instance().postWithConnection(Api.graphql(), getCommunityInfoGraphQLRequestBody(communityRelativePath), ConnectionProvider().communityConnection);
var response = await HttpClient.instance().postWithConnection(Api.graphql(), getCommunityInfoGraphQLRequestBody(relativePath), ConnectionProvider().communityConnection);
var project = response.body()['data']?['project'];
_types = ((project?['labels']?['nodes'] ?? []) as List)
.map((e) => CommunityType.fromGraphqlJson(e))
.where((e) => e.name.isNotEmpty)
.where((e) => e.name.startsWith(communityTypeLabelPrefix))
.toSet()
.toList();
_types =
((project?['labels']?['nodes'] ?? []) as List).map((e) => CommunityType.fromGraphqlJson(e)).where((e) => e.name.isNotEmpty).where((e) => e.name.startsWith(typeLabelPrefix)).toSet().toList();
_inMaintenance = project?['archived'] ?? false;
return true;
} catch (error) {
......
import 'package:jihu_gitlab_app/core/connection_provider/connection.dart';
import 'package:jihu_gitlab_app/core/load_state.dart';
import 'package:jihu_gitlab_app/core/net/api.dart';
import 'package:jihu_gitlab_app/core/net/http_client.dart';
......@@ -6,7 +7,7 @@ class IssueUpdateModel {
LoadState _updateState = LoadState.noItemState;
Map<String, dynamic>? _updatedResp;
Future<bool> update(int projectId, int issueIid, {String? title, String? description, String? labels}) async {
Future<bool> update(int projectId, int issueIid, {String? title, String? description, String? labels, Connection? connection}) async {
_updateState = LoadState.loadingState;
try {
String uri = '/projects/$projectId/issues/$issueIid';
......@@ -20,7 +21,7 @@ class IssueUpdateModel {
if (labels != null) {
body['labels'] = labels;
}
var response = await HttpClient.instance().put<Map<String, dynamic>>(Api.join(uri), body);
var response = await HttpClient.instance().put<Map<String, dynamic>>(Api.join(uri), body, connection: connection);
_updatedResp = response.body();
_updateState = LoadState.successState;
return Future.value(true);
......
......@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:jihu_gitlab_app/core/connection_provider/community_connection.dart';
import 'package:jihu_gitlab_app/core/connection_provider/connection_provider.dart';
import 'package:jihu_gitlab_app/core/db_manager.dart';
import 'package:jihu_gitlab_app/core/global_keys.dart';
......@@ -467,12 +468,33 @@ void main() {
await tester.pumpAndSettle();
expect((tester.firstWidget<TextButton>(buttonFinder).child as Text).style!.color, Theme.of(GlobalKeys.rootAppKey.currentContext!).primaryColor.withOpacity(0.5));
// Go back
await tester.tap(find.byType(BackButton));
// Update
titleTextField = find.widgetWithText(TextField, 'Why remote?');
descriptionTextField = find.widgetWithText(TextField, 'Community detail for test');
await tester.enterText(titleTextField, 'New Title');
await tester.enterText(descriptionTextField, 'New Description');
await tester.pumpAndSettle();
when(client.put<Map<String, dynamic>>('/api/v4/projects/89335/issues/238', {'title': 'New Title', 'description': 'New Description'}, connection: argThat(isNotNull, named: 'connection')))
.thenAnswer((_) => Future(() => Response.of<Map<String, dynamic>>({})));
when(client.postWithConnection('/api/graphql', getCommunityDetailsGraphQLRequestBody('ultimate-plan/jihu-gitlab-app/faq', 6), any))
.thenAnswer((_) => Future(() => Response.of<dynamic>(geekCommunityDetailsGraphQLResponse)));
await tester.tap(buttonFinder);
await tester.pumpAndSettle(const Duration(seconds: 2));
expect(find.byType(CommunityPostEditPage), findsNothing);
expect(find.byType(CommunityPostDetailsPage), findsOneWidget);
expect(find.widgetWithText(InkWell, 'Edit'), findsNothing);
expect(find.text('Why remote?'), findsNothing);
expect(find.text('Community detail for test'), findsNothing);
expect(find.text('Why remote?2'), findsOneWidget);
expect(find.text('Community detail for test2'), findsOneWidget);
//
// // Go back
// await tester.tap(find.byType(BackButton));
// await tester.pumpAndSettle();
// expect(find.byType(CommunityPostEditPage), findsNothing);
// expect(find.byType(CommunityPostDetailsPage), findsOneWidget);
// expect(find.widgetWithText(InkWell, 'Edit'), findsNothing);
});
testWidgets('Should not display edit button when user has no edit permission', (WidgetTester tester) async {
......
......@@ -131,8 +131,8 @@ Map<String, dynamic> geekCommunityDetailsGraphQLResponse = {
"id": "gid://gitlab/Issue/326489",
"iid": "238",
"projectId": 89335,
"title": "Why remote?",
"description": "Community detail for test",
"title": "Why remote?2",
"description": "Community detail for test2",
"webUrl": "https://jihulab.com/ultimate-plan/jihu-gitlab-app/faq/-/issues/238",
"confidential": false,
"state": "opened",
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
想要评论请 注册