Skip to content

Commit

Permalink
Refactor useTabBarConfig and gql queries (#100)
Browse files Browse the repository at this point in the history
* Refactor useTabBarConfig and gql queries

* Add URL override functionality
  • Loading branch information
jthoward64 authored Apr 1, 2024
1 parent a78ba3d commit 5b99f45
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 101 deletions.
9 changes: 2 additions & 7 deletions packages/common/lib/graphql-client-public/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const documents = {
"\n fragment NotificationFragment on NotificationResource {\n uuid\n title\n body\n url\n }\n": types.NotificationFragmentFragmentDoc,
"\n fragment NotificationDeliveryFragment on NotificationDeliveryResource {\n uuid\n sentAt\n notification {\n ...NotificationFragment\n }\n }\n": types.NotificationDeliveryFragmentFragmentDoc,
"\n query useAllowedLoginTypes {\n activeConfiguration(key: \"ALLOWED_LOGIN_TYPES\") {\n data {\n ...SimpleConfig\n }\n }\n }\n": types.UseAllowedLoginTypesDocument,
"\n query useMarathonStart {\n activeConfiguration(key: \"MARATHON_START\") {\n data {\n ...SimpleConfig\n }\n }\n }\n": types.UseMarathonStartDocument,
"\n query useMarathonEnd {\n activeConfiguration(key: \"MARATHON_END\") {\n data {\n ...SimpleConfig\n }\n }\n }\n": types.UseMarathonEndDocument,
"\n query MarathonTime {\n nextMarathon {\n startDate\n endDate\n }\n }\n": types.MarathonTimeDocument,
"\n query useTabBarConfig {\n activeConfiguration(key: \"TAB_BAR_CONFIG\") {\n data {\n ...SimpleConfig\n }\n }\n }\n": types.UseTabBarConfigDocument,
"\n query AuthState {\n me {\n data {\n uuid\n }\n }\n loginState {\n role {\n dbRole\n committeeIdentifier\n committeeRole\n }\n loggedIn\n authSource\n }\n }\n": types.AuthStateDocument,
"\n mutation SetDevice($input: RegisterDeviceInput!) {\n registerDevice(input: $input) {\n ok\n }\n }\n": types.SetDeviceDocument,
Expand Down Expand Up @@ -81,11 +80,7 @@ export function graphql(source: "\n query useAllowedLoginTypes {\n activeCon
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(source: "\n query useMarathonStart {\n activeConfiguration(key: \"MARATHON_START\") {\n data {\n ...SimpleConfig\n }\n }\n }\n"): (typeof documents)["\n query useMarathonStart {\n activeConfiguration(key: \"MARATHON_START\") {\n data {\n ...SimpleConfig\n }\n }\n }\n"];
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(source: "\n query useMarathonEnd {\n activeConfiguration(key: \"MARATHON_END\") {\n data {\n ...SimpleConfig\n }\n }\n }\n"): (typeof documents)["\n query useMarathonEnd {\n activeConfiguration(key: \"MARATHON_END\") {\n data {\n ...SimpleConfig\n }\n }\n }\n"];
export function graphql(source: "\n query MarathonTime {\n nextMarathon {\n startDate\n endDate\n }\n }\n"): (typeof documents)["\n query MarathonTime {\n nextMarathon {\n startDate\n endDate\n }\n }\n"];
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
Expand Down
18 changes: 3 additions & 15 deletions packages/common/lib/graphql-client-public/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1755,21 +1755,10 @@ export type UseAllowedLoginTypesQuery = { readonly __typename?: 'Query', readonl
& { ' $fragmentRefs'?: { 'SimpleConfigFragment': SimpleConfigFragment } }
) } };

export type UseMarathonStartQueryVariables = Exact<{ [key: string]: never; }>;
export type MarathonTimeQueryVariables = Exact<{ [key: string]: never; }>;


export type UseMarathonStartQuery = { readonly __typename?: 'Query', readonly activeConfiguration: { readonly __typename?: 'GetConfigurationByUuidResponse', readonly data: (
{ readonly __typename?: 'ConfigurationResource' }
& { ' $fragmentRefs'?: { 'SimpleConfigFragment': SimpleConfigFragment } }
) } };

export type UseMarathonEndQueryVariables = Exact<{ [key: string]: never; }>;


export type UseMarathonEndQuery = { readonly __typename?: 'Query', readonly activeConfiguration: { readonly __typename?: 'GetConfigurationByUuidResponse', readonly data: (
{ readonly __typename?: 'ConfigurationResource' }
& { ' $fragmentRefs'?: { 'SimpleConfigFragment': SimpleConfigFragment } }
) } };
export type MarathonTimeQuery = { readonly __typename?: 'Query', readonly nextMarathon?: { readonly __typename?: 'MarathonResource', readonly startDate: Date | string, readonly endDate: Date | string } | null };

export type UseTabBarConfigQueryVariables = Exact<{ [key: string]: never; }>;

Expand Down Expand Up @@ -1885,8 +1874,7 @@ export const ScoreBoardFragmentFragmentDoc = {"kind":"Document","definitions":[{
export const HighlightedTeamFragmentFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"HighlightedTeamFragment"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"TeamResource"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"uuid"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"legacyStatus"}},{"kind":"Field","name":{"kind":"Name","value":"type"}}]}}]} as unknown as DocumentNode<HighlightedTeamFragmentFragment, unknown>;
export const MyTeamFragmentFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"MyTeamFragment"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"TeamResource"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"uuid"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"totalPoints"}},{"kind":"Field","name":{"kind":"Name","value":"pointEntries"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"personFrom"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"uuid"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"linkblue"}}]}},{"kind":"Field","name":{"kind":"Name","value":"points"}}]}},{"kind":"Field","name":{"kind":"Name","value":"members"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"position"}},{"kind":"Field","name":{"kind":"Name","value":"person"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"linkblue"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}}]}}]}}]} as unknown as DocumentNode<MyTeamFragmentFragment, unknown>;
export const UseAllowedLoginTypesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"useAllowedLoginTypes"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"activeConfiguration"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"key"},"value":{"kind":"StringValue","value":"ALLOWED_LOGIN_TYPES","block":false}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"data"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SimpleConfig"}}]}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SimpleConfig"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"ConfigurationResource"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"uuid"}},{"kind":"Field","name":{"kind":"Name","value":"key"}},{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]} as unknown as DocumentNode<UseAllowedLoginTypesQuery, UseAllowedLoginTypesQueryVariables>;
export const UseMarathonStartDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"useMarathonStart"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"activeConfiguration"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"key"},"value":{"kind":"StringValue","value":"MARATHON_START","block":false}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"data"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SimpleConfig"}}]}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SimpleConfig"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"ConfigurationResource"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"uuid"}},{"kind":"Field","name":{"kind":"Name","value":"key"}},{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]} as unknown as DocumentNode<UseMarathonStartQuery, UseMarathonStartQueryVariables>;
export const UseMarathonEndDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"useMarathonEnd"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"activeConfiguration"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"key"},"value":{"kind":"StringValue","value":"MARATHON_END","block":false}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"data"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SimpleConfig"}}]}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SimpleConfig"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"ConfigurationResource"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"uuid"}},{"kind":"Field","name":{"kind":"Name","value":"key"}},{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]} as unknown as DocumentNode<UseMarathonEndQuery, UseMarathonEndQueryVariables>;
export const MarathonTimeDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"MarathonTime"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"nextMarathon"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"startDate"}},{"kind":"Field","name":{"kind":"Name","value":"endDate"}}]}}]}}]} as unknown as DocumentNode<MarathonTimeQuery, MarathonTimeQueryVariables>;
export const UseTabBarConfigDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"useTabBarConfig"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"activeConfiguration"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"key"},"value":{"kind":"StringValue","value":"TAB_BAR_CONFIG","block":false}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"data"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SimpleConfig"}}]}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SimpleConfig"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"ConfigurationResource"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"uuid"}},{"kind":"Field","name":{"kind":"Name","value":"key"}},{"kind":"Field","name":{"kind":"Name","value":"value"}}]}}]} as unknown as DocumentNode<UseTabBarConfigQuery, UseTabBarConfigQueryVariables>;
export const AuthStateDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"AuthState"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"me"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"data"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"uuid"}}]}}]}},{"kind":"Field","name":{"kind":"Name","value":"loginState"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"role"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"dbRole"}},{"kind":"Field","name":{"kind":"Name","value":"committeeIdentifier"}},{"kind":"Field","name":{"kind":"Name","value":"committeeRole"}}]}},{"kind":"Field","name":{"kind":"Name","value":"loggedIn"}},{"kind":"Field","name":{"kind":"Name","value":"authSource"}}]}}]}}]} as unknown as DocumentNode<AuthStateQuery, AuthStateQueryVariables>;
export const SetDeviceDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"SetDevice"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"input"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"RegisterDeviceInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"registerDevice"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"input"},"value":{"kind":"Variable","name":{"kind":"Name","value":"input"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"ok"}}]}}]}}]} as unknown as DocumentNode<SetDeviceMutation, SetDeviceMutationVariables>;
Expand Down
10 changes: 10 additions & 0 deletions packages/mobile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { LogBox } from "react-native";
import "react-native-url-polyfill/auto";

import App from "./App";
import { overrideApiBaseUrl } from "./src/common/apiUrl";
import { Logger } from "./src/common/logger/Logger";

Logger.debug("Starting app");
Expand Down Expand Up @@ -40,6 +41,15 @@ if (isDevelopmentBuild()) {
console.log(values);
},
},
{
name: "Override url",
callback: async () => {
Logger.log("Overriding url");
overrideApiBaseUrl(
prompt("Enter the url to override or blank for default")
);
},
},
]).catch((error) => console.error(error));
}

Expand Down
19 changes: 18 additions & 1 deletion packages/mobile/src/common/apiUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,22 @@
// : "http://localhost:8000";
// }

export const API_BASE_URL =
import { reloadAsync } from "expo-updates";

import { Logger } from "./logger/Logger";

export let API_BASE_URL =
process.env.EXPO_PUBLIC_API_BASE_URL || "https://app.danceblue.org";

export function overrideApiBaseUrl(newUrl: string) {
API_BASE_URL =
newUrl ||
process.env.EXPO_PUBLIC_API_BASE_URL ||
"https://app.danceblue.org";
reloadAsync().catch((error: unknown) => {
Logger.error("Failed to reload app", {
error,
source: "overrideApiBaseUrl",
});
});
}
105 changes: 38 additions & 67 deletions packages/mobile/src/common/hooks/useMarathonTime.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
import { SimpleConfigFragment } from "@common/fragments/Configuration";
import { log, logError } from "@common/logging";
import {
getFragmentData,
graphql,
} from "@ukdanceblue/common/dist/graphql-client-public";
import { Logger } from "@common/logger/Logger";
import { dateTimeFromSomething } from "@ukdanceblue/common";
import { graphql } from "@ukdanceblue/common/dist/graphql-client-public";
import { DateTime } from "luxon";
import { useEffect, useMemo } from "react";
import { useQuery } from "urql";

export interface MarathonTime {
startTime: DateTime,
endTime: DateTime,
startTime: DateTime;
endTime: DateTime;
}

const useMarathonStartQuery = graphql(/* GraphQL */ `
query useMarathonStart {
activeConfiguration(key: "MARATHON_START") {
data {
...SimpleConfig
}
}
}
`);

const useMarathonEndQuery = graphql(/* GraphQL */ `
query useMarathonEnd {
activeConfiguration(key: "MARATHON_END") {
data {
...SimpleConfig
}
const marathonTimeQuery = graphql(/* GraphQL */ `
query MarathonTime {
nextMarathon {
startDate
endDate
}
}
`);
Expand All @@ -37,68 +23,53 @@ export function useMarathonTime(): {
timesLoading: boolean;
marathonTime: MarathonTime;
} {
const [{ data: startData, fetching: startFetch, error: startError }] = useQuery({
query: useMarathonStartQuery,
});

const startValue = getFragmentData(
SimpleConfigFragment,
startData?.activeConfiguration.data
);

const [{ data: endData, fetching: endFetch, error: endError }] = useQuery({
query: useMarathonEndQuery,
const [{ data, fetching, error }] = useQuery({
query: marathonTimeQuery,
});

const endValue = getFragmentData(
SimpleConfigFragment,
endData?.activeConfiguration.data
);

useEffect(() => {
if (startError) {
logError(startError);
}
if (endError) {
logError(endError);
if (error) {
Logger.error("Failed to fetch marathon start time", {
error,
source: "useMarathonTime",
});
}
});

const determineMarathonIntervals = useMemo(() => {
const marathonInterval = useMemo(() => {
let startTime: DateTime = DateTime.fromMillis(0);
let endTime: DateTime = DateTime.fromMillis(0);

try {
if (startValue) {
const parsed = startValue.value;
startTime = DateTime.fromISO(parsed);
if (data?.nextMarathon) {
startTime = dateTimeFromSomething(data.nextMarathon.startDate);
if (!startTime.isValid) {
log(`Unrecognized marathon start time: ${startTime.toString()}`, "warn");
Logger.warn(
`Unrecognized marathon start time: ${startTime.toString()}`,
{
source: "useMarathonTime",
}
);
}
}

if (endValue) {
const parsed = endValue.value;
endTime = DateTime.fromISO(parsed);
endTime = dateTimeFromSomething(data.nextMarathon.endDate);
if (!endTime.isValid) {
log(`Unrecognized marathon end time: ${endTime.toString()}`, "warn");
Logger.warn(`Unrecognized marathon end time: ${endTime.toString()}`, {
source: "useMarathonTime",
});
}
}
} catch (error) {
if (error instanceof Error) {
logError(error);
} else {
log(String(error), "error");
}
Logger.error("Failed to determine marathon intervals", {
error,
source: "useMarathonTime",
});
}

return({startTime, endTime} as MarathonTime);

}, [startValue, endValue]);

return { startTime, endTime } as MarathonTime;
}, [data?.nextMarathon]);

return {
timesLoading: (startFetch || endFetch),
marathonTime: determineMarathonIntervals
timesLoading: fetching,
marathonTime: marathonInterval,
};
}
Loading

0 comments on commit 5b99f45

Please sign in to comment.