Add bookmark request

This commit is contained in:
Juan Gilsanz Polo
2024-02-23 16:12:15 +01:00
parent 6aa6acfbfd
commit 56c12e3828
5 changed files with 214 additions and 2 deletions

View File

@@ -1,9 +1,14 @@
import 'package:expandable/expandable.dart';
import 'package:flutter/material.dart';
import 'package:linkdy/utils/snackbar.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:linkdy/screens/links/provider/links.provider.dart';
import 'package:linkdy/screens/links/model/add_link.model.dart';
import 'package:linkdy/models/data/post_bookmark.dart';
import 'package:linkdy/providers/router_provider.dart';
import 'package:linkdy/models/data/bookmarks.dart';
import 'package:linkdy/constants/enums.dart';
import 'package:linkdy/constants/regexp.dart';
import 'package:linkdy/models/api_response.dart';
@@ -18,6 +23,12 @@ Future<ApiResponse<CheckBookmark>> checkBookmark(CheckBookmarkRef ref, String ur
return result;
}
@riverpod
FutureOr<ApiResponse<Bookmark>> addBookmark(AddBookmarkRef ref, PostBookmark newBookmark) async {
final result = await ref.watch(apiClientProviderProvider)!.fetchPostBookmark(newBookmark);
return result;
}
@riverpod
class AddLink extends _$AddLink {
@override
@@ -63,4 +74,30 @@ class AddLink extends _$AddLink {
state.markAsUnread = value;
ref.notifyListeners();
}
void addBookmark() async {
final newBookmark = PostBookmark(
url: state.urlController.text,
title: state.titleController.text != "" ? state.titleController.text : state.checkBookmark?.metadata?.title ?? '',
description: state.descriptionController.text != ""
? state.descriptionController.text
: state.checkBookmark?.metadata?.description ?? '',
isArchived: false,
unread: state.markAsUnread,
shared: false,
tagNames: [],
);
print(newBookmark.toJson());
final result = await ref.watch(addBookmarkProvider(newBookmark).future);
print(result.content);
if (result.successful == true) {
ref.invalidate(linksRequestProvider);
ref.watch(routerProvider).pop();
} else {
showSnacbkar(
label: "Add bookmark failed",
color: Colors.red,
);
}
}
}