From 652aa213f1a60b239530844cd5276ab783777781 Mon Sep 17 00:00:00 2001 From: Naveen Date: Fri, 12 Jan 2024 00:51:07 +0530 Subject: [PATCH] Trial: Post to db --- app/settings.tsx | 31 +++++++++++++---- worker-turso-ts/src/index.ts | 65 +++++++++++++++++++++++++++--------- 2 files changed, 74 insertions(+), 22 deletions(-) diff --git a/app/settings.tsx b/app/settings.tsx index 4e87f52..8faf2da 100644 --- a/app/settings.tsx +++ b/app/settings.tsx @@ -1,13 +1,26 @@ import { StatusBar } from "expo-status-bar"; -import React from "react"; +import React, { useState } from "react"; import { TextInput } from "react-native"; import { Text, View } from "../components/Themed"; export default function ModalScreen() { + const [phoneNumber, setPhoneNumber] = useState(""); + const [isEditing, setIsEditing] = useState(true); + const [user, setUser] = useState(null); + + const savePhoneNumber = () => { + setUser({phoneNumber,}); + setIsEditing(false); + }; + const editPhoneNumber = () => { + setIsEditing(true); + }; + + console.log("user", user); return ( - Setting + Settings setPhoneNumber(text)} // assuming you have state for phone number + value={phoneNumber} + onChangeText={(text) => setPhoneNumber(text)} + editable={isEditing} /> + + {/* Save/Edit button */} - Save Phone Number + {isEditing ? "Save Phone Number" : "Edit Phone Number"} {/* Use a light status bar on iOS to account for the black space above the modal */} diff --git a/worker-turso-ts/src/index.ts b/worker-turso-ts/src/index.ts index b31384c..4cb29a6 100644 --- a/worker-turso-ts/src/index.ts +++ b/worker-turso-ts/src/index.ts @@ -32,6 +32,8 @@ function buildLibsqlClient(env: Env): LibsqlClient { return createClient({ url, authToken }); } +console.log(buildLibsqlClient); + function buildRouter(env: Env): RouterType { const router = Router(); @@ -53,24 +55,55 @@ function buildRouter(env: Env): RouterType { }); }); - //post changes to tasks table - router.post('call-logs', async (request) => { - const client = buildLibsqlClient(env); - const body = await request.json(); - const rs = await client.execute({ - sql: 'insert into callLogs (taskId, callTime, callStatus, duration) values (?, ?, ?, ?)', - args: [body.taskId, body.callTime, body.callStatus, body.duration], - }); + router.post('/call-logs', async (request) => { + try { + const client = buildLibsqlClient(env); + const jsonBody = await request.json(); + console.log('Parsed Request Body:', jsonBody); + const rs = await client.execute({ + sql: 'insert into callLogs (taskId, callTime, callStatus, duration) values (?, ?, ?, ?)', + args: [jsonBody.taskId, jsonBody.callTime, jsonBody.callStatus, jsonBody.duration], + }); + + console.log('SQL Execution Result:', rs); + + return new Response('Successfully inserted into callLogs', { + status: 200, + headers: { 'Content-Type': 'text/plain' }, + }); + } catch (error) { + console.error('Error inserting into callLogs:', error); + return new Response('Error inserting into callLogs', { + status: 500, + headers: { 'Content-Type': 'text/plain' }, + }); + } }); - //post changes to callLogs table - router.post('tasks', async (request) => { - const client = buildLibsqlClient(env); - const body = await request.json(); - const rs = await client.execute({ - sql: 'insert into tasks (name, contactNumber, city, state, targetCallCount) values (?, ?, ?, ?, ?)', - args: [body.name, body.contactNumber, body.city, body.state, body.targetCallCount], - }); + router.post('/tasks', async (request) => { + try { + const client = buildLibsqlClient(env); + console.log(client); + const jsonBody = await request.json(); + console.log('Parsed Request Body:', jsonBody); + const rs = await client.execute({ + sql: 'insert into tasks (name, contactNumber, city, state, targetCallCount) values (?, ?, ?, ?, ?)', + args: [jsonBody.name, jsonBody.contactNumber, jsonBody.city, jsonBody.state, jsonBody.targetCallCount], + }); + + console.log('SQL Execution Result:', rs); + + return new Response('Successfully inserted into tasks', { + status: 200, + headers: { 'Content-Type': 'text/plain' }, + }); + } catch (error) { + console.error('Error inserting into tasks:', error); + return new Response('Error inserting into tasks', { + status: 500, + headers: { 'Content-Type': 'text/plain' }, + }); + } }); router.all('*', () => new Response('Not Found.', { status: 404 }));