Skip to content

Commit

Permalink
Trial: Post to db
Browse files Browse the repository at this point in the history
  • Loading branch information
Naveen-g09 committed Jan 11, 2024
1 parent 8473a3b commit 652aa21
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 22 deletions.
31 changes: 25 additions & 6 deletions app/settings.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<View className="flex-1 items-center justify-center">
<Text className="text-2xl font-bold">Setting</Text>
<Text className="text-2xl font-bold">Settings</Text>
<View
className="my-8 border-t border-gray-300 w-4/5"
lightColor="#eee"
Expand All @@ -19,13 +32,19 @@ export default function ModalScreen() {
className="h-10 border border-blue-300 p-2 mb-4"
placeholder="Enter your phone number"
keyboardType="phone-pad"
// onChangeText={(text) => setPhoneNumber(text)} // assuming you have state for phone number
value={phoneNumber}
onChangeText={(text) => setPhoneNumber(text)}
editable={isEditing}
/>

{/* Save/Edit button */}
<Text
className="bg-blue-500 text-white p-2 rounded"
// onPress={savePhoneNumber} // assuming you have a function to save phone number
className={`bg-blue-500 text-white p-2 rounded ${
isEditing ? "" : "opacity-50"
}`}
onPress={isEditing ? savePhoneNumber : editPhoneNumber}
>
Save Phone Number
{isEditing ? "Save Phone Number" : "Edit Phone Number"}
</Text>

{/* Use a light status bar on iOS to account for the black space above the modal */}
Expand Down
65 changes: 49 additions & 16 deletions worker-turso-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function buildLibsqlClient(env: Env): LibsqlClient {
return createClient({ url, authToken });
}

console.log(buildLibsqlClient);

function buildRouter(env: Env): RouterType {
const router = Router();

Expand All @@ -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 }));
Expand Down

0 comments on commit 652aa21

Please sign in to comment.