Skip to content

Commit

Permalink
add redux store for portfolio to track new trades
Browse files Browse the repository at this point in the history
  • Loading branch information
capture120 committed Apr 18, 2024
1 parent 215ac95 commit 11f3fbe
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 34 deletions.
3 changes: 0 additions & 3 deletions backend/src/services/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package services

import (
"backend/src/models"
"fmt"

"gorm.io/gorm"
)
Expand Down Expand Up @@ -37,10 +36,8 @@ func (os *PortfolioService) CopyPortfolio(currentUserPortfolio models.UserPortfo

// if position exists, update the quantity -> skip to next position
if positionExists {
fmt.Println("position exists", matchingPosition.Ticker, matchingPosition.Quantity, position.Quantity)
matchingPosition.Quantity += position.Quantity
updatedPositions = append(updatedPositions, matchingPosition)
fmt.Println("position exists after", matchingPosition.Ticker, matchingPosition.Quantity, position.Quantity)
continue
}

Expand Down
3 changes: 2 additions & 1 deletion frontend/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { Provider } from 'react-redux';
import { configureStore } from '@reduxjs/toolkit';
import onboardingReducer from './reducers/onboarding/onboardingReducer';
import makePostReducer from './reducers/makePost/makePostReducer';
import portfolioReducer from './reducers/portfolio/portfolioReducer';
import LayoutWrapper from './components/LayoutWrapper';
import { ClerkProvider } from '@clerk/clerk-expo';
//import 'react-native-gesture-handler';
import * as SecureStore from 'expo-secure-store';

const store = configureStore({
reducer: {
onboarding: onboardingReducer,
makePost: makePostReducer,
portfolio: portfolioReducer,
},
});

Expand Down
2 changes: 2 additions & 0 deletions frontend/components/LayoutWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import AuthNavigator from '../router/AuthNavigation';
import { configureStore } from '@reduxjs/toolkit';
import onboardingReducer from '../reducers/onboarding/onboardingReducer';
import makePostReducer from '../reducers/makePost/makePostReducer';
import portfolioReducer from '../reducers/portfolio/portfolioReducer';
import { useSelector } from 'react-redux';
import MakePostNavigator from '../router/MakePostNavigation';

const store = configureStore({
reducer: {
onboarding: onboardingReducer,
makePost: makePostReducer,
portfolio: portfolioReducer,
},
});

Expand Down
9 changes: 6 additions & 3 deletions frontend/pages/Copy/CopyTradesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import {
} from 'react-native';
import { useNavigation, useRoute } from '@react-navigation/native';
import { useSession } from '@clerk/clerk-expo';
import { useDispatch } from 'react-redux';
import {
ProfileNavigationProp,
AuthNavigationProp,
} from '../../types/navigationTypes';
import { CopyRouteParams } from '../../types/types';
import { copyTrades } from '../../services/copy';
import { updatePortfolio } from '../../reducers/portfolio/portfolioReducer';

function CopyTradesPage() {
const dispatch = useDispatch();
const { session } = useSession();
const navigation = useNavigation<ProfileNavigationProp>();
const authNavigation = useNavigation<AuthNavigationProp>();
Expand All @@ -37,15 +40,15 @@ function CopyTradesPage() {
}

try {
await copyTrades(session?.user.id as string, user?.id);
// Alert.alert(`session?.user.id: ${session?.user.id} | user.id: ${user?.id}`);
const copiedPortfolio = await copyTrades(session?.user.id as string, user?.id);
dispatch(updatePortfolio(copiedPortfolio));
} catch (error) {
Alert.alert('Error', 'Failed to copy trades');
return;
}

Alert.alert('Success', 'Trades copied successfully');
navigation.navigate('Profile');
navigation.navigate('FollowerProfile', { user });
};

return (
Expand Down
57 changes: 38 additions & 19 deletions frontend/pages/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useNavigation, useRoute } from '@react-navigation/native';
import React, { useEffect, useState } from 'react';
import { FlatList, Pressable, ScrollView, Text, View } from 'react-native';
import { FlatList, Pressable, ScrollView, View } from 'react-native';
import { useSession } from '@clerk/clerk-expo';
import ProfileBanner from '../components/ProfileBanner';
import SubTabButton from '../components/SubTabButton';
Expand All @@ -10,28 +10,34 @@ import { ProfileActivityData } from '../constants';
import ProfilePerformance from '../components/ProfilePerformance';
import SignOut from '../components/SignOutButton';
import { getPortoflio } from '../services/etrade';
import { ProfileRouteParams, UserPortfolio } from '../types/types';
import { ProfileRouteParams } from '../types/types';
import { ProfilePositions } from '../components/ProfilePositions';
import { useSelector } from 'react-redux';
import { RootState } from '../components/LayoutWrapper';
import { useDispatch } from 'react-redux';
import { updatePortfolio } from '../reducers/portfolio/portfolioReducer';
// import SettingsSvg from '../assets/SettingsIcon.svg';



const Profile = () => {
const dispatch = useDispatch();
const { session } = useSession();
const navigation = useNavigation();
const [pageNumber, setPageNumber] = useState<number>(0);
const [portfolio, setPortfolio] = useState<UserPortfolio>()
const route = useRoute()
const isFollowerProfile = (route.params as ProfileRouteParams)?.user !== undefined;
const route = useRoute();
const isFollowerProfile =
(route.params as ProfileRouteParams)?.user !== undefined;
const user = (route.params as ProfileRouteParams)?.user || session!.user!;
const {portfolio} = useSelector((state: RootState) => {
return state.portfolio;
});

const OnActivitySelected = () => {
setPageNumber(1)
}
setPageNumber(1);
};

const OnPortfolioSelected = () => {
setPageNumber(0)
}
setPageNumber(0);
};

useEffect(() => {
// set the title of the page
Expand All @@ -40,14 +46,26 @@ const Profile = () => {
headerTitle: `@${user.username}`,
headerTitleAlign: 'center',
headerRight: () => (
<Icon type='material-community' name='cog' size={30} color='black' style={{paddingRight: 10}} />
<Icon
type="material-community"
name="cog"
size={30}
color="black"
style={{ paddingRight: 10 }}
/>
),
headerLeft: () => (
<Pressable onPress={() => navigation.goBack()}>
<Icon type='material-community' name='chevron-left' size={30} color='black' style={{paddingLeft: 5}} />
<Icon
type="material-community"
name="chevron-left"
size={30}
color="black"
style={{ paddingLeft: 5 }}
/>
</Pressable>
),
})
});

return navigation.addListener('focus', () => {
if (session?.user.username === undefined) {
Expand All @@ -58,9 +76,11 @@ const Profile = () => {
}, []);

useEffect(() => {
getPortoflio(user.id).then(userPortfolio => {
setPortfolio(userPortfolio)
})
const fetchPortfolio = async () => {
const portfolio = await getPortoflio(user.id);
dispatch(updatePortfolio(portfolio));
};
fetchPortfolio();
}, []);

return (
Expand Down Expand Up @@ -120,10 +140,9 @@ const Profile = () => {
</View>
</ScrollView>
</View>
<Text>{user.username}</Text>
<SignOut />
</ScrollView>
);
};

export default Profile;
export default Profile;
17 changes: 17 additions & 0 deletions frontend/reducers/portfolio/portfolioReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createSlice } from '@reduxjs/toolkit';
import { UserPortfolio } from '../../types/types';

const onboardingSlice = createSlice({
name: 'onboarding',
initialState: {
portfolio: null as UserPortfolio | null,
},
reducers: {
updatePortfolio(state, action) {
state.portfolio = action.payload;
},
},
});

export const { updatePortfolio } = onboardingSlice.actions;
export default onboardingSlice.reducer;
8 changes: 0 additions & 8 deletions frontend/services/etrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ export const getPortoflio = async (id: string): Promise<UserPortfolio> => {
return response.data;
}

// export const copyPortfolio = async (id: string, portfolio: UserPortfolio): Promise<HttpStatusCode> => {
// const response: AxiosResponse = await axios.post<UserPortfolio>(
// `http://${API_LINK}/etrade/portfolio/${id}`,
// portfolio,
// );
// return response.status;
// }

export const makeOrder = async (id: string, ticker: string, quantity: number, type: string): Promise<HttpStatusCode> => {
const response: AxiosResponse = await axios.post<UserPortfolio>(
`http://${API_LINK}/etrade/order/${id}`,
Expand Down

0 comments on commit 11f3fbe

Please sign in to comment.