fixed useQuery some code style and unused hooks

This commit is contained in:
Clément Le Bihan
2023-04-15 21:30:11 +02:00
parent 9d8bb499ba
commit fe782a4f94
7 changed files with 77 additions and 75 deletions
+1 -1
View File
@@ -172,7 +172,7 @@ export default class API {
data: { data: {
gamesPlayed: user.partyPlayed as number, gamesPlayed: user.partyPlayed as number,
xp: 0, xp: 0,
createdAt: "9 avril 2023", createdAt: new Date("2023-04-09T00:00:00.000Z"),
avatar: avatar:
"https://imgs.search.brave.com/RnQpFhmAFvuQsN_xTw7V-CN61VeHDBg2tkEXnKRYHAE/rs:fit:768:512:1/g:ce/aHR0cHM6Ly96b29h/c3Ryby5jb20vd3At/Y29udGVudC91cGxv/YWRzLzIwMjEvMDIv/Q2FzdG9yLTc2OHg1/MTIuanBn", "https://imgs.search.brave.com/RnQpFhmAFvuQsN_xTw7V-CN61VeHDBg2tkEXnKRYHAE/rs:fit:768:512:1/g:ce/aHR0cHM6Ly96b29h/c3Ryby5jb20vd3At/Y29udGVudC91cGxv/YWRzLzIwMjEvMDIv/Q2FzdG9yLTc2OHg1/MTIuanBn",
}, },
+7 -15
View File
@@ -48,22 +48,14 @@ const ElementList = ({ elements, style }: ElementListProps) => {
return ( return (
<Column style={[style, elementStyle]}> <Column style={[style, elementStyle]}>
{(() => { {elements.map((element, index, __) => (
const elementsWithDividers = []; <Box key={element.title}>
for (let i = 0; i < elements.length; i++) { <Element {...element} />
elementsWithDividers.push( { index < elements.length - 1 &&
<Box key={elements[i]?.title}> <Divider />
<Element {...elements[i]} />
</Box>
);
if (i < elements.length - 1) {
elementsWithDividers.push(
<Divider key={elements[i]?.title + "Divider"} />
);
} }
} </Box>
return elementsWithDividers; ))}
})()}
</Column> </Column>
); );
}; };
+40 -34
View File
@@ -1,6 +1,12 @@
import { Select, Switch, Text, Icon, Row, Slider } from "native-base"; import { Select, Switch, Text, Icon, Row, Slider } from "native-base";
import { MaterialIcons } from "@expo/vector-icons"; import { MaterialIcons } from "@expo/vector-icons";
export type ElementType = "custom" | "default" | "text" | "toggle" | "dropdown" | "range"; export type ElementType =
| "custom"
| "default"
| "text"
| "toggle"
| "dropdown"
| "range";
export type DropdownOption = { export type DropdownOption = {
label: string; label: string;
@@ -26,13 +32,13 @@ export type ElementDropdownProps = {
}; };
export type ElementRangeProps = { export type ElementRangeProps = {
onChange: (value: number) => void; onChange: (value: number) => void;
value: number; value: number;
defaultValue?: number; defaultValue?: number;
min: number; min: number;
max: number; max: number;
step?: number; step?: number;
} };
export const getElementTextNode = ( export const getElementTextNode = (
{ text, onPress }: ElementTextProps, { text, onPress }: ElementTextProps,
@@ -71,7 +77,7 @@ export const getElementToggleNode = (
) => { ) => {
return ( return (
<Switch <Switch
// the callback is called by the Pressable component wrapping the entire row // the callback is called by the Pressable component wrapping the entire row
isChecked={value ?? false} isChecked={value ?? false}
defaultIsChecked={defaultValue} defaultIsChecked={defaultValue}
disabled={disabled} disabled={disabled}
@@ -103,29 +109,29 @@ export const getElementDropdownNode = (
}; };
export const getElementRangeNode = ( export const getElementRangeNode = (
{ onChange, value, defaultValue, min, max, step }: ElementRangeProps, { onChange, value, defaultValue, min, max, step }: ElementRangeProps,
disabled: boolean, disabled: boolean,
title: string, title: string
) => { ) => {
return ( return (
<Slider <Slider
// this is a hot fix for now but ideally this input should be managed // this is a hot fix for now but ideally this input should be managed
// by the value prop and not the defaultValue prop but it requires the // by the value prop and not the defaultValue prop but it requires the
// caller to manage the state of the continuous value which is not ideal // caller to manage the state of the continuous value which is not ideal
defaultValue={value} defaultValue={value}
// defaultValue={defaultValue} // defaultValue={defaultValue}
minValue={min} minValue={min}
maxValue={max} maxValue={max}
step={step} step={step}
isDisabled={disabled} isDisabled={disabled}
onChangeEnd={onChange} onChangeEnd={onChange}
accessibilityLabel={`Slider for ${title}`} accessibilityLabel={`Slider for ${title}`}
width="200" width="200"
> >
<Slider.Track> <Slider.Track>
<Slider.FilledTrack /> <Slider.FilledTrack />
</Slider.Track> </Slider.Track>
<Slider.Thumb /> <Slider.Thumb />
</Slider> </Slider>
) );
} };
+1 -1
View File
@@ -2,7 +2,7 @@ interface UserData {
gamesPlayed: number; gamesPlayed: number;
xp: number; xp: number;
avatar: string | undefined; avatar: string | undefined;
createdAt: string; createdAt: Date;
} }
export default UserData; export default UserData;
+1 -1
View File
@@ -35,7 +35,7 @@ const handleSignup = async (username: string, password: string, email: string, a
const AuthenticationView = ({ route, navigation }: any) => { const AuthenticationView = ({ route, navigation }: any) => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const isSignup = route.params?.isSignup ?? false; const isSignup = route.params?.isSignup ?? false;
const [mode, setMode] = React.useState((isSignup ? "signup" : "signin") as "signin" | "signup"); const [mode, setMode] = React.useState<"signin" | "signup">(isSignup ? "signup" : "signin");
return ( return (
<Center style={{ flex: 1 }}> <Center style={{ flex: 1 }}>
+13 -17
View File
@@ -23,30 +23,26 @@ import TextButton from "../../components/TextButton";
import LoadingComponent from "../../components/Loading"; import LoadingComponent from "../../components/Loading";
import ElementList from "../../components/GtkUI/ElementList"; import ElementList from "../../components/GtkUI/ElementList";
import { translate } from "../../i18n/i18n"; import { translate } from "../../i18n/i18n";
import { useQuery } from "react-query";
const getInitials = (name: string) => { const getInitials = (name: string) => {
const names = name.split(" "); return name.split(" ").map((n) => n[0]).join("");
if (names.length === 1) return names[0]?.charAt(0);
let initials = [];
for (let i = 0; i < names.length; i++) {
initials.push(names[i]?.charAt(0));
}
return initials.join("");
}; };
const ProfileSettings = ({ navigation }: { navigation: any }) => { const ProfileSettings = ({ navigation }: { navigation: any }) => {
const [user, setUser] = useState<User | null>(null); const userQuery = useQuery(["appSettings", "user"], API.getUserInfo);
const dispatch = useDispatch(); const dispatch = useDispatch();
const [toggle, setToggle] = useState(true); const user = userQuery.data;
const [selectValue, setSelectValue] = useState("fr");
useEffect(() => { if (userQuery.isError) {
API.getUserInfo().then((user) => { return (
setUser(user); <Center style={{ flex: 1 }}>
}); <Text>{translate("errorLoadingUser")}</Text>
}, []); </Center>
);
}
if (!user) { if (!userQuery || userQuery.isLoading || !userQuery.data) {
return ( return (
<Center style={{ flex: 1 }}> <Center style={{ flex: 1 }}>
<LoadingComponent /> <LoadingComponent />
@@ -136,7 +132,7 @@ const ProfileSettings = ({ navigation }: { navigation: any }) => {
helperText: helperText:
"La date de création est actuellement arbitraire car le serveur ne retourne pas cette information", "La date de création est actuellement arbitraire car le serveur ne retourne pas cette information",
data: { data: {
text: user.data.createdAt, text: user.data.createdAt.toLocaleDateString(),
}, },
}, },
{ {
+14 -6
View File
@@ -18,6 +18,7 @@ import NotificationsView from './NotificationView';
import PrivacyView from './PrivacyView'; import PrivacyView from './PrivacyView';
import PreferencesView from './PreferencesView'; import PreferencesView from './PreferencesView';
import GuestToUserView from './GuestToUserView'; import GuestToUserView from './GuestToUserView';
import { useQuery } from 'react-query';
import API, { APIError } from '../../API'; import API, { APIError } from '../../API';
import User from '../../models/User'; import User from '../../models/User';
@@ -116,13 +117,20 @@ export const PianoSettingsView = ({navigation}) => {
const TabRow = createTabRowNavigator(); const TabRow = createTabRowNavigator();
const SetttingsNavigator = () => { const SetttingsNavigator = () => {
const [user, setUser] = React.useState<null | User>(null); const userQuery = useQuery(["appSettings", 'user'], API.getUserInfo);
const user = userQuery.data;
React.useEffect(() => { if (userQuery.isError) {
API.getUserInfo().then((user) => { user.isGuest = false;
setUser(user); }
});
}, []); if (userQuery.isLoading) {
return (
<Center style={{ flex: 1}}>
<Text>Loading...</Text>
</Center>
)
}
return ( return (
<TabRow.Navigator initialRouteName='InternalDefault'> <TabRow.Navigator initialRouteName='InternalDefault'>