Skip to content

Commit 6c73cf1

Browse files
committed
in progress navbar
1 parent fc801e2 commit 6c73cf1

File tree

4 files changed

+100
-27
lines changed

4 files changed

+100
-27
lines changed

app/src/Dashboard/NavbarDash.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ const useStyles = makeStyles((theme: Theme) => createStyles({
4747

4848
// --------------------------Sorting Buttons------------------------------------//
4949
const sortByRating = (props) => {
50-
console.log('rating');
51-
console.log("props", props);
50+
// console.log('rating');
51+
// console.log("props", props);
5252

5353
// should change state to true: then in the return for project container, a conditional rendering will occur
5454

@@ -67,22 +67,17 @@ const sortByUser = (props) => {
6767
const sortMethods = ['rating', 'date', 'user'];
6868

6969
export default function NavBar(props) {
70+
console.log(props)
7071

7172
const classes = useStyles();
7273
const { style, setStyle } = useContext(styleContext);
7374

7475

7576
// toggle dropdown sorting menu
7677
const [isOpen, setIsOpen] = useState(false);
77-
const [selectedOption, setSelectedOption] = useState('rating');
7878

7979
const toggling = () => setIsOpen(!isOpen);
80-
// function not working properly, you have to go through the process and click twice
81-
const optionClicked = value => () => {
82-
setSelectedOption(value);
83-
setIsOpen(false);
84-
console.log('selectedOption', selectedOption);
85-
};
80+
8681

8782
return (
8883
<div className={classes.root} style={style}>
@@ -110,7 +105,10 @@ export default function NavBar(props) {
110105
isOpen && (
111106
<div className="sortDoc">
112107
{sortMethods.map((option, index) => (
113-
<Button onClick={optionClicked(option)}
108+
<Button onClick={() => {
109+
props.optionClicked(option);
110+
toggling();
111+
}}
114112
variant="contained"
115113
color="primary"
116114
style={{ minWidth: '137.69px' }}

app/src/Dashboard/ProjectContainer.tsx

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import React from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import { Link } from 'react-router-dom';
33
import { useQuery } from '@apollo/client';
4+
45
import { GET_PROJECTS } from './gqlStrings';
56
import Project from './Project.tsx';
67
import NavBar from './NavbarDash';
@@ -10,24 +11,56 @@ import NavBar from './NavbarDash';
1011
// 2) useQuery hook will update the data stored in Apollo Client's cache and automatically trigger child components rendering
1112

1213
const ProjectContainer = () => {
13-
let myVar = {};
14+
const myVar = {};
1415
// Need this for the individual user dasboard, for now, dashboard shows all projects from all users
1516
const userSSID = window.localStorage.getItem('ssid') || 'unavailable';
1617
const username = window.localStorage.getItem('username') || 'unavailable';
1718
// if (userSSID !== 'guest') {
1819
// myVar = { userId: userSSID };
1920
// }
2021

22+
// --------------------------Sorting Buttons------------------------------------//
23+
// hook for sorting menu
24+
const [selectedOption, setSelectedOption] = useState(null);
25+
26+
const sortByRating = (projects) => {
27+
// console.log('sort by rating :', projects);
28+
// generate a sorted array of public projects based on likes
29+
const sortedRatings = projects.sort((a, b) => b.likes - a.likes );
30+
// console.log('sort by rating result >>>', sortedRatings);
31+
32+
// setRenderedOption(sortedRatings);
33+
return sortedRatings;
34+
};
35+
36+
const sortByDate = (projects) => {
37+
console.log('sort by date', projects);
38+
// not sure how to sort by dates
39+
};
40+
41+
const sortByUser = (projects) => {
42+
console.log('sort by user', projects);
43+
const sortedRatings = projects.sort((a, b) => b.user - a.user);
44+
console.log('sort by rating result >>>', sortedRatings);
45+
46+
return sortedRatings;
47+
};
48+
49+
50+
// ===================================================================================== //
51+
2152
// useQuery hook abstracts fetch request
22-
const { loading, error, data } = useQuery(GET_PROJECTS, { pollInterval: 2000, variables: myVar }); // Need to find where the userId is stored for the logged in user.
53+
// Need to find where the userId is stored for the logged in user.
54+
const { loading, error, data } = useQuery(GET_PROJECTS, { pollInterval: 2000, variables: myVar });
2355
if (loading) return <p>Loading...</p>;
2456
if (error) return <p>Error :{error}</p>;
2557
// based on resolver(getAllProject) for this query, the data is stored in the data object with the key 'getAllProjects'
2658
const projects = data.getAllProjects;
27-
console.log('Projects >>> ', projects);
59+
let sortedProjects = [];
60+
// console.log('Projects >>> ', projects);
2861
// generate an array of Project components based on data
29-
const publicProjects = [];
30-
const userProjects = [];
62+
let publicDisplay = [];
63+
const userDisplay = [];
3164
projects.forEach((proj, index) => {
3265
const component = <Project
3366
key= { index }
@@ -37,25 +70,65 @@ const ProjectContainer = () => {
3770
userId = {proj.userId}
3871
username = {proj.username}
3972
id = {proj.id}
40-
/>;
41-
if (username === proj.username) userProjects.push(component);
42-
if (proj.published) publicProjects.push(component);
73+
/>;
74+
if (username === proj.username) userDisplay.push(component);
75+
if (proj.published) {
76+
// publicDisplay.push(component);
77+
sortedProjects.push(proj);
78+
}
4379
});
4480

45-
46-
4781

82+
// function for sorting menu in nav bar
83+
84+
const optionClicked = (value) => {
85+
console.log('value', value);
86+
setSelectedOption(value);
87+
};
88+
89+
90+
91+
console.log('SortedProject >>> ', sortedProjects);
92+
93+
// if selectedOption === null, displaySorted = publicProjects
94+
// else displaySorted = convert to components from return value of a sortBy function)
95+
if (selectedOption === 'date') sortedProjects = sortByDate(sortedProjects);
96+
else if (selectedOption === 'user') sortedProjects = sortByUser(sortedProjects);
97+
else if (selectedOption === 'rating') sortedProjects = sortByRating(sortedProjects);
98+
99+
const sortedDisplay = [];
100+
sortedProjects.forEach((proj, index) => {
101+
console.log('Pushing to displayProjects >>> ', proj);
102+
sortedDisplay.push(<Project
103+
key= { index }
104+
name = {proj.name}
105+
likes = {proj.likes}
106+
published = { proj.published }
107+
userId = {proj.userId}
108+
username = {proj.username}
109+
id = {proj.id}
110+
/>);
111+
});
112+
console.log('displaySorted >>> ', sortedDisplay);
113+
114+
115+
// if (selectedOption === 'date') sortByDate(publicProjects);
116+
// if (selectedOption === 'user') sortByUser(publicProjects);
117+
// console.log('selectedOption', selectedOption);
118+
119+
120+
48121
return (
49-
<div>
50-
<NavBar/>
122+
<div>
123+
<NavBar optionClicked = {optionClicked}/>
51124
<h1> Public Dashboard </h1>
52125
<div className = "projectContainer">
53-
{publicProjects}
126+
{sortedDisplay}
54127
</div>
55128
<hr></hr>
56129
<h1> User Dashboard </h1>
57130
<div className = "projectContainer">
58-
{userProjects}
131+
{userDisplay}
59132
</div>
60133

61134
</div>

server/controllers/projectController.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ const projectController = {};
77
projectController.saveProject = (req, res, next) => {
88
// pull project name and project itself from body
99
const { name, project, userId, username } = req.body;
10+
// create createdBy field for the document
11+
const createdAt = Date.now();
1012
// pull ssid from cookies for user id
1113
Projects.findOneAndUpdate(
1214
// looks in projects collection for project by user and name
13-
{ name, userId, username },
15+
{ name, userId, username, createdAt },
1416
// update or insert the project
1517
{ project },
1618
// Options:

server/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,4 @@ app.listen(PORT, () => {
137137
console.log(`Server listening on port: ${PORT}`);
138138
});
139139

140-
module.export = PORT;
140+
module.exports = PORT;

0 commit comments

Comments
 (0)