From aebc1e4b7992eeca7aed479c936e36fd0dd43364 Mon Sep 17 00:00:00 2001 From: Netcup Gituser Date: Sat, 9 Dec 2023 23:57:16 +0100 Subject: [PATCH] login and sign up with account --- src/components/MyButton.tsx | 33 +++- src/components/MyToast.tsx | 131 +++++++++++++++ src/components/MyTouchableOpacity.tsx | 27 +-- src/components/map/DisplayMarkerList.tsx | 11 +- src/components/map/EventMarker.tsx | 162 +++++++++++++----- src/components/map/cluster/getData.ts | 129 ++++++-------- src/components/map/types.ts | 32 ++++ src/configs/appNonSaveVar.ts | 6 +- src/configs/appNonSaveVarReducer.ts | 8 +- src/configs/appVar.ts | 22 ++- src/configs/appVarReducer.ts | 6 +- src/configs/types.ts | 13 +- src/helper/animations.tsx | 1 - src/helper/base64.ts | 9 + src/helper/request.ts | 69 +++++--- src/helper/storage/bdm/getDB.ts | 2 +- src/helper/storage/bdm/schemas/users.ts | 28 ++-- src/lang/en.ts | 2 +- src/navigation/navigation.tsx | 2 + src/navigation/registration/registration.tsx | 31 ++-- src/navigation/tabs/main/MapTab.tsx | 14 ++ src/pages/appStart/StartHelper.tsx | 9 +- src/pages/map/map.tsx | 18 +- src/pages/profile/profile.tsx | 57 +++++-- src/pages/welcome/login/login.tsx | 168 ++++++++++++------- src/pages/welcome/signUp/signUp.tsx | 101 +++++++++-- src/user/MyUserManager.ts | 58 +++---- src/user/UserManager.ts | 103 ++++++------ src/user/types.ts | 19 +-- 29 files changed, 832 insertions(+), 439 deletions(-) create mode 100644 src/components/MyToast.tsx create mode 100644 src/components/map/types.ts create mode 100644 src/helper/base64.ts diff --git a/src/components/MyButton.tsx b/src/components/MyButton.tsx index 04f3381..c65d963 100644 --- a/src/components/MyButton.tsx +++ b/src/components/MyButton.tsx @@ -10,16 +10,22 @@ import LinearGradient from 'react-native-linear-gradient'; import {MyIcon} from './MyIcon'; import {useSelector} from 'react-redux'; import {RootState} from '@redux/store'; -import {Text} from '@gluestack-ui/themed'; +import {Spinner, Text} from '@gluestack-ui/themed'; import {MyTouchableOpacity} from './MyTouchableOpacity'; +import {TouchableOpacityProps} from 'react-native'; -interface MyImageButtonProps { +interface MyImageButtonProps extends TouchableOpacityProps { image: ImageSourcePropType; imageStyle?: StyleProp; text: string; } -export function MyImageButton({image, imageStyle, text}: MyImageButtonProps) { +export function MyImageButton({ + image, + imageStyle, + text, + ...rest +}: MyImageButtonProps) { const currentTheme = useSelector( (state: RootState) => state.nonSaveVariables.theme.colors, ); @@ -32,7 +38,8 @@ export function MyImageButton({image, imageStyle, text}: MyImageButtonProps) { padding: 10, flexDirection: 'row', borderRadius: 10, - }}> + }} + {...rest}> void; disabled?: boolean; + isLoading?: boolean; } export function MyButton({ @@ -61,15 +69,21 @@ export function MyButton({ text, onPress, disabled, + isLoading, + ...rest }: MyButtonProps) { const currentTheme = useSelector( (state: RootState) => state.nonSaveVariables.theme, ); const ButtonText = () => ( - - {text} - + + {isLoading && } + + + {text} + + ); return ( @@ -80,8 +94,9 @@ export function MyButton({ borderRadius: 10, }, ]} - disabled={disabled} - onPress={onPress}> + disabled={disabled || isLoading} + onPress={onPress} + {...rest}> {type === 'primary' ? ( { + const toast = useToast(); + const ToastDetails = [ + { + title: 'Account verified', + variant: 'solid', + description: 'Thanks for signing up with us.', + isClosable: true, + }, + { + title: 'Something went wrong', + variant: 'subtle', + description: 'Please create a support ticket from the support page', + }, + { + title: 'Network connection restored', + variant: 'left-accent', + description: + 'This is to inform you that your network connectivity is restored', + isClosable: true, + }, + { + title: 'Invalid email address', + variant: 'top-accent', + description: 'Please enter a valid email address', + }, + { + title: 'Invalid email address', + variant: 'outline', + description: 'Please enter a valid email address', + }, + ]; +}; + +interface toastType { + id?: string; + action?: 'info' | 'error' | 'success' | 'warning' | 'muted' | undefined; + variant: 'solid' | 'outline' | 'accent' | undefined; + title: any; + description: any; + isClosable?: boolean; + rest: any; +} + +interface alertType extends toastType { + id: string; +} + +function showToast(toast: any, item: toastType) { + const ToastAlert = ({ + id, + action, + variant, + title, + description, + isClosable, + rest, + }: alertType) => ( + + + + + + + {title} + + + {isClosable ? ( + toast.close(id)} + /> + ) : /*} + _icon={{ + color: variant === 'solid' ? 'lightText' : 'darkText', + }} + onPress={() => toast.close(id)} + /> */ + null} + + + {description} + + + + ); + + toast.show({ + render: ({id}: {id: string}) => { + return ; + }, + }); +} + +export default showToast; diff --git a/src/components/MyTouchableOpacity.tsx b/src/components/MyTouchableOpacity.tsx index 81d8044..d9e5108 100644 --- a/src/components/MyTouchableOpacity.tsx +++ b/src/components/MyTouchableOpacity.tsx @@ -5,39 +5,20 @@ import { GestureResponderEvent, StyleProp, TouchableOpacity, + TouchableOpacityProps, ViewStyle, } from 'react-native'; import {useSelector} from 'react-redux'; -interface CustomTouchableProps { - children?: ReactNode; - onPress?: () => void; - onLongPress?: ((event: GestureResponderEvent) => void) | undefined; - style?: StyleProp; - disabled?: boolean; - accessibilityRole?: AccessibilityRole | undefined; -} +interface CustomTouchableProps extends TouchableOpacityProps {} -export function MyTouchableOpacity({ - children, - onPress, - onLongPress, - style, - disabled, - accessibilityRole, -}: CustomTouchableProps) { +export function MyTouchableOpacity({children, ...rest}: CustomTouchableProps) { const currentTheme = useSelector( (state: RootState) => state.nonSaveVariables.theme.opacity, ); return ( - + {children} ); diff --git a/src/components/map/DisplayMarkerList.tsx b/src/components/map/DisplayMarkerList.tsx index 07ec062..2d7a99a 100644 --- a/src/components/map/DisplayMarkerList.tsx +++ b/src/components/map/DisplayMarkerList.tsx @@ -1,25 +1,24 @@ import React from 'react'; import {View, Text, Image, StyleSheet} from 'react-native'; -import {PA_Point} from './cluster/getData'; import {MarkerView} from '@rnmapbox/maps'; import {useSelector} from 'react-redux'; import {RootState, store} from '@redux/store'; import CircleNumber from './cluster/Cluster'; import {EventMarker} from './EventMarker'; +import {PA_Point} from './types'; -function DisplayMarkerList(props: {markers: PA_Point[]}) { - const eventMapMarkers = props.markers; - /*const eventMapMarkers = useSelector( +function DisplayMarkerList() { + const eventMapMarkers = useSelector( (state: RootState) => state.nonSaveVariables.eventMapMarkers, - );*/ + ); const colors = store.getState().nonSaveVariables.theme.colors; return ( {eventMapMarkers.map((marker: PA_Point) => { - return marker.type === 'event' ? ( + return marker.type === 'event' || marker.type === 'eventStore' ? ( (); + const fadeAnim = React.useRef(new Animated.Value(0)).current; const fadeAnim2 = React.useRef(new Animated.Value(0.9)).current; @@ -39,9 +45,38 @@ export function EventMarker(props: {marker: PA_Point}) { }).start(); }, [fadeAnim2]); + return ( + + { + console.log('marker pressed'); + navigation.navigate('Home', { + screen: 'Map', + params: {screen: 'Event', params: {eventID: marker.id}}, + }); + }}> + {marker.theme === 1 ? ( + + ) : ( + + )} + + {/*{marker.type}*/} + + ); +} + +function getEventAvatars(marker: EventType, length: number) { const avatars = [ { - src: 'https://image.stern.de/32707724/t/o5/v2/w1440/r1.7778/-/andrew-tate.jpg', + src: tate, alt: 'Sandeep Srivastva', color: '$emerald600', }, @@ -66,65 +101,100 @@ export function EventMarker(props: {marker: PA_Point}) { color: '$red400', }, ]; - const extraAvatars = avatars.slice(3); - const remainingCount = extraAvatars.length; + const extraAvatars = avatars.slice(length); + const remainingCount = extraAvatars.length > 99 ? 99 : extraAvatars.length; + + return {avatars, remainingCount}; +} + +function MarkerTheme1({marker}: {marker: EventType}) { + const colors = store.getState().nonSaveVariables.theme.colors; return ( - - - {avatars.slice(0, 3).map((avatar, index) => { - return ( + + + + ); +} + +function MarkerTheme0({marker}: {marker: EventType}) { + const colors = store.getState().nonSaveVariables.theme.colors; + + const {avatars, remainingCount} = getEventAvatars(marker, 3); + + return ( + + + + {[ + remainingCount > 0 ? ( + + + +{remainingCount} + + + ) : ( + <> + ), + ...avatars.slice(0, 3).map((avatar, index) => ( + bg={avatar.color}> {avatar.alt} + - ); - })} - - {'+ ' + remainingCount + ''} - + )), + ]} - - - {marker.type} - + ); } const styles = StyleSheet.create({ marker: { - padding: 5, - borderRadius: 5, + padding: 3, + borderRadius: 10, + }, + marker1: { + padding: 3, + borderRadius: (90 + 3 * 2) / 2, + }, + markerImage: { + borderRadius: 10 - 3, + width: 160, + height: 100, + alignSelf: 'center', + }, + markerImage1: { + borderRadius: 90 / 2, + width: 90, + height: 90, + + alignSelf: 'center', }, markerText: { color: 'white', fontWeight: 'bold', }, + avatarGroup: { + position: 'absolute', + top: -8 - 5, + //right: -8 - 5, + right: -5, + }, }); diff --git a/src/components/map/cluster/getData.ts b/src/components/map/cluster/getData.ts index d6bff35..b4fb87e 100644 --- a/src/components/map/cluster/getData.ts +++ b/src/components/map/cluster/getData.ts @@ -1,32 +1,14 @@ import {Position} from '@rnmapbox/maps/src/types/Position'; -import { getDistance } from 'geolib'; - +import {getDistance} from 'geolib'; +import { PA_Point, PA_Point_Cluster, PA_Point_Event } from '../types'; -interface PA_Point_Cluster { - latitude: number; - longitude: number; - cluster: number; - type: 'cluster'; - id: string; -} -interface PA_Point_Event { - latitude: number; - longitude: number; - type: 'event'; - id: number; -} -type PA_Point = PA_Point_Cluster | PA_Point_Event; - -export type {PA_Point_Cluster, PA_Point_Event, PA_Point}; - - const createChecksum = (ids: (number | string)[]) => { let checksum = 0; for (const id of ids.sort()) { const value = typeof id === 'string' ? parseInt(id, 36) : id; - checksum ^= value & 0xFFFF; + checksum ^= value & 0xffff; } return checksum; }; @@ -68,33 +50,25 @@ const clusterMarkers = (markers: PA_Point[], threshold: number) => { longitudes.reduce((a, b) => a + b, 0) / longitudes.length; const sumValues = cluster.reduce((sum, marker) => { if (marker.type === 'event') { - if(isEvent === 0) - isEvent = 1; - else if(isEvent === 1) - isEvent = -1; - - return sum + 1}; + if (isEvent === 0) isEvent = 1; + else if (isEvent === 1) isEvent = -1; - isEvent = -1; + return sum + 1; + } + + isEvent = -1; marker = marker as PA_Point_Cluster; return sum + (marker.cluster || 0); }, 0); - if(isEvent === 1) - { - return { - latitude: centerLatitude, - longitude: centerLongitude, - type: 'event', - id: cluster[0].id, - } as PA_Point; + if (isEvent === 1) { + return cluster[0] as PA_Point_Event; } const ids = cluster.map(marker => marker.id); const idChecksum = createChecksum(ids); - return { latitude: centerLatitude, @@ -102,8 +76,7 @@ const clusterMarkers = (markers: PA_Point[], threshold: number) => { type: 'cluster', cluster: sumValues, id: idChecksum.toString(), - } as PA_Point; - + } as PA_Point_Cluster; }); return centerPoints; @@ -118,18 +91,16 @@ const clusterMarkers = (markers: PA_Point[], threshold: number) => { };*/ const calculateRadius = (bounds: [Position, Position]) => { - - - const distanceInMeters = getDistance( - { latitude: bounds[0][0], longitude: bounds[0][1] }, - { latitude: bounds[1][0], longitude: bounds[1][1] } - ); - return distanceInMeters/600000/(1.5/*android bug fix for roteration*/); + const distanceInMeters = getDistance( + {latitude: bounds[0][0], longitude: bounds[0][1]}, + {latitude: bounds[1][0], longitude: bounds[1][1]}, + ); + return distanceInMeters / 600000 / 1.5 /*android bug fix for roteration*/; }; - function getLocationData( - bounds: [Position, Position], zoomLevel: number + bounds: [Position, Position], + zoomLevel: number, ): Promise<{locations: PA_Point[]} | undefined> { if (bounds === undefined) return Promise.resolve(undefined); @@ -148,43 +119,51 @@ function getLocationData( {"latitude": 50.1109, "longitude": 8.6821, "cluster": 23, "type": "cluster", "id": "467132"}, {"latitude": 51.0493, "longitude": 13.7384, "cluster": 8, "type": "cluster", "id": "842906"}, {"latitude": 49.0069, "longitude": 8.4037, "cluster": 46, "type": "cluster", "id": "346708"},*/ - {"latitude": 51.9674, "longitude": 7.6194, "type": "event", "id": 203867}, - {"latitude": 50.7753, "longitude": 6.0839, "type": "event", "id": 501298}, - {"latitude": 53.0793, "longitude": 8.8017, "type": "event", "id": 763159}, - {"latitude": 48.7758, "longitude": 9.1829, "type": "event", "id": 917425}, - {"latitude": 52.3759, "longitude": 9.732, "type": "event", "id": 635028}, - {"latitude": 51.512, "longitude": 7.4653, "type": "event", "id": 841592}, - {"latitude": 49.4875, "longitude": 8.466, "type": "event", "id": 180356}, - {"latitude": 51.3611, "longitude": 7.0119, "type": "event", "id": 592874}, - {"latitude": 54.3227, "longitude": 10.1356, "type": "event", "id": 384791}, - {"latitude": 51.8049, "longitude": 10.3351, "type": "event", "id": 273495}, - {"latitude": 50.3213, "longitude": 11.8676, "type": "event", "id": 699318}, - {"latitude": 49.8765, "longitude": 6.6695, "type": "event", "id": 159627}, - {"latitude": 52.2651, "longitude": 7.3085, "type": "event", "id": 903716}, - {"latitude": 53.9638, "longitude": 12.413, "type": "event", "id": 487612}, - {"latitude": 50.9321, "longitude": 9.1847, "type": "event", "id": 827464}, - {"latitude": 52.4378, "longitude": 13.2847, "type": "event", "id": 931845}, - {"latitude": 49.4644, "longitude": 11.0168, "type": "event", "id": 674132}, - {"latitude": 51.1983, "longitude": 8.8998, "type": "event", "id": 298473} + { + latitude: 51.9674, + longitude: 7.6197, + type: 'event', + id: "203868", + theme: 1, + }, + { + latitude: 51.9674, + longitude: 7.6196, + type: 'event', + id: "203866", + theme: 1, + }, + {latitude: 51.9674, longitude: 7.6194, type: 'event', id: "203867"}, + {latitude: 50.7753, longitude: 6.0839, type: 'event', id: "501298"}, + {latitude: 53.0793, longitude: 8.8017, type: 'event', id: "763159"}, + {latitude: 48.7758, longitude: 9.1829, type: 'event', id: "917425"}, + {latitude: 52.3759, longitude: 9.732, type: 'event', id: "635028"}, + {latitude: 51.512, longitude: 7.4653, type: 'event', id: "841592"}, + {latitude: 49.4875, longitude: 8.466, type: 'event', id: "180356"}, + {latitude: 51.3611, longitude: 7.0119, type: 'event', id: "592874"}, + {latitude: 54.3227, longitude: 10.1356, type: 'event', id: "384791"}, + {latitude: 51.8049, longitude: 10.3351, type: 'event', id: "273495"}, + {latitude: 50.3213, longitude: 11.8676, type: 'event', id: "699318"}, + {latitude: 49.8765, longitude: 6.6695, type: 'event', id: "159627"}, + {latitude: 52.2651, longitude: 7.3085, type: 'event', id: "903716"}, + {latitude: 53.9638, longitude: 12.413, type: 'event', id: "487612"}, + {latitude: 50.9321, longitude: 9.1847, type: 'event', id: "827464"}, + {latitude: 52.4378, longitude: 13.2847, type: 'event', id: "931845"}, + {latitude: 49.4644, longitude: 11.0168, type: 'event', id: "674132"}, + {latitude: 51.1983, longitude: 8.8998, type: 'event', id: "298473"}, ], }; - - - console.log("got data"); const zoomLevelChanged = calculateRadius(bounds); - console.log(zoomLevelChanged); + //console.log(zoomLevelChanged); - const data = { - locations: clusterMarkers(rawData.locations, zoomLevelChanged),//calculateThreshold(bounds) + locations: clusterMarkers(rawData.locations, zoomLevelChanged), //calculateThreshold(bounds) }; - console.log("clustered data"); - resolve(data); - }, 120); // Simulating a delay of 1 second for the asynchronous operation + }, 2); // Simulating a delay of 1 second for the asynchronous operation }); } diff --git a/src/components/map/types.ts b/src/components/map/types.ts new file mode 100644 index 0000000..6afe2e6 --- /dev/null +++ b/src/components/map/types.ts @@ -0,0 +1,32 @@ +type EventID = string; + +interface BasicEvent { + id: EventID; + latitude: number; + longitude: number; +} + +interface PA_Point_Cluster extends BasicEvent { + type: 'cluster'; + cluster: number; +} + +interface PA_Point_Event extends BasicEvent { + type: 'event'; + theme?: number; +} + +interface PA_Point_EventStore extends BasicEvent { + type: 'eventStore'; + theme?: number; +} + +type PA_Point = PA_Point_Cluster | PA_Point_Event | PA_Point_EventStore; + +export type { + PA_Point_Cluster, + PA_Point_Event, + PA_Point_EventStore, + PA_Point, + EventID, +}; diff --git a/src/configs/appNonSaveVar.ts b/src/configs/appNonSaveVar.ts index 41b18d8..d05f213 100644 --- a/src/configs/appNonSaveVar.ts +++ b/src/configs/appNonSaveVar.ts @@ -2,12 +2,12 @@ import {chatEntity, roomId} from '@configs/chat/types'; import {User} from '@user/types'; -import {UserId} from './types'; +import {AccountName} from './types'; import {getVersionByNum, VersionType} from '@helper/version'; import configDarkTheme, {ThemeTokensType} from '@configs/colors'; -import { PA_Point } from '@components/map/cluster/getData'; +import {PA_Point} from '@components/map/cluster/getData'; export const APP_VERSION = getVersionByNum(1); export const AppVarMaxBackups: number = 10; @@ -30,7 +30,7 @@ export interface NON_SAVE_VARS { appStatus: appStatus; theme: ThemeTokensType; connectionStatus: connectionStatus; - cachedUsers: {[key: UserId]: User}; + cachedUsers: {[key: AccountName]: User}; chats: {[key: roomId]: chatEntity}; chatActivity: roomId[]; selectedChat: roomId | 'none'; diff --git a/src/configs/appNonSaveVarReducer.ts b/src/configs/appNonSaveVarReducer.ts index 06caba7..d6b5679 100644 --- a/src/configs/appNonSaveVarReducer.ts +++ b/src/configs/appNonSaveVarReducer.ts @@ -5,7 +5,7 @@ import {ThemeTokensType} from '@configs/colors'; import {chatEntity, roomId} from '@configs/chat/types'; import {User} from '@user/types'; -import {UserId} from './types'; +import {AccountName} from './types'; import {PA_Point} from '@components/map/cluster/getData'; export const appNonSaveVariablesSlice = createSlice({ @@ -19,9 +19,9 @@ export const appNonSaveVariablesSlice = createSlice({ state.theme = action.payload; }, setCachedUser: (state, action: PayloadAction) => { - state.cachedUsers[action.payload.UserId] = action.payload; + state.cachedUsers[action.payload.AccountName] = action.payload; }, - removeCachedUser: (state, action: PayloadAction) => { + removeCachedUser: (state, action: PayloadAction) => { delete state.cachedUsers[action.payload]; }, setSelectedChat: (state, action: PayloadAction) => { @@ -56,7 +56,7 @@ export const appNonSaveVariablesSlice = createSlice({ }, setMapMarkers: (state, action: PayloadAction) => { state.eventMapMarkers = action.payload; - } + }, }, }); diff --git a/src/configs/appVar.ts b/src/configs/appVar.ts index f94db1a..7146503 100644 --- a/src/configs/appVar.ts +++ b/src/configs/appVar.ts @@ -1,4 +1,4 @@ -import {EMail, UserId, XToken} from '@configs/types'; +import {XToken, AccountName, Username} from '@configs/types'; import {VersionType} from '@helper/version'; import {MyUserAccount} from '@user/types'; import {APP_VERSION} from './appNonSaveVar'; @@ -41,9 +41,11 @@ export function applyUpdateChanges(appVar: any): Promise { //these variables may be changed by the user and will be saved in storage export interface RegisterProcess { - isRegistering: false | 'stepTwo' | 'stepFinal'; - XToken: XToken | undefined; - EMail: EMail; + //isRegistering: false | 'stepTwo' | 'stepFinal'; + //XToken: XToken | undefined; + Username: Username; + Password: string; + AccountName: AccountName; } export interface PREFERENCES_VARS { @@ -51,8 +53,8 @@ export interface PREFERENCES_VARS { version: VersionType; theme: ThemeMode; RegisterProcess: RegisterProcess; - selectedAccount: UserId | 'none'; - accounts: {[key: UserId]: MyUserAccount}; + selectedAccount: AccountName | 'none'; + accounts: {[key: AccountName]: MyUserAccount}; } export const preferences_vars_default: PREFERENCES_VARS = { @@ -60,9 +62,11 @@ export const preferences_vars_default: PREFERENCES_VARS = { version: APP_VERSION, //version of datatypes in storage theme: ThemeMode.Dark, RegisterProcess: { - isRegistering: false, - XToken: undefined, - EMail: '', + Username: '', + Password: '', + //isRegistering: false, + //XToken: undefined, + AccountName: '', }, selectedAccount: 'none', accounts: {}, diff --git a/src/configs/appVarReducer.ts b/src/configs/appVarReducer.ts index 4d98a5c..d01b357 100644 --- a/src/configs/appVarReducer.ts +++ b/src/configs/appVarReducer.ts @@ -7,7 +7,7 @@ import { } from './appVar'; import LangFormat from '@lang/default'; import {lang as defaultLang} from '@lang/en'; -import {UserId} from './types'; +import {AccountName} from './types'; import {MyUserAccount} from '@user/types'; import {ThemeMode} from './colors'; @@ -37,11 +37,11 @@ export const appVariablesSlice = createSlice({ setRegisterProcess: (state, action: PayloadAction) => { state.preferences.RegisterProcess = action.payload; }, - setCurrentAccount: (state, action: PayloadAction) => { + setCurrentAccount: (state, action: PayloadAction) => { state.preferences.selectedAccount = action.payload; }, setAccount: (state, action: PayloadAction) => { - state.preferences.accounts[action.payload.UserId] = action.payload; + state.preferences.accounts[action.payload.AccountName] = action.payload; }, setDBEK: (state, action: PayloadAction) => { state.preferences.dbek = action.payload; diff --git a/src/configs/types.ts b/src/configs/types.ts index 518b417..aaf9c1c 100644 --- a/src/configs/types.ts +++ b/src/configs/types.ts @@ -1,6 +1,6 @@ -import {ThemeMode} from './appVar'; +//import {ThemeMode} from './appVar'; -export type EMail = string; +//export type EMail = string; export type langCode = 'en' | string; @@ -11,11 +11,11 @@ export type Username = string | undefined; export type Password = string; export type UserAgent = string; export type XToken = string; -export type verifyId = string; +//export type verifyId = string; export type XAuthorization = string; -export type UserId = string; -export type WebSocketSessionId = string; +//export type UserId = string; +//export type WebSocketSessionId = string; export const accountNameOptions = { minLength: 4, @@ -42,6 +42,7 @@ export const passwordOptions = { }, }; +/* export const emailOptions = { maxLength: 48, isAllowed: (email: string) => { @@ -49,4 +50,4 @@ export const emailOptions = { /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, ); }, -}; +}; */ diff --git a/src/helper/animations.tsx b/src/helper/animations.tsx index 395ac34..3f86e8e 100644 --- a/src/helper/animations.tsx +++ b/src/helper/animations.tsx @@ -1,6 +1,5 @@ import {useFocusEffect} from '@react-navigation/native'; import {animated, useSpring} from '@react-spring/native'; -import {useEffect} from 'react'; import {Dimensions, View} from 'react-native'; const AnimationView = animated(View); diff --git a/src/helper/base64.ts b/src/helper/base64.ts new file mode 100644 index 0000000..511d01d --- /dev/null +++ b/src/helper/base64.ts @@ -0,0 +1,9 @@ +import {Buffer} from 'buffer'; + +export function ToBase64(str: string) { + return Buffer.from(str).toString('base64'); +} + +export function FromBase64(str: string) { + return Buffer.from(str, 'base64').toString(); +} diff --git a/src/helper/request.ts b/src/helper/request.ts index 6827676..4ad93bc 100644 --- a/src/helper/request.ts +++ b/src/helper/request.ts @@ -1,39 +1,35 @@ -import {types} from '@babel/core'; import {store} from '@redux/store'; -import {Any} from '@react-spring/types'; -import {type} from 'os'; import {Platform} from 'react-native'; import getUserAgent from './userAgent'; import { AccountName, - EMail, Password, XAuthorization, - UserId, Username, - verifyId, - WebSocketSessionId, - XToken, } from '../configs/types'; import MyUserManager from '@user/MyUserManager'; export const apiPath = { backend: { - prefix: 'https://alpha-api.clickandjoin.umbach.dev/v1', + prefix: 'https://api-partyapp.ex.umbach.dev/api/v1', }, }; export enum apiBackendRequest { - REGISTER_STEP_1 = '/admin/users/email', + LOGIN = '/user/login', + APP_START = '/appstart', + GET_USER_PROFILE = '/users/:accountName', + LOGOUT = '/user/logout', + SIGN_UP = '/user/signup', + /*REGISTER_STEP_1 = '/admin/users/email', REGISTER_RESEND_MAIL = '/admin/users/email/resend', REGISTER_STEP_2 = '/verify/email/:xToken/:verifyId', REGISTER_STEP_FINAL = '/admin/users', REGISTER_STEP_FINAL_ACCOUNT_NAME_CHECK = '/admin/users/validation/accountname', LOGIN = '/user', - GET_USER_PROFILE = '/users/:userId', - APP_START = '/appstart', + APP_START = '/appstart', */ } type requestGET = {[key: string]: string}; @@ -46,6 +42,7 @@ export interface defaultRequest { requestHeader?: requestHeader; } +/* interface REGISTER_STEP_1 extends defaultRequest { path: apiBackendRequest.REGISTER_STEP_1; @@ -105,17 +102,30 @@ interface REGISTER_STEP_FINAL extends defaultRequest { WebSocketSessionId: WebSocketSessionId; }; } - + */ interface LOGIN extends defaultRequest { path: apiBackendRequest.LOGIN; request: { - Email: EMail; + AccountName: AccountName; + //Email: EMail; Password: Password; }; response?: { XAuthorization: XAuthorization; - UserId: UserId; - WebSocketSessionId: WebSocketSessionId; + Username: Username; + }; +} + +interface SIGN_UP extends defaultRequest { + path: apiBackendRequest.SIGN_UP; + request: { + AccountName: AccountName; + Username: Username; + Password: Password; + }; + response?: { + XAuthorization: XAuthorization; + Username: Username; }; } @@ -123,10 +133,10 @@ interface GET_USER_PROFILE extends defaultRequest { path: apiBackendRequest.GET_USER_PROFILE; requestGET: { - ':userId': UserId; + ':accountName': AccountName; }; response: { - AccountName: AccountName; + //AccountName: AccountName; Username: Username; Description: string; FollowersCount: number; @@ -141,7 +151,7 @@ interface APP_START extends defaultRequest { path: apiBackendRequest.APP_START; response: { - TokenValid: boolean; + /* TokenValid: boolean; AccountName: AccountName; Username: Username; Description: string; @@ -151,23 +161,30 @@ interface APP_START extends defaultRequest { XpPoints: number; AccountStatus: number; AvatarUrl: string; - Events: any; + Events: any; */ }; } +interface LOGOUT extends defaultRequest { + path: apiBackendRequest.LOGOUT; +} + type FetchTypes = - | REGISTER_STEP_1 + | LOGIN + | SIGN_UP + /*| REGISTER_STEP_1 | REGISTER_RESEND_MAIL | REGISTER_STEP_2 | REGISTER_STEP_FINAL | REGISTER_STEP_FINAL_ACCOUNT_NAME_CHECK - | LOGIN + | LOGIN*/ | GET_USER_PROFILE - | APP_START; - + | APP_START + | LOGOUT; +/* function isA(obj: any): obj is REGISTER_STEP_1 { return obj.request !== undefined; -} +} */ export function makeRequest(type: T1): Promise { let makeRequestObj: any = type; @@ -204,6 +221,8 @@ export function makeRequest(type: T1): Promise { const SessionId = MyUserManager.getSessionId(); if (SessionId !== undefined) headers['X-Authorization'] = SessionId; + console.log('session id: ' + SessionId); + const requestOptions: RequestInit = { method: makeRequestObj.request !== undefined ? 'POST' : 'GET', diff --git a/src/helper/storage/bdm/getDB.ts b/src/helper/storage/bdm/getDB.ts index 98a72e0..f79ac72 100644 --- a/src/helper/storage/bdm/getDB.ts +++ b/src/helper/storage/bdm/getDB.ts @@ -63,7 +63,7 @@ export async function openMyDatabase( schema: databaseConfType, nameObj: databaseNameSuffix, ): Promise { - const folderPath = MyUserManager.getSelectedUserId(); + const folderPath = MyUserManager.getSelectedUserAccount(); const path = folderPath + '/' + diff --git a/src/helper/storage/bdm/schemas/users.ts b/src/helper/storage/bdm/schemas/users.ts index e1a49a0..961db8d 100644 --- a/src/helper/storage/bdm/schemas/users.ts +++ b/src/helper/storage/bdm/schemas/users.ts @@ -5,26 +5,25 @@ import {setEntry} from '../set'; import {databaseConf, possibleDBKeys} from '../types'; enum keys { - UserId = 'a', - AccountName = 'b', - Username = 'c', - Description = 'd', - FollowersCount = 'e', - FollowingCount = 'f', - lastUpdateTimestamp = 'g', - ProfilePicture = 'h', - ProfilePictureBinaryLQ = 'i', - ProfilePictureBinaryHQ = 'j', + // UserId = 'a', + AccountName = 'a', + Username = 'b', + Description = 'c', + FollowersCount = 'd', + FollowingCount = 'e', + lastUpdateTimestamp = 'f', + ProfilePicture = 'g', + ProfilePictureBinaryLQ = 'h', + ProfilePictureBinaryHQ = 'i', - XpLevel = 'k', - XpPoints = 'l', + XpLevel = 'j', + XpPoints = 'k', } const name = 'users'; -const primaryKey: keyof typeof propsDefault = keys.UserId; +const primaryKey: keyof typeof propsDefault = keys.AccountName; const propsType: {[key in keyof typeof propsDefault]: string} = { - [keys.UserId]: 'string', [keys.AccountName]: 'string', [keys.Username]: 'string', [keys.Description]: 'string', @@ -40,7 +39,6 @@ const propsType: {[key in keyof typeof propsDefault]: string} = { }; const propsDefault = { - [keys.UserId]: '', [keys.AccountName]: '', [keys.Username]: '', [keys.Description]: '', diff --git a/src/lang/en.ts b/src/lang/en.ts index a7039ad..61130fd 100644 --- a/src/lang/en.ts +++ b/src/lang/en.ts @@ -58,7 +58,7 @@ export const lang: LangFormat = { }, login: { title: 'Welcome back!', - inputPhoneNumberOrAccountName: 'PHONE NUMBER OR ACCOUNT NAME', + inputPhoneNumberOrAccountName: 'ACCOUNT NAME', inputPassword: 'PASSWORD', }, signUpStepUsername: { diff --git a/src/navigation/navigation.tsx b/src/navigation/navigation.tsx index a5168d4..1e93646 100644 --- a/src/navigation/navigation.tsx +++ b/src/navigation/navigation.tsx @@ -38,6 +38,8 @@ export default function Navigation() { const currentUser = reduxStore.getState().appVariables.preferences.selectedAccount; + console.log('NAVIGATION', currentUser); + return ( + + */ + return ( - - (); @@ -41,6 +45,16 @@ function MapTab() { options={{headerShown: false}} component={MapScreen} /> + + ); } diff --git a/src/pages/appStart/StartHelper.tsx b/src/pages/appStart/StartHelper.tsx index 3cfb05d..d2f5364 100644 --- a/src/pages/appStart/StartHelper.tsx +++ b/src/pages/appStart/StartHelper.tsx @@ -5,19 +5,13 @@ import {SafeAreaView} from 'react-native-safe-area-context'; import {useSelector} from 'react-redux'; import {RootState, store} from '@redux/store'; import {useEffect} from 'react'; - -import imgSrc from '@img/maimg.png'; -import {placeholder} from '@lang/default'; - import {initAppData} from '@helper/appData'; import {appStatus} from '@configs/appNonSaveVar'; import {appNonSaveVarActions} from '@configs/appNonSaveVarReducer'; import BigDataManager from '@helper/storage/BigDataManager'; import {initKey} from '@helper/storage/bdm/encryption'; -import UserManager from '@user/UserManager'; -import MyUserManager from '@user/MyUserManager'; import DBSchemas from '@helper/storage/bdm/schemas'; -import {chatTags, convertDatabaseChat} from '@configs/chat/types'; +import {convertDatabaseChat} from '@configs/chat/types'; import {addChatEntity} from '@components/chat/initChatDatabase'; import {initDatabase} from '@helper/storage/bdm/init'; @@ -68,7 +62,6 @@ export async function onAppStart() { console.log('finish'); const usrDBKeys = BigDataManager.databases.users.keys; await BigDataManager.databases.users.setEntry({ - [usrDBKeys.UserId]: 'test', [usrDBKeys.AccountName]: '#845613', [usrDBKeys.Username]: 'TestGroupVirtual', [usrDBKeys.Description]: 'This is a test account that is not real. ^^', diff --git a/src/pages/map/map.tsx b/src/pages/map/map.tsx index 1b57d06..299a16a 100644 --- a/src/pages/map/map.tsx +++ b/src/pages/map/map.tsx @@ -21,7 +21,7 @@ let isRerenderData = 0; export const Map = () => { const mapRef = React.useRef(null); - const [mapMarkers, setMapMarkers] = useState([]); // Add useState for visibleBounds + //const [mapMarkers, setMapMarkers] = useState([]); // Add useState for visibleBounds const [lastDataRerender, setLastDataRerender] = useState(0); // Add useState for visibleBounds const getVisibleBounds = async () => { @@ -32,7 +32,7 @@ export const Map = () => { isRerenderData = now; - console.log('----1'); + //console.log('----1'); if (mapRef.current) { const visibleBounds = await ( @@ -53,17 +53,17 @@ export const Map = () => { //const zoomLevel = await (mapRef.current as Mapbox.MapView).getZoom(); - console.log('----2', Date.now() - now + 'ms'); + //console.log('----2', Date.now() - now + 'ms'); //console.log(visibleBounds); let markerData = (await getLocationData(visibleBounds, 0))?.locations; - console.log('----3', Date.now() - now + 'ms'); + //console.log('----3', Date.now() - now + 'ms'); - //store.dispatch(appNonSaveVarActions.setMapMarkers(markerData || [])); - setMapMarkers(markerData || []); + store.dispatch(appNonSaveVarActions.setMapMarkers(markerData || [])); + //setMapMarkers(markerData || []); - console.log('----4', Date.now() - now + 'ms'); + //console.log('----4', Date.now() - now + 'ms'); } isRerenderData = 0; @@ -75,7 +75,7 @@ export const Map = () => { if (now - lastCameraChange > 500) { lastCameraChange = now; setTimeout(getVisibleBounds, 500); - console.log('------------5', Date.now() - now + 'ms'); + //console.log('------------5', Date.now() - now + 'ms'); } } @@ -117,7 +117,7 @@ export const Map = () => { logoPosition={{top: 10, left: 10}} styleURL="mapbox://styles/titaniumbach/clpij5uoo00o301pg2dj23j0m" projection="globe"> - + diff --git a/src/pages/profile/profile.tsx b/src/pages/profile/profile.tsx index 64a6059..3f86736 100644 --- a/src/pages/profile/profile.tsx +++ b/src/pages/profile/profile.tsx @@ -14,6 +14,9 @@ import {useSelector} from 'react-redux'; import {RootState} from '@redux/store'; import {Text} from '@gluestack-ui/themed'; import {MyTouchableOpacity} from '@components/MyTouchableOpacity'; +import UserManager from '@user/UserManager'; +import MyUserManager from '@user/MyUserManager'; +import {apiBackendRequest, makeRequest} from '@helper/request'; function UserAvatar() { return ( @@ -29,6 +32,11 @@ export function ProfileOverview() { const lang = useSelector( (state: RootState) => state.appVariables.lang.profile.overview.statistics, ); + const user = UserManager.getUserSelector( + useSelector( + (state: RootState) => state.appVariables.preferences.selectedAccount, + ), + ); return ( - + - + - + - + ); @@ -97,6 +107,20 @@ export function ProfileSettings() { const navigation = useNavigation(); const rootNavigation = useNavigation(); + const user = UserManager.getUserSelector( + useSelector( + (state: RootState) => state.appVariables.preferences.selectedAccount, + ), + ); + + /* + + */ + return ( navigation.navigate('UpdateUsername')} /> - - @@ -153,9 +171,18 @@ export function ProfileSettings() { - rootNavigation.navigate('Registration', {screen: 'LoginPreview'}) - } + onPress={() => { + makeRequest({ + path: apiBackendRequest.LOGOUT, + request: {}, + response: {}, + }).catch(reason => { + console.log('logout failed, err: ', reason); + }); + + MyUserManager.logoutMyUser(); + rootNavigation.navigate('Registration', {screen: 'LoginPreview'}); + }} /> diff --git a/src/pages/welcome/login/login.tsx b/src/pages/welcome/login/login.tsx index 62e4645..53439db 100644 --- a/src/pages/welcome/login/login.tsx +++ b/src/pages/welcome/login/login.tsx @@ -1,58 +1,114 @@ -import { MyButton } from "@components/MyButton"; -import { MyIconInput } from "@components/MyInput"; -import { MyScreenContainer } from "@components/MyScreenContainer"; -import { MyTitle } from "@components/MyTitle"; -import { RootScreenNavigationProp } from "@navigation/navigation"; -import { ContentContainer, navigateToHome } from "@navigation/registration/registration"; -import { useNavigation } from "@react-navigation/native"; -import { RootState } from "@redux/store"; -import { useState } from "react"; -import { View } from "react-native"; -import { useSelector } from "react-redux"; +import {MyButton} from '@components/MyButton'; +import {MyIconInput} from '@components/MyInput'; +import {MyScreenContainer} from '@components/MyScreenContainer'; +import {MyTitle} from '@components/MyTitle'; +import {apiBackendRequest, makeRequest} from '@helper/request'; +import {RootScreenNavigationProp} from '@navigation/navigation'; +import { + ContentContainer, + navigateToHome, +} from '@navigation/registration/registration'; +import {useNavigation} from '@react-navigation/native'; +import {RootState} from '@redux/store'; +import MyUserManager from '@user/MyUserManager'; +import {useState} from 'react'; +import {View} from 'react-native'; +import {useSelector} from 'react-redux'; +import {ToBase64} from '@helper/base64'; +import showToast from '@components/MyToast'; +import {useToast} from '@gluestack-ui/themed'; export function Login() { - const lang = useSelector( - (state: RootState) => state.appVariables.lang.registration, - ); - const navigation = useNavigation(); - - const [username, setUsername] = useState(''); - const [password, setPassword] = useState(''); - - const loginEnabled = username.length > 0 && password.length > 0; - - return ( - - - - - - setUsername(text)} - /> - setPassword(text)} - /> - - navigateToHome(navigation)} - disabled={!loginEnabled} - /> - - - - ); - } \ No newline at end of file + const lang = useSelector( + (state: RootState) => state.appVariables.lang.registration, + ); + const navigation = useNavigation(); + const toast = useToast(); + + const [isLoading, setIsLoading] = useState(false); + const [accountName, setAccountName] = useState('anna'); + const [password, setPassword] = useState('testtesttest'); + + const loginEnabled = accountName.length > 0 && password.length > 0; + + return ( + + + + + + setAccountName(text)} + /> + setPassword(text)} + /> + + { + console.log('login'); + + setIsLoading(true); + + makeRequest({ + path: apiBackendRequest.LOGIN, + request: { + AccountName: accountName, + Password: ToBase64(password), + }, + response: { + XAuthorization: '', + Username: '', + }, + }) + .then(resp => { + console.log('resp', resp); + + MyUserManager.createNewMyUser( + accountName, + resp.response.Username, + resp.response.XAuthorization, + ) + .then(async () => { + setIsLoading(false); + navigateToHome(navigation); + }) + .catch(err => { + console.log('catch', err); + }); + }) + .catch(reason => { + console.log('reason', reason); + setIsLoading(false); + + showToast(toast, { + title: 'Failed', + variant: 'solid', + action: 'error', + description: undefined, + isClosable: true, + rest: {colorScheme: 'primary'}, + }); + }); + }} + disabled={!loginEnabled} + /> + + + + ); +} diff --git a/src/pages/welcome/signUp/signUp.tsx b/src/pages/welcome/signUp/signUp.tsx index 21f9337..8aeefb8 100644 --- a/src/pages/welcome/signUp/signUp.tsx +++ b/src/pages/welcome/signUp/signUp.tsx @@ -1,17 +1,21 @@ import {MyButton} from '@components/MyButton'; -import {MyClickableText} from '@components/MyClickableText'; -import {ConfirmationCodeInput, MyIconInput} from '@components/MyInput'; +import {MyIconInput} from '@components/MyInput'; import {MyScreenContainer} from '@components/MyScreenContainer'; import {MyTitle} from '@components/MyTitle'; +import {appVarActions} from '@configs/appVarReducer'; +import {ToBase64} from '@helper/base64'; +import {apiBackendRequest, makeRequest} from '@helper/request'; import {RootScreenNavigationProp} from '@navigation/navigation'; import { ContentContainer, navigateToHome, } from '@navigation/registration/registration'; import {useNavigation} from '@react-navigation/native'; -import {RootState} from '@redux/store'; +import {RootState, store} from '@redux/store'; +import MyUserManager from '@user/MyUserManager'; +import {useState} from 'react'; import {Text} from 'react-native'; -import {KeyboardAvoidingView, ScrollView, View} from 'react-native'; +import {View} from 'react-native'; import {useSelector} from 'react-redux'; function Title({text, description}: {text: string; description?: string}) { @@ -29,6 +33,12 @@ export function SignUpStepUsername() { ); const navigation = useNavigation(); + const registerProcess = useSelector( + (state: RootState) => state.appVariables.preferences.RegisterProcess, + ); + + const [username, setUsername] = useState(''); + return ( setUsername(text)} /> { + let rp = {...registerProcess}; + + rp.Username = username; + + store.dispatch(appVarActions.setRegisterProcess(rp)); + navigation.navigate('Registration', { - screen: 'SignUpStepPhoneNumber', + screen: /*'SignUpStepPhoneNumber' */ 'SignUpStepPassword', }); }} /> @@ -61,7 +79,7 @@ export function SignUpStepUsername() { ); } - +/* export function SignUpStepPhoneNumber() { const lang = useSelector( (state: RootState) => state.appVariables.lang.registration, @@ -143,13 +161,19 @@ export function SignUpStepVerifyPhoneNumber() { ); } - +*/ export function SignUpStepPassword() { const lang = useSelector( (state: RootState) => state.appVariables.lang.registration, ); const navigation = useNavigation(); + const registerProcess = useSelector( + (state: RootState) => state.appVariables.preferences.RegisterProcess, + ); + + const [password, setPassword] = useState(''); + return ( setPassword(text)} /> + onPress={() => { + let rp = {...registerProcess}; + + rp.Password = ToBase64(password); + + store.dispatch(appVarActions.setRegisterProcess(rp)); + navigation.navigate('Registration', { screen: 'SignUpStepAccountName', - }) - } + }); + }} /> @@ -190,6 +222,13 @@ export function SignUpStepAccountName() { ); const navigation = useNavigation(); + const registerProcess = useSelector( + (state: RootState) => state.appVariables.preferences.RegisterProcess, + ); + + const [isLoading, setIsLoading] = useState(false); + const [accountName, setAccountName] = useState(''); + return ( setAccountName(text)} /> navigateToHome(navigation)} + isLoading={isLoading} + onPress={() => { + let rp = {...registerProcess}; + + rp.AccountName = accountName; + + store.dispatch(appVarActions.setRegisterProcess(rp)); + + console.log('registerProcess', rp); + + makeRequest({ + path: apiBackendRequest.SIGN_UP, + request: rp, + method: 'POST', + response: { + XAuthorization: '', + Username: '', + }, + }) + .then(resp => { + console.log('response', resp); + + MyUserManager.createNewMyUser( + accountName, + resp.response.Username, + resp.response.XAuthorization, + ) + .then(async () => { + setIsLoading(false); + navigateToHome(navigation); + }) + .catch(err => { + console.log('catch', err); + }); + }) + .catch(error => { + console.log('error', error); + }); + }} /> diff --git a/src/user/MyUserManager.ts b/src/user/MyUserManager.ts index c4b447b..e60706a 100644 --- a/src/user/MyUserManager.ts +++ b/src/user/MyUserManager.ts @@ -1,12 +1,5 @@ import {appVarActions} from '@configs/appVarReducer'; -import { - AccountName, - EMail, - XAuthorization, - UserId, - Username, - WebSocketSessionId, -} from '@configs/types'; +import {AccountName, XAuthorization, Username} from '@configs/types'; import {saveVarChanges} from '@helper/appData'; import {apiBackendRequest, makeRequest} from '@helper/request'; import BigDataManager from '@helper/storage/BigDataManager'; @@ -15,30 +8,24 @@ import {useSelector} from 'react-redux'; import {MyUserAccount, createUserProp, SourceProp} from './types'; function createNewMyUser( - UserId: UserId, AccountName: AccountName, Username: Username, - EMail: EMail, SessionId: XAuthorization, - WebSocketSessionId: WebSocketSessionId, ): Promise { return new Promise((resolve, reject) => { let user: MyUserAccount = { - UserId, - AccountName: createUserProp(SourceProp.offline, AccountName), + AccountName /*: createUserProp(SourceProp.offline, AccountName)*/, Username: createUserProp(SourceProp.offline, Username), - Description: createUserProp(SourceProp.online), + /* Description: createUserProp(SourceProp.online), FollowersCount: createUserProp(SourceProp.online), FollowingCount: createUserProp(SourceProp.online), XpLevel: createUserProp(SourceProp.online), XpPoints: createUserProp(SourceProp.online), - EMail, - SessionId, - WebSocketSessionId, lastUpdateTimestamp: Math.floor(new Date().getTime() / 1000), ProfilePicture: { lq: createUserProp(SourceProp.online), - }, + }, */ + SessionId, userSettings: { lang: store.getState().appVariables.lang.details.langCode, theme: store.getState().appVariables.preferences.theme, @@ -48,10 +35,13 @@ function createNewMyUser( createMyUser(user); BigDataManager.initDatabase(); + console.log('appstart'); + makeRequest({ path: apiBackendRequest.APP_START, - requestGET: {':userId': UserId}, + requestGET: {':AccountName': AccountName}, response: { + /* AccountName: '', Description: '', FollowersCount: 0, @@ -62,19 +52,20 @@ function createNewMyUser( AvatarUrl: '', AccountStatus: 0, Events: {}, - TokenValid: false, + TokenValid: false, */ }, }) .then(resp => { - let user = {...getMyUser(UserId)}; + let user = {...getMyUser(AccountName)}; + /* user.AccountName = createUserProp( SourceProp.offline, resp.response.AccountName, - ); + ); user.Username = createUserProp( SourceProp.offline, resp.response.Username, - ); + ); user.Description = createUserProp( SourceProp.offline, resp.response.Description, @@ -94,13 +85,13 @@ function createNewMyUser( user.XpPoints = createUserProp( SourceProp.offline, resp.response.XpPoints, - ); + );*/ setMyUser(user); resolve(); }) .catch(resp => { - console.error(resp.status); + console.error(`appstart req failed: ${resp.status}`); reject(); }); }); @@ -108,12 +99,12 @@ function createNewMyUser( function createMyUser(user: MyUserAccount) { store.dispatch(appVarActions.setAccount(user)); - store.dispatch(appVarActions.setCurrentAccount(user.UserId)); + store.dispatch(appVarActions.setCurrentAccount(user.AccountName)); saveVarChanges(); } -function getMyUser(userId: UserId): MyUserAccount { - return store.getState().appVariables.preferences.accounts[userId]; +function getMyUser(accountName: AccountName): MyUserAccount { + return store.getState().appVariables.preferences.accounts[accountName]; } function setMyUser(user: MyUserAccount) { @@ -126,7 +117,7 @@ function logoutMyUser() { saveVarChanges(); } -function getSelectedUserId(): UserId { +function getSelectedUserAccount(): AccountName { return store.getState().appVariables.preferences.selectedAccount; } @@ -142,9 +133,12 @@ function getSelectedMyUserSelector(): MyUserAccount | undefined { return myUser; } -function getSessionId(userId?: UserId): XAuthorization | undefined { +function getSessionId(accountName?: AccountName): XAuthorization | undefined { const preferences = store.getState().appVariables.preferences; - let user = preferences.accounts[userId || preferences.selectedAccount]; + + console.log('pref', preferences); + + let user = preferences.accounts[accountName || preferences.selectedAccount]; if (user === undefined) return undefined; @@ -156,7 +150,7 @@ function getSessionId(userId?: UserId): XAuthorization | undefined { const MyUserManager = { createNewMyUser, getSessionId, - getSelectedUserId, + getSelectedUserAccount, logoutMyUser, getSelectedMyUserSelector, }; diff --git a/src/user/UserManager.ts b/src/user/UserManager.ts index 4cc1a7d..6b8404c 100644 --- a/src/user/UserManager.ts +++ b/src/user/UserManager.ts @@ -1,6 +1,6 @@ import {maxCachedUsers} from '@configs/appNonSaveVar'; import {appNonSaveVarActions} from '@configs/appNonSaveVarReducer'; -import {UserId, XAuthorization} from '@configs/types'; +import {AccountName, XAuthorization} from '@configs/types'; import {makeRequest, apiBackendRequest} from '@helper/request'; import BigDataManager from '@helper/storage/BigDataManager'; import {RootState, store} from '@redux/store'; @@ -14,13 +14,13 @@ import { User, } from './types'; -let cachedUserList: UserId[] = []; +let cachedUserList: AccountName[] = []; async function getUser( - UserId: UserId, + AccountName: AccountName, save?: boolean, ): Promise { - if (UserId === 'none') { + if (AccountName === 'none') { return undefined; } @@ -30,35 +30,34 @@ async function getUser( let userIsInCache = false; { - const usr = state.nonSaveVariables.cachedUsers[UserId]; + const usr = state.nonSaveVariables.cachedUsers[AccountName]; if (usr !== undefined) { user = usr; userIsInCache = true; } } - if (UserId === state.appVariables.preferences.selectedAccount) { - const usr = state.appVariables.preferences.accounts[UserId]; + if (AccountName === state.appVariables.preferences.selectedAccount) { + const usr = state.appVariables.preferences.accounts[AccountName]; if (usr !== undefined) { user = { AccountName: usr.AccountName, - Description: usr.Description, + /*Description: usr.Description, FollowersCount: usr.FollowersCount, FollowingCount: usr.FollowingCount, lastUpdateTimestamp: usr.lastUpdateTimestamp, - ProfilePicture: usr.ProfilePicture, - UserId, - Username: usr.Username, + ProfilePicture: usr.ProfilePicture, */ + Username: usr.Username /* XpLevel: usr.XpLevel, - XpPoints: usr.XpPoints, + XpPoints: usr.XpPoints,*/, }; } } if (user === undefined) { const usrDBKeys = BigDataManager.databases.users.keys; - const usr = await BigDataManager.databases.users.getEntry(UserId); + const usr = await BigDataManager.databases.users.getEntry(AccountName); if (usr !== undefined && usr !== null) { let ProfilePicture = { @@ -87,10 +86,7 @@ async function getUser( }; user = { - AccountName: createUserProp( - SourceProp.offline, - usr[usrDBKeys.AccountName], - ), + AccountName, Description: createUserProp( SourceProp.offline, usr[usrDBKeys.Description], @@ -105,7 +101,6 @@ async function getUser( ), lastUpdateTimestamp: usr[usrDBKeys.lastUpdateTimestamp], ProfilePicture, - UserId, Username: createUserProp(SourceProp.offline, usr[usrDBKeys.Username]), XpLevel: createUserProp(SourceProp.offline, usr[usrDBKeys.XpLevel]), XpPoints: createUserProp(SourceProp.offline, usr[usrDBKeys.XpPoints]), @@ -117,9 +112,8 @@ async function getUser( try { const resp = await makeRequest({ path: apiBackendRequest.GET_USER_PROFILE, - requestGET: {':userId': UserId}, + requestGET: {':accountName': AccountName}, response: { - AccountName: '', Description: '', FollowersCount: 0, FollowingCount: 0, @@ -131,10 +125,7 @@ async function getUser( }); user = { - AccountName: createUserProp( - SourceProp.cached, - resp.response.AccountName, - ), + AccountName: AccountName, Description: createUserProp( SourceProp.cached, resp.response.Description, @@ -155,7 +146,6 @@ async function getUser( resp.response.AvatarUrl, ), }, - UserId, Username: createUserProp(SourceProp.offline, resp.response.Username), XpLevel: createUserProp(SourceProp.cached, resp.response.XpLevel), XpPoints: createUserProp(SourceProp.cached, resp.response.XpPoints), @@ -170,7 +160,7 @@ async function getUser( if (userIsInCache === false && user !== undefined) { console.log('save in cache'); store.dispatch(appNonSaveVarActions.setCachedUser(user)); - cachedUserList.push(user.UserId); + cachedUserList.push(user.AccountName); if (cachedUserList.length > maxCachedUsers) { let usrId = cachedUserList[0]; @@ -189,47 +179,47 @@ enum GetParam { SAVE, } -let getUserList: {[key: UserId]: GetParam} = {}; +let getUserList: {[key: AccountName]: GetParam} = {}; async function refreshUsers() { - for (let UserId in getUserList) { - const param = getUserList[UserId]; - delete getUserList[UserId]; + for (let AccountName in getUserList) { + const param = getUserList[AccountName]; + delete getUserList[AccountName]; - await getUser(UserId); + await getUser(AccountName); } } setInterval(refreshUsers, 500); -function addUserToGetQueue(UserId: UserId, param: GetParam) { - if (getUserList[UserId] === undefined) { - getUserList[UserId] = param; - } else if (getUserList[UserId] < param) { - getUserList[UserId] = param; +function addUserToGetQueue(AccountName: AccountName, param: GetParam) { + if (getUserList[AccountName] === undefined) { + getUserList[AccountName] = param; + } else if (getUserList[AccountName] < param) { + getUserList[AccountName] = param; } } -function getUserSelector(UserId: UserId) { - addUserToGetQueue(UserId, GetParam.CACHE); +function getUserSelector(AccountName: AccountName) { + addUserToGetQueue(AccountName, GetParam.CACHE); const myUser = useSelector( - (state: RootState) => state.nonSaveVariables.cachedUsers[UserId], + (state: RootState) => state.nonSaveVariables.cachedUsers[AccountName], ); if (myUser === undefined) { - return initUndefinedUser(UserId); + return initUndefinedUser(AccountName); } return myUser; } -function getUserSelectorPicture(UserId: UserId): ProfilePicture { - addUserToGetQueue(UserId, GetParam.CACHE); +function getUserSelectorPicture(AccountName: AccountName): ProfilePicture { + addUserToGetQueue(AccountName, GetParam.CACHE); const myUser = useSelector( (state: RootState) => - state.nonSaveVariables.cachedUsers[UserId]?.ProfilePicture, + state.nonSaveVariables.cachedUsers[AccountName]?.ProfilePicture, ); if (myUser === undefined) { @@ -241,13 +231,15 @@ function getUserSelectorPicture(UserId: UserId): ProfilePicture { return myUser; } - -function getUserSelectorAccountName(UserId: UserId): BasicUserProp { - addUserToGetQueue(UserId, GetParam.CACHE); +/* +function getUserSelectorAccountName( + AccountName: AccountName, +): AccountName { + addUserToGetQueue(AccountName, GetParam.CACHE); const myUser = useSelector( (state: RootState) => - state.nonSaveVariables.cachedUsers[UserId]?.AccountName, + state.nonSaveVariables.cachedUsers[AccountName]?.AccountName, ); if (myUser === undefined) { @@ -255,23 +247,22 @@ function getUserSelectorAccountName(UserId: UserId): BasicUserProp { } return myUser; -} +} */ -function initUndefinedUser(UserId: UserId): User { +function initUndefinedUser(AccountName: AccountName): User { return { - AccountName: createUserProp(SourceProp.online), - Description: createUserProp(SourceProp.online), + AccountName: AccountName, + /* Description: createUserProp(SourceProp.online), FollowersCount: createUserProp(SourceProp.online), FollowingCount: createUserProp(SourceProp.online), lastUpdateTimestamp: 0, ProfilePicture: { lq: createUserProp(SourceProp.online), hq: createUserProp(SourceProp.online), - }, - UserId, - Username: createUserProp(SourceProp.online), + }, */ + Username: createUserProp(SourceProp.online) /* XpLevel: createUserProp(SourceProp.online), - XpPoints: createUserProp(SourceProp.online), + XpPoints: createUserProp(SourceProp.online), */, }; } @@ -279,7 +270,7 @@ const UserManager = { getUser, getUserSelector, getUserSelectorPicture, - getUserSelectorAccountName, + //getUserSelectorAccountName, initUndefinedUser, }; export default UserManager; diff --git a/src/user/types.ts b/src/user/types.ts index aa4ffd7..82a5b15 100644 --- a/src/user/types.ts +++ b/src/user/types.ts @@ -1,13 +1,10 @@ import {ThemeMode} from '@configs/colors'; import { AccountName, - EMail, langCode, XAuthorization, timestamp, - UserId, Username, - WebSocketSessionId, } from '@configs/types'; export enum SourceProp { @@ -30,23 +27,23 @@ export interface ProfilePicture { } export interface User { - UserId: UserId; + //UserId: UserId; + AccountName: AccountName; - ProfilePicture: ProfilePicture; - lastUpdateTimestamp: timestamp; - AccountName: BasicUserProp; - Username: BasicUserProp; + /* ProfilePicture: ProfilePicture; + lastUpdateTimestamp: timestamp; */ + Username: BasicUserProp /* Description: BasicUserProp; FollowersCount: BasicUserProp; FollowingCount: BasicUserProp; XpLevel: BasicUserProp; - XpPoints: BasicUserProp; + XpPoints: BasicUserProp; */; } export interface MyUserAccount extends User { - EMail: EMail; + //EMail: EMail; SessionId: XAuthorization; - WebSocketSessionId: WebSocketSessionId; + //WebSocketSessionId: WebSocketSessionId; userSettings: userSettings; }