1
- import React from 'react' ;
1
+ import React , { useState , useEffect } from 'react' ;
2
2
import { Link } from 'react-router-dom' ;
3
3
import { useQuery } from '@apollo/client' ;
4
+
4
5
import { GET_PROJECTS } from './gqlStrings' ;
5
6
import Project from './Project.tsx' ;
6
7
import NavBar from './NavbarDash' ;
@@ -10,24 +11,56 @@ import NavBar from './NavbarDash';
10
11
// 2) useQuery hook will update the data stored in Apollo Client's cache and automatically trigger child components rendering
11
12
12
13
const ProjectContainer = ( ) => {
13
- let myVar = { } ;
14
+ const myVar = { } ;
14
15
// Need this for the individual user dasboard, for now, dashboard shows all projects from all users
15
16
const userSSID = window . localStorage . getItem ( 'ssid' ) || 'unavailable' ;
16
17
const username = window . localStorage . getItem ( 'username' ) || 'unavailable' ;
17
18
// if (userSSID !== 'guest') {
18
19
// myVar = { userId: userSSID };
19
20
// }
20
21
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
+
21
52
// 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 } ) ;
23
55
if ( loading ) return < p > Loading...</ p > ;
24
56
if ( error ) return < p > Error :{ error } </ p > ;
25
57
// based on resolver(getAllProject) for this query, the data is stored in the data object with the key 'getAllProjects'
26
58
const projects = data . getAllProjects ;
27
- console . log ( 'Projects >>> ' , projects ) ;
59
+ let sortedProjects = [ ] ;
60
+ // console.log('Projects >>> ', projects);
28
61
// generate an array of Project components based on data
29
- const publicProjects = [ ] ;
30
- const userProjects = [ ] ;
62
+ let publicDisplay = [ ] ;
63
+ const userDisplay = [ ] ;
31
64
projects . forEach ( ( proj , index ) => {
32
65
const component = < Project
33
66
key = { index }
@@ -37,25 +70,65 @@ const ProjectContainer = () => {
37
70
userId = { proj . userId }
38
71
username = { proj . username }
39
72
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
+ }
43
79
} ) ;
44
80
45
-
46
-
47
81
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
+
48
121
return (
49
- < div >
50
- < NavBar />
122
+ < div >
123
+ < NavBar optionClicked = { optionClicked } />
51
124
< h1 > Public Dashboard </ h1 >
52
125
< div className = "projectContainer" >
53
- { publicProjects }
126
+ { sortedDisplay }
54
127
</ div >
55
128
< hr > </ hr >
56
129
< h1 > User Dashboard </ h1 >
57
130
< div className = "projectContainer" >
58
- { userProjects }
131
+ { userDisplay }
59
132
</ div >
60
133
61
134
</ div >
0 commit comments