Skip to content

Commit

Permalink
feat: upperCase edge function (#8)
Browse files Browse the repository at this point in the history
* feat: uppdercase edge function

* refactor: requested changes

* refactor: minor change
  • Loading branch information
nicolantean authored Oct 13, 2023
1 parent cbeaa91 commit f51426a
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 30 deletions.
17 changes: 0 additions & 17 deletions supabase-ws-flutter-backend/supabase.code-workspace

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.

import { serve } from "https://deno.land/[email protected]/http/server.ts"
import { createClient } from "https://esm.sh/@supabase/[email protected]?target=deno";

const supabaseClient = createClient(
Deno.env.get("SUPABASE_URL") ?? "",
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? "",
{
global: {
headers: {
Authorization: "Bearer " + Deno.env.get("SUPABASE_SERVICE_ROLE_KEY"),
},
},
},
);

serve(async (req) => {
const { id, message } = await req.json()

const capitalizedMessage = message.toUpperCase()

const { error } = await supabaseClient.from("messages").update({ body: capitalizedMessage }).eq("id", id)

if (error != null) {
return new Response(JSON.stringify({ "message": "Error updating message" }), { headers: { "Content-Type": "application/json" } },)
}


return new Response(
JSON.stringify({ "message": "Success" }),
{ headers: { "Content-Type": "application/json" } },
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ class MessagesRepository {
.map((event) => event.requireData());

Future<void> sendMessage(String body) =>
_messagesRemoteSource.sendMessage(body);
_messagesRemoteSource.sendMessage(body: body);

Future<void> uppercaseMessage(Message message) => _messagesRemoteSource
.uppercaseMessage(id: message.id, body: message.body);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ abstract interface class IMessagesRemoteSource {
Future<List<UserMessage>> getMessages();
Stream<List<Message>> getMessagesStream();
Stream<List<UserResponse>> getUsersStream();
Future<void> sendMessage(String body);
Future<void> sendMessage({required String body});
Future<void> uppercaseMessage({required String id, required String body});
}

class MessagesRemoteSource implements IMessagesRemoteSource {
Expand All @@ -21,16 +22,20 @@ class MessagesRemoteSource implements IMessagesRemoteSource {
Future<List<UserMessage>> getMessages() async {
final response = await _supabaseClient
.from('messages')
.select('id, created_at, body, sender, user:sender(id, alias)');
.select('id, created_at, body, sender, user:sender(id, alias)')
.order('created_at', ascending: true);
final messageResponse = MessageResponse.fromJsonList(response);
return messageResponse.toUserMessageList(
userId: _supabaseClient.auth.currentUser!.id,
);
}

@override
Stream<List<Message>> getMessagesStream() =>
_supabaseClient.from('messages').stream(primaryKey: ['id']).map(
Stream<List<Message>> getMessagesStream() => _supabaseClient
.from('messages')
.stream(primaryKey: ['id'])
.order('created_at', ascending: true)
.map(
(response) => Message.fromJsonList(response),
);

Expand All @@ -41,10 +46,15 @@ class MessagesRemoteSource implements IMessagesRemoteSource {
);

@override
Future<void> sendMessage(String body) async {
Future<void> sendMessage({required String body}) async {
final currentUserId = _supabaseClient.auth.currentUser!.id;
return _supabaseClient
.from('messages')
.insert(MessageRequest(body: body, sender: currentUserId));
}

@override
Future<void> uppercaseMessage({required String id, required String body}) =>
_supabaseClient.functions
.invoke('uppercase_message', body: {"id": id, "message": body});
}
2 changes: 1 addition & 1 deletion supabase-ws-flutter-mobile/lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"error_unknown_error_description": "Something went wrong!",
"error_unknown_error_title": "Ops!",
"home_empty_state": "Be the first\nto send a message!",
"home_message_translate": "Translate",
"home_message_uppercase": "Uppercase",
"home_text_field": "Say something",
"log_out": "Log out",
"mail": "Mail",
Expand Down
6 changes: 6 additions & 0 deletions supabase-ws-flutter-mobile/lib/ui/home/home_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_template/core/common/extension/stream_future_extensions.dart';
import 'package:flutter_template/core/di/di_provider.dart';
import 'package:flutter_template/core/model/message.dart';
import 'package:flutter_template/core/model/user_message.dart';
import 'package:flutter_template/core/repository/messages_repository.dart';
import 'package:flutter_template/core/repository/session_repository.dart';
Expand Down Expand Up @@ -43,5 +44,10 @@ class HomeCubit extends Cubit<HomeBaseState> {
Future<void> sendMessage() =>
_messagesRepository.sendMessage(state.currentText);

Future<void> uppercaseMessage(Message message) => _messagesRepository
.uppercaseMessage(message)
.mapToResult()
.filterSuccess(_globalEventHandler.handleError);

Future<void> logOut() => _sessionRepository.logOut().mapToResult();
}
5 changes: 4 additions & 1 deletion supabase-ws-flutter-mobile/lib/ui/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ class _MessagesSection extends StatelessWidget {
itemCount: messages.length,
itemBuilder: (context, index) {
final userMessage = messages[index];
return MessageBox(userMessage: userMessage);
return MessageBox(
userMessage: userMessage,
uppercaseMessage: context.read<HomeCubit>().uppercaseMessage,
);
},
),
),
Expand Down
19 changes: 14 additions & 5 deletions supabase-ws-flutter-mobile/lib/ui/widgets/message_box.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_template/core/common/extension/date_time_extensions.dart';
import 'package:flutter_template/core/model/message.dart';
import 'package:flutter_template/core/model/user_message.dart';
import 'package:flutter_template/ui/extensions/context_extensions.dart';
import 'package:flutter_template/ui/theme/app_theme.dart';
import 'package:flutter_template/ui/theme/text_styles.dart';

class MessageBox extends StatelessWidget {
final UserMessage userMessage;
final Future<void> Function(Message) uppercaseMessage;

const MessageBox({
required this.userMessage,
required this.uppercaseMessage,
super.key,
});

Expand Down Expand Up @@ -72,7 +75,11 @@ class MessageBox extends StatelessWidget {
],
),
),
if (!userMessage.isFromCurrentUser) const _TranslateButton(),
if (!userMessage.isFromCurrentUser)
_UppercaseButton(
message: userMessage.message,
onTap: uppercaseMessage,
),
],
),
),
Expand All @@ -81,13 +88,15 @@ class MessageBox extends StatelessWidget {
);
}

class _TranslateButton extends StatelessWidget {
const _TranslateButton();
class _UppercaseButton extends StatelessWidget {
final Message message;
final Future<void> Function(Message) onTap;
const _UppercaseButton({required this.message, required this.onTap});

@override
Widget build(BuildContext context) => InkWell(
//TODO: add logic
onTap: () => {},
onTap: () => onTap(message),
child: SizedBox(
height: 20.h,
width: 60.w,
Expand All @@ -96,7 +105,7 @@ class _TranslateButton extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.localizations.home_message_translate,
context.localizations.home_message_uppercase,
style: context.theme.textStyles.bodyXSmall
.copyWith(
color: context.theme.colors.textColor.shade200,
Expand Down

0 comments on commit f51426a

Please sign in to comment.