Post editor able to edit article

This commit is contained in:
LittleSheep 2024-07-30 16:44:04 +08:00
parent 58bb549217
commit fc77c8693f
3 changed files with 40 additions and 2 deletions

View File

@ -94,6 +94,7 @@ class PostEditorController extends GetxController {
'repost_to': repostTo.value?.toJson(),
'edit_to': editTo.value?.toJson(),
'realm': realmZone.value?.toJson(),
'type': type,
}),
);
}
@ -131,6 +132,7 @@ class PostEditorController extends GetxController {
return;
}
type = value.type;
editTo.value = value;
isDraft.value = value.isDraft ?? false;
titleController.text = value.body['title'] ?? '';
@ -142,6 +144,37 @@ class PostEditorController extends GetxController {
contentLength.value = contentController.text.length;
}
String get typeEndpoint {
switch(mode.value) {
case 0:
return 'stories';
case 1:
return 'articles';
default:
return 'stories';
}
}
String get type {
switch(mode.value) {
case 0:
return 'story';
case 1:
return 'article';
default:
return 'story';
}
}
set type(String value) {
switch (value) {
case 'story':
mode.value = 0;
case 'article':
mode.value = 1;
}
}
String? get title {
if (titleController.text.isEmpty) return null;
return titleController.text;
@ -168,6 +201,7 @@ class PostEditorController extends GetxController {
}
set payload(Map<String, dynamic> value) {
type = value['type'];
titleController.text = value['title'] ?? '';
descriptionController.text = value['description'] ?? '';
contentController.text = value['content'] ?? '';

View File

@ -12,6 +12,7 @@ class Post {
List<Tag>? tags;
List<Category>? categories;
List<Post>? replies;
String type;
int? replyId;
int? repostId;
int? realmId;
@ -31,6 +32,7 @@ class Post {
required this.updatedAt,
required this.editedAt,
required this.deletedAt,
required this.type,
required this.body,
required this.tags,
required this.categories,
@ -56,6 +58,7 @@ class Post {
deletedAt: json['deleted_at'] != null
? DateTime.parse(json['deleted_at'])
: null,
type: json['type'],
body: json['body'],
tags: json['tags']?.map((x) => Tag.fromJson(x)).toList().cast<Tag>(),
categories: json['categories']
@ -93,6 +96,7 @@ class Post {
'updated_at': updatedAt.toIso8601String(),
'edited_at': editedAt?.toIso8601String(),
'deleted_at': deletedAt?.toIso8601String(),
'type': type,
'body': body,
'tags': tags,
'categories': categories,

View File

@ -59,12 +59,12 @@ class _PostPublishScreenState extends State<PostPublishScreen> {
Response resp;
if (widget.edit != null) {
resp = await client.put(
'/stories/${widget.edit!.id}',
'/${_editorController.typeEndpoint}/${widget.edit!.id}',
_editorController.payload,
);
} else {
resp = await client.post(
'/stories',
'/${_editorController.typeEndpoint}',
_editorController.payload,
);
}