From be045fafdedce99729e0e321f3fa92377e71e738 Mon Sep 17 00:00:00 2001 From: Violet Caulfield Date: Tue, 15 Oct 2024 16:54:08 -0500 Subject: [PATCH] so I do need it this way --- api/mutators/functions/storage.ts | 15 ++++++ api/mutators/storage.ts | 52 +------------------ components/Login/helpers/server-address.tsx | 34 ++++++++++++ .../Login/helpers/server-authentication.tsx | 9 ++++ 4 files changed, 59 insertions(+), 51 deletions(-) diff --git a/api/mutators/functions/storage.ts b/api/mutators/functions/storage.ts index 2f32b30b..b26d26fb 100644 --- a/api/mutators/functions/storage.ts +++ b/api/mutators/functions/storage.ts @@ -1,8 +1,23 @@ +import { Jellyfin } from "@jellyfin/sdk/lib/jellyfin"; import { fetchServer } from "../../queries/functions/storage"; import { JellyfinCredentials } from "../../types/jellyfin-credentials"; import * as Keychain from "react-native-keychain" +import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api"; +import { client } from "../../queries"; +export const serverMutation = async (serverUrl: string) => { + + console.log("Mutating server URL"); + if (!!!serverUrl) + throw Error("Server URL is empty") + + let jellyfin = new Jellyfin(client); + let api = jellyfin.createApi(serverUrl); + + console.log(`Created API client for ${api.basePath}`) + return await getSystemApi(api).getPublicSystemInfo() +} export const mutateServerCredentials = async (credentials: JellyfinCredentials) => { return Keychain.setInternetCredentials((await fetchServer()).url, credentials.username, credentials.accessToken!); diff --git a/api/mutators/storage.ts b/api/mutators/storage.ts index c068189f..4bfc981c 100644 --- a/api/mutators/storage.ts +++ b/api/mutators/storage.ts @@ -1,62 +1,12 @@ import { useMutation } from "@tanstack/react-query"; import { MutationKeys } from "../../enums/mutation-keys"; -import AsyncStorage from "@react-native-async-storage/async-storage"; -import { AsyncStorageKeys } from "../../enums/async-storage-keys"; -import { Jellyfin } from "@jellyfin/sdk"; -import { client } from "../queries"; -import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api" import { JellyfinCredentials } from "../types/jellyfin-credentials"; import { mutateServerCredentials } from "./functions/storage"; -import { JellifyServer } from "../../types/JellifyServer"; - -export const clearServer = useMutation({ - mutationFn: async () => { - await AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, ""); - } -}) - -export const serverUrlMutation = useMutation({ - mutationFn: async (serverUrl: string) => { - - console.log("Mutating server URL"); - - if (!!!serverUrl) - throw Error("Server URL is empty") - - let jellyfin = new Jellyfin(client); - let api = jellyfin.createApi(serverUrl); - - console.log(`Created API client for ${api.basePath}`) - return await getSystemApi(api).getPublicSystemInfo() - }, - onSuccess: (publicSystemInfoResponse, serverUrl, context) => { - if (!!!publicSystemInfoResponse.data.Version) - throw new Error("Jellyfin instance did not respond"); - - console.debug("REMOVE THIS::onSuccess variable", serverUrl); - console.log(`Connected to Jellyfin ${publicSystemInfoResponse.data.Version!}`); - - // TODO: Store these along side address - // TODO: Rename url to address - - let jellifyServer: JellifyServer = { - url: serverUrl, - name: publicSystemInfoResponse.data.ServerName!, - version: publicSystemInfoResponse.data.Version!, - startUpComplete: publicSystemInfoResponse.data.StartupWizardCompleted! - } - return AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, JSON.stringify(jellifyServer)); - }, - onError: async (error: Error) => { - console.error("An error occurred connecting to the Jellyfin instance", error); - await AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, ""); - } -}); export const credentials = useMutation({ mutationKey: [MutationKeys.Credentials], mutationFn: async (credentials: JellyfinCredentials) => { - mutateServerCredentials(credentials) + return mutateServerCredentials(credentials) }, }); \ No newline at end of file diff --git a/components/Login/helpers/server-address.tsx b/components/Login/helpers/server-address.tsx index 0853d70e..63e216e8 100644 --- a/components/Login/helpers/server-address.tsx +++ b/components/Login/helpers/server-address.tsx @@ -2,6 +2,14 @@ import React, { useState } from "react"; import _ from "lodash"; import { serverUrlMutation } from "../../../api/mutators/storage"; import { Button, SafeAreaView, TextInput, useColorScheme, View } from "react-native"; +import { Jellyfin } from "@jellyfin/sdk"; +import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api"; +import AsyncStorage from "@react-native-async-storage/async-storage"; +import { useMutation } from "@tanstack/react-query"; +import { client } from "../../../api/queries"; +import { AsyncStorageKeys } from "../../../enums/async-storage-keys"; +import { JellifyServer } from "../../../types/JellifyServer"; +import { serverMutation } from "../../../api/mutators/functions/storage"; export default function ServerAddress(): React.JSX.Element { @@ -9,6 +17,32 @@ export default function ServerAddress(): React.JSX.Element { const isDarkMode = useColorScheme() === 'dark'; + const useServerMutation = useMutation({ + mutationFn: serverMutation, + onSuccess: (publicSystemInfoResponse, serverUrl, context) => { + if (!!!publicSystemInfoResponse.data.Version) + throw new Error("Jellyfin instance did not respond"); + + console.debug("REMOVE THIS::onSuccess variable", serverUrl); + console.log(`Connected to Jellyfin ${publicSystemInfoResponse.data.Version!}`); + + // TODO: Store these along side address + // TODO: Rename url to address + + let jellifyServer: JellifyServer = { + url: serverUrl, + name: publicSystemInfoResponse.data.ServerName!, + version: publicSystemInfoResponse.data.Version!, + startUpComplete: publicSystemInfoResponse.data.StartupWizardCompleted! + } + return AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, JSON.stringify(jellifyServer)); + }, + onError: async (error: Error) => { + console.error("An error occurred connecting to the Jellyfin instance", error); + return await AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, ""); + } + }); + return ( { + return await AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, ""); + } + }) + return (