chore: save lemma edits to database (#3360)

This commit is contained in:
ggurdin 2025-07-07 14:48:00 -04:00 committed by GitHub
parent 41830f2f2d
commit f50f202c03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 103 additions and 7 deletions

View file

@ -65,6 +65,8 @@ class PApiUrls {
"${PApiUrls.choreoEndpoint}/practice";
static String lemmaDictionary = "${PApiUrls.choreoEndpoint}/lemma_definition";
static String lemmaDictionaryEdit =
"${PApiUrls.choreoEndpoint}/lemma_definition/edit";
static String morphDictionary = "${PApiUrls.choreoEndpoint}/morph_meaning";
static String activityPlanGeneration =

View file

@ -0,0 +1,40 @@
class LemmaEditRequest {
String lemma;
String partOfSpeech;
String lemmaLang;
String userL1;
String? newMeaning;
List<String>? newEmojis;
LemmaEditRequest({
required this.lemma,
required this.partOfSpeech,
required this.lemmaLang,
required this.userL1,
this.newMeaning,
this.newEmojis,
});
Map<String, dynamic> toJson() {
return {
"lemma": lemma,
"part_of_speech": partOfSpeech,
"lemma_lang": lemmaLang,
"user_l1": userL1,
"new_meaning": newMeaning,
"new_emojis": newEmojis,
};
}
factory LemmaEditRequest.fromJson(Map<String, dynamic> json) {
return LemmaEditRequest(
lemma: json["lemma"],
partOfSpeech: json["part_of_speech"],
lemmaLang: json["lemma_lang"],
userL1: json["user_l1"],
newMeaning: json["new_meaning"],
newEmojis: List<String>.from(json["new_emojis"] ?? []),
);
}
}

View file

@ -10,6 +10,7 @@ import 'package:fluffychat/pangea/common/config/environment.dart';
import 'package:fluffychat/pangea/common/network/requests.dart';
import 'package:fluffychat/pangea/common/network/urls.dart';
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
import 'package:fluffychat/pangea/lemmas/lemma_edit_request.dart';
import 'package:fluffychat/pangea/lemmas/lemma_info_request.dart';
import 'package:fluffychat/pangea/lemmas/lemma_info_response.dart';
import 'package:fluffychat/widgets/matrix.dart';
@ -101,4 +102,35 @@ class LemmaInfoRepo {
rethrow;
}
}
static Future<void> edit(LemmaEditRequest request) async {
final Requests req = Requests(
choreoApiKey: Environment.choreoApiKey,
accessToken: MatrixState.pangeaController.userController.accessToken,
);
final resp = await req.post(
url: PApiUrls.lemmaDictionaryEdit,
body: request.toJson(),
);
if (resp.statusCode != 200) {
throw Exception(
'Failed to edit lemma: ${resp.statusCode} ${resp.body}',
);
}
final decodedBody = jsonDecode(utf8.decode(resp.bodyBytes));
final response = LemmaInfoResponse.fromJson(decodedBody);
set(
LemmaInfoRequest(
lemma: request.lemma,
partOfSpeech: request.partOfSpeech,
lemmaLang: request.lemmaLang,
userL1: request.userL1,
),
response,
);
}
}

View file

@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
import 'package:fluffychat/pangea/constructs/construct_identifier.dart';
import 'package:fluffychat/pangea/learning_settings/constants/language_constants.dart';
import 'package:fluffychat/pangea/lemmas/lemma_edit_request.dart';
import 'package:fluffychat/pangea/lemmas/lemma_info_repo.dart';
import 'package:fluffychat/pangea/lemmas/lemma_info_request.dart';
import 'package:fluffychat/pangea/lemmas/lemma_info_response.dart';
@ -84,14 +86,34 @@ class LemmaMeaningBuilderState extends State<LemmaMeaningBuilder> {
void toggleEditMode(bool value) => setState(() => editMode = value);
Future<void> editLemmaMeaning(String userEdit) async {
final originalMeaning = lemmaInfo;
if (originalMeaning != null) {
LemmaInfoRepo.set(
_request,
LemmaInfoResponse(emoji: originalMeaning.emoji, meaning: userEdit),
try {
await LemmaInfoRepo.edit(
LemmaEditRequest(
lemma: widget.constructId.lemma,
partOfSpeech: widget.constructId.category,
lemmaLang: widget.langCode,
userL1: MatrixState
.pangeaController.languageController.userL1?.langCode ??
LanguageKeys.defaultLanguage,
newMeaning: userEdit,
newEmojis: lemmaInfo?.emoji,
),
);
} catch (e, s) {
ErrorHandler.logError(
e: e,
s: s,
data: {
'lemma': widget.constructId.lemma,
'partOfSpeech': widget.constructId.category,
'lemmaLang': widget.langCode,
'userL1': MatrixState
.pangeaController.languageController.userL1?.langCode ??
LanguageKeys.defaultLanguage,
'newMeaning': userEdit,
},
);
} finally {
toggleEditMode(false);
_fetchLemmaMeaning();
}