Skip to content

Commit

Permalink
POR FIN LOS LOGS FUNCIONAN (mas o menos)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakaHaceCosas committed Jun 25, 2024
1 parent e158a90 commit c3147cb
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 162 deletions.
9 changes: 7 additions & 2 deletions app/CreateObjective.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Button from "@/components/Buttons";
import * as Router from "expo-router";
import Notification from "@/components/Notification";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { termLog } from "./DeveloperInterface";

// TypeScript, supongo
interface FormProps {
Expand Down Expand Up @@ -137,7 +138,8 @@ export default function Form({ onSubmit }: FormProps) {
await processData(formData); // procesa
Router.router.push("/"); // vuelve a casa DESPUES de procesar
} catch (e) {
console.error("error creating obj:", e);
const log = "Could not create an objective, got error: " + e;
termLog(log, "error");
}
};

Expand Down Expand Up @@ -173,7 +175,10 @@ export default function Form({ onSubmit }: FormProps) {

await AsyncStorage.setItem("objs", JSON.stringify(finalObjs));
} catch (e) {
console.error("Error storing data:", e);
const log =
"Got an error storing data while processing data to create an objective: " +
e;
termLog(log, "error");
}
};

Expand Down
58 changes: 10 additions & 48 deletions app/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import GapView from "@/components/GapView";
import Footer from "@/components/Footer";
import AsyncStorage from "@react-native-async-storage/async-storage";
import Button from "@/components/Buttons";
import { testLog } from "./DeveloperInterface";
import { termLog } from "./DeveloperInterface";

// TypeScript, supongo
interface Objective {
Expand Down Expand Up @@ -54,32 +54,20 @@ export default function Dashboard() {
const storedObjs = await AsyncStorage.getItem("objs");
if (storedObjs) {
setObjs(JSON.parse(storedObjs));
console.log(
"%cGOD%cAll is ok%c Objectives (OBJS) fetched and parsed!",
"font-weight: bold; background: #30FF97; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #30FF97;"
);
termLog("Objectives (OBJS) fetched and parsed!", "success");
} else {
await AsyncStorage.setItem("objs", JSON.stringify({}));
setObjs({});
console.log(
"%cWOR%cDev error%c Could not get objectives (OBJS) fetched! Setting them to an empty array ( {} )",
"font-weight: bold; background: #FFD700; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #FFD700;"
termLog(
"Could not get objectives (OBJS) fetched! Setting them to an empty array ( {} )",
"warn"
);
}
} catch (e) {
const log =
"Could not get objectives (OBJS) fetched due to error: " +
e;
console.log(
"%cWOR%cDev error%c " + log,
"font-weight: bold; background: #FFD700; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #FFD700;"
);
termLog(log, "error");
}
};

Expand All @@ -104,36 +92,15 @@ export default function Dashboard() {

await AsyncStorage.setItem("objs", JSON.stringify(objs));
const log: string = `OBJ (Objective) with ID ${id} has been removed.`;
testLog(
`OBJ (Objective) with ID ${id} has been removed.`,
"log"
);
console.log(
"%cGOD%cAll is ok%c " + log,
"font-weight: bold; background: #30FF97; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #30FF97;"
);
termLog(log, "success");
Router.router.replace("/");
} else {
const log: string = `No OBJS found - no way to delete.`;
testLog(`No OBJS found - no way to delete.`, "warn");
console.log(
"%cHMM%cDev warning%c " + log,
"font-weight: bold; background: #FFC832; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #FFC832;"
);
termLog(log, "warn");
}
} catch (e) {
const log: string = `Error removing OBJ, got the following: ${e}`;
testLog(`Error removing OBJ, got the following: ${e}`, "warn");
console.log(
"%cWOR%cDev error%c " + log,
"font-weight: bold; background: #FFD700; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #FFD700;"
);
termLog(log, "error");
}
};

Expand All @@ -160,12 +127,7 @@ export default function Dashboard() {
const obj = objs[key];
if (!obj) {
const log = `Data is undefined for objective with key: ${key}`;
console.log(
"%cWOR%cDev error%c " + log,
"font-weight: bold; background: #FFD700; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #FFD700;"
);
termLog(log, "warn");
return null;
}

Expand Down
35 changes: 34 additions & 1 deletion app/DeveloperInterface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const testLog = (
) => {
const logMessage = message.toString();
const timestamp = Date.now();
// @ts-expect-error: Assigned an extra "success" type to console.log which is not existing, therefore gives a type error.
const newLog: Log = { message: logMessage, type, timestamp };

// Add the log to the global logs array
Expand Down Expand Up @@ -132,6 +133,39 @@ export const testLog = (
console.log(`%c${prefix} %c${logMessage}`, style, "");
};

export const termLog = (
message: string,
type: "log" | "warn" | "error" | "success" = "log"
) => {
let prefix;
let style;
switch (type) {
case "log":
prefix = "LOG";
style =
"font-weight: bold; background: #FFF; color: black; padding: 2px 4px; border-radius: 2px;";
break;
case "success":
prefix = "GOD";
style =
"font-weight: bold; background: #30FF97; color: black; padding: 2px 4px; border-radius: 2px;";
break;
case "warn":
prefix = "WAR";
style =
"font-weight: bold; background: #FFD700; color: black; padding: 2px 4px; border-radius: 2px;";
break;
case "error":
prefix = "ERR";
style =
"font-weight: bold; background: #FF3232; color: black; padding: 2px 4px; border-radius: 2px;";
}

const log = "%c" + prefix + "%c " + message;
console.log(log, style, "background: transparent; color: white;");
testLog(message, type);
};

export default function ConsoleLogger() {
const [logs, setLogs] = React.useState<Log[]>([]);

Expand Down Expand Up @@ -188,7 +222,6 @@ export default function ConsoleLogger() {
const storedObjs = await AsyncStorage.getItem("objs");
if (storedObjs) {
setObjs(JSON.parse(storedObjs));
testLog("a", "success");
console.log(
"%cGOD%cAll is ok%c Objectives (OBJS) fetched and parsed!",
"font-weight: bold; background: #30FF97; color: black; padding: 2px 4px; border-radius: 2px;",
Expand Down
58 changes: 12 additions & 46 deletions app/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Button from "@/components/Buttons";
import AsyncStorage from "@react-native-async-storage/async-storage";
import BottomNav from "@/components/BottomNav";
import GapView from "@/components/GapView";
import { testLog } from "./DeveloperInterface";
import { termLog, testLog } from "./DeveloperInterface";

// Creamos los estilos
const styles = Native.StyleSheet.create({
Expand Down Expand Up @@ -49,32 +49,17 @@ export default function Profile() {
if (olduname !== uname) {
await AsyncStorage.setItem("uname", uname);
const log = `Changed your username to ${uname}`;
console.log(
"%cGOD%cAll is ok%c " + log,
"font-weight: bold; background: #30FF97; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #30FF97;"
);
termLog(log, "success");
setTimeout(() => {
Router.router.replace("/");
}, 25);
} else {
const log = `Your username is already ${uname}! Can't change it to that`;
console.log(
"%cWOR%cDev error%c " + log,
"font-weight: bold; background: #FFD700; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #FFD700;"
);
termLog(log, "error");
}
} catch (e) {
const log = "Could not handleUnameBtnClick() due to error: " + e;
console.log(
"%cWOR%cDev error%c " + log,
"font-weight: bold; background: #FFD700; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #FFD700;"
);
termLog(log, "error");
}
};

Expand All @@ -87,29 +72,17 @@ export default function Profile() {
await AsyncStorage.getItem("uname");
if (uname) {
setUsername(uname);
console.log(
"%cGOD%cAll is ok%c Username (uname) fetched!",
"font-weight: bold; background: #30FF97; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #30FF97;"
);
termLog("Username (uname) fetched!", "success");
} else {
console.log(
"%cWOR%cDev error%c Username could not be fetched due to an error!",
"font-weight: bold; background: #FFD700; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #FFD700;"
termLog(
"Username could not be fetched due to an error!",
"error"
);
}
} catch (e) {
const log =
"Could not fetch username (uname) due to error: " + e;
console.log(
"%cWOR%cDev error%c " + log,
"font-weight: bold; background: #FFD700; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #FFD700;"
);
termLog(log, "error");
}
};

Expand Down Expand Up @@ -137,12 +110,7 @@ export default function Profile() {
const log =
"Got an error checking if the user wants to use Dev interface: " +
e;
console.log(
"%cWOR%cDev error%c " + log,
"font-weight: bold; background: #FFD700; color: black; padding: 2px 4px; border-radius: 2px;",
"font-weight: bold; background: white; color: black; padding: 2px 4px; border-radius: 2px;",
"color: #FFD700;"
);
termLog(log, "warn");
}
};
checkForDev();
Expand All @@ -158,12 +126,11 @@ export default function Profile() {
const enableDevInterface = async function () {
try {
await AsyncStorage.setItem("useDevTools", "true");
testLog("ENABLED DEV INTERFACE", "log");
termLog("ENABLED DEV INTERFACE", "log");
Router.router.navigate("/");
} catch (e) {
const log = "ERROR ENABLING DEV INTERFACE: " + e;
testLog(log, "error");
console.error(log);
termLog(log, "error");
Router.router.navigate("/DeveloperInterface"); // lol. if cant enable, at least go to page with logs to see the error.
}
};
Expand All @@ -176,7 +143,6 @@ export default function Profile() {
} catch (e) {
const log = "ERROR DISABLING DEV INTERFACE: " + e;
testLog(log, "error");
console.error(log);
Router.router.navigate("/DeveloperInterface"); // lol. if cant disable, at least go to page with logs to see the error.
}
};
Expand Down
14 changes: 11 additions & 3 deletions app/Sessions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import BetterText from "@/components/BetterText";
import AsyncStorage from "@react-native-async-storage/async-storage";
import Ionicons from "@expo/vector-icons/MaterialIcons";
import GapView from "@/components/GapView";
import { termLog } from "./DeveloperInterface";

// TypeScript, supongo.
interface Objective {
Expand All @@ -33,14 +34,21 @@ export default function Sessions() {
if (storedObjs) {
const parsedObjs: Objective[] = JSON.parse(storedObjs);
setObjs(parsedObjs);
console.log("OBJS fetched");
termLog("Objectives (OBJS) fetched", "success");
} else {
await AsyncStorage.setItem("objs", JSON.stringify([]));
console.error("Could not get OBJS fetched");
termLog(
"Could not get Objectives (OBJS) fetched. Setting them to an empty object.",
"error"
);
setObjs([]);
}
} catch (e) {
console.error("Could not get OBJS fetched: " + e);
const log =
"Could not get Objectives (OBJS) fetched due to error: " +
e +
". Setting them to an empty object.";
termLog(log, "warn");
setObjs([]);
}
};
Expand Down
Loading

0 comments on commit c3147cb

Please sign in to comment.