Skip to content

Commit

Permalink
Liked Feed Update
Browse files Browse the repository at this point in the history
  • Loading branch information
TangMartin committed Nov 20, 2021
1 parent f6e934d commit 983c292
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 9 deletions.
1 change: 1 addition & 0 deletions frontend/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import TabsUI from './TabsUI';
import Search from './src/screens/tab1/search';
import Recipe from './src/screens/tab1/recipe';
import EditPantry from './src/screens/tab2/editPantry';
import EditProfile from './src/screens/tab5/editprofile';

enableScreens();

Expand Down
5 changes: 5 additions & 0 deletions frontend/src/actions/profileActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Redux
export const LIKED_RECIPES = 'LIKED_RECIPES';

// Saga
export const GET_LIKEDRECIPES = 'GET_LIKEDRECIPES';
2 changes: 2 additions & 0 deletions frontend/src/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import accountReducer from './accountReducer';
import globalReducer from './globalReducer';
import recipeReducer from './recipeReducer';
import pantryReducer from './pantryReducer';
import profileReducer from './profileReducer';

const rootReducer = combineReducers({
accountReducer,
globalReducer,
recipeReducer,
pantryReducer,
profileReducer,
});

export default rootReducer;
20 changes: 20 additions & 0 deletions frontend/src/redux/profileReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {combineReducers} from 'redux';
import {
FEATURED_FEED,
FORYOU_FEED,
SEARCH_RESULT,
} from '../actions/feedActions';
import {LIKED_RECIPES} from '../actions/profileActions';

const likedRecipeReducer = (state = [], action) => {
switch (action.type) {
case LIKED_RECIPES:
return action.payload;
default:
return state;
}
};

export default combineReducers({
likedRecipeReducer,
});
2 changes: 2 additions & 0 deletions frontend/src/saga/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
removeIngredient,
} from './pantrySaga';
import {getRecipe} from './recipeSaga';
import {getLikedRecipes} from './profileSaga';

export default function* rootSaga() {
yield all([
Expand All @@ -21,5 +22,6 @@ export default function* rootSaga() {
fork(getAllIngredients),
fork(addIngredient),
fork(removeIngredient),
fork(getLikedRecipes),
]);
}
74 changes: 74 additions & 0 deletions frontend/src/saga/profileSaga.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {takeLatest, call, put} from 'redux-saga/effects';
import axios from 'axios';
import {API_URL} from '@env';
import {GET_LIKEDRECIPES} from '../actions/profileActions';

function* getLikedRecipesCall(param) {
try {
const apiConfig = {
method: 'get',
url: `${API_URL}/user_activity?user_id=${param.userID}`,
headers: {
'Content-Type': 'application/json',
},
};

const response = yield call(axios, apiConfig);
const resultsArray = response.data[0];
console.log(
'[INFO]: GET LIKEDRECIPESFEED API: Recipe obtained = ' +
resultsArray.length.toString(),
);
const recipeArray = [];
for (const results of resultsArray) {
yield storage()
.refFromURL(results[4])
.getDownloadURL()
.then(res => {
const vegetarian = param.user.vegetarian ? results[11] === 1 : true;
const vegan = param.user.vegan ? results[12] === 1 : true;
const pescatarian = param.user.pescatarian ? true : true;
const gluten_free = param.user.gluten_free ? true : true;
const dairy_free = param.user.dairy_free ? true : true;
const keto = param.user.keto ? results[7] > 30 : true;
const paleo = param.user.paleo ? true : true;
if (
vegetarian &&
vegan &&
pescatarian &&
gluten_free &&
dairy_free &&
keto &&
paleo
) {
const recipeObj = {
recipe_id: results[0],
name: results[1],
created_time: results[2],
user_id: results[3],
header_image: res,
protein: results[5],
carbs: results[6],
fat: results[7],
fiber: results[8],
calories: results[9],
servings: results[10],
vegetarian: results[11],
vegan: results[12],
cooking_time: results[13],
};
recipeArray.push(recipeObj);
}
});
}

// console.log(recipeArray);
yield put({type: LIKED_RECIPES, payload: recipeArray});
} catch (e) {
console.log('Get LikedRecipesFeed Failed: ' + e);
}
}

export function* getLikedRecipes() {
yield takeLatest(GET_LIKEDRECIPES, getLikedRecipesCall);
}
9 changes: 4 additions & 5 deletions frontend/src/screens/tab5/editprofile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,21 @@ export default function EditProfile({navigation}) {
const dispatch = useDispatch();
const onboarded = useSelector(state => state.globalReducer.onboardReducer);
const user = useSelector(state => state.accountReducer.userInfoReducer);
const bottomSheetRef = useRef(null);
const flatListRef = useRef(null);
const snapPoints = useMemo(() => ['70%'], []);
const featuredFeed = useSelector(
state => state.recipeReducer.featureFeedReducer,
);
const forYouFeed = useSelector(
state => state.recipeReducer.forYouFeedReducer,
);
const [page, setPage] = useState('Liked');
const bottomSheetRef = useRef(null);
const flatListRef = useRef(null);
const snapPoints = useMemo(() => ['70%'], []);


useEffect(() => {
dispatch({type: GET_USER, userID: auth().currentUser.uid});
}, [dispatch]);

//test
if (!onboarded) {
navigation.replace('ShoppingStyle');
} else {
Expand Down
13 changes: 9 additions & 4 deletions frontend/src/screens/tab5/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import profileStyle from './profileStyle';
import color from '../../styles/color';
import {GET_USER} from '../../actions/accountActions';
import { FlatList } from 'react-native-gesture-handler';
import { GET_LIKEDRECIPES } from '../../actions/profileActions';

export default function Profile({navigation}) {
const dispatch = useDispatch();
const onboarded = useSelector(state => state.globalReducer.onboardReducer);
const user = useSelector(state => state.accountReducer.userInfoReducer);
const featuredFeed = useSelector(
state => state.recipeReducer.featureFeedReducer,
const likedFeed = useSelector(
state => state.profileReducer.likedRecipeReducer,
);
const forYouFeed = useSelector(
state => state.recipeReducer.forYouFeedReducer,
Expand All @@ -38,6 +39,10 @@ export default function Profile({navigation}) {
dispatch({type: GET_USER, userID: auth().currentUser.uid});
}, [dispatch]);

useEffect(() => {
dispatch({type: GET_LIKEDRECIPES, userID: auth().currentUser.uid});
}, [dispatch]);

function profileTab(tab) {
if (tab === 'Liked')
{
Expand Down Expand Up @@ -93,7 +98,7 @@ function likedTab() {
<View>
<FlatList
ref={flatListRef}
data={forYouFeed}
data={likedFeed}
showsVerticalScrollIndicator={false}
onResponderEnd={() => {
bottomSheetRef.current.close();
Expand Down Expand Up @@ -278,4 +283,4 @@ if (!onboarded) {
}

return null;
}
}

0 comments on commit 983c292

Please sign in to comment.