1
1
import React , { useState , useContext , useEffect , createContext } from 'react' ;
2
2
import { createMuiTheme , MuiThemeProvider } from '@material-ui/core/styles' ;
3
- import { makeStyles } from '@material-ui/core/styles' ;
4
- import Grid from '@material-ui/core/Grid' ;
5
3
import { useQuery } from '@apollo/client' ;
6
4
import { GET_PROJECTS } from './gqlStrings' ;
7
5
import Project from './Project' ;
@@ -22,15 +20,27 @@ const lightTheme = theme1;
22
20
const darkTheme = theme2 ; // dark mode color in theme.ts not reached
23
21
24
22
23
+ const arrToComponent = ( arr ) => {
24
+ return arr . map ( ( proj , index ) => < Project
25
+ key = { index }
26
+ name = { proj . name }
27
+ likes = { proj . likes }
28
+ published = { proj . published }
29
+ userId = { proj . userId }
30
+ username = { proj . username }
31
+ createdAt = { proj . createdAt }
32
+ id = { proj . id }
33
+ comments = { proj . comments }
34
+ /> ) ;
35
+ } ;
36
+
37
+
25
38
const ProjectContainer = ( ) => {
26
39
const myVar = { } ;
27
40
// Need this for the individual user dasboard, for now, dashboard shows all projects from all users
28
41
const userSSID = window . localStorage . getItem ( 'ssid' ) || 'unavailable' ;
29
42
const username = window . localStorage . getItem ( 'username' ) || 'unavailable' ;
30
- // if (userSSID !== 'guest') {
31
- // myVar = { userId: userSSID };
32
- // }
33
-
43
+
34
44
const [ isThemeLight , setTheme ] = useState ( true ) ;
35
45
36
46
const initialStyle = useContext ( styleContext ) ;
@@ -39,11 +49,11 @@ const ProjectContainer = () => {
39
49
// --------------------------Sorting Buttons------------------------------------//
40
50
41
51
// hook for sorting menu
42
- const [ selectedOption , setSelectedOption ] = useState ( null ) ;
52
+ const [ selectedOption , setSelectedOption ] = useState ( 'RATING' ) ;
43
53
44
54
const sortByRating = ( projects ) => {
45
55
// generate a sorted array of public projects based on likes
46
- const sortedRatings = projects . sort ( ( a , b ) => b . likes - a . likes ) ;
56
+ const sortedRatings = projects . sort ( ( a , b ) => b . likes - a . likes ) ;
47
57
return sortedRatings ;
48
58
} ;
49
59
@@ -55,81 +65,56 @@ const ProjectContainer = () => {
55
65
56
66
const sortByUser = ( projects ) => {
57
67
// generate a sorted array of public projects based on username
58
- const sortedRatings = projects . sort ( ( a , b ) => b . user - a . user ) ;
68
+ const sortedRatings = projects . sort ( ( a , b ) => b . username - a . username ) ;
59
69
return sortedRatings ;
60
70
} ;
61
71
// ===================================================================================== //
62
72
73
+ // function for selecting drop down sorting menu
74
+ const optionClicked = ( value ) => {
75
+ setSelectedOption ( value ) ;
76
+ } ;
77
+ // ===================================================================================== //
78
+
79
+
63
80
// useQuery hook abstracts fetch request
64
81
const { loading, error, data } = useQuery ( GET_PROJECTS , { pollInterval : 2000 , variables : myVar } ) ;
65
82
if ( loading ) return < p > Loading...</ p > ;
66
83
if ( error ) return < p > Error :{ error } </ p > ;
67
84
68
85
// based on resolver(getAllProject) for this query, the data is stored in the data object with the key 'getAllProjects'
69
86
const projects = data . getAllProjects ;
87
+
70
88
// create array to hold the data recieved in the public dashboard the will be conditionally rendered
71
- let sortedProjects = [ ] ;
72
- // create array to hold the components Project of loggin-in users
73
- const userDisplay = [ ] ;
74
- // generate an array of Project components based on queried data
75
- projects . forEach ( ( proj , index ) => {
76
- const component = < Project
77
- key = { index }
78
- name = { proj . name }
79
- likes = { proj . likes }
80
- published = { proj . published }
81
- userId = { proj . userId }
82
- username = { proj . username }
83
- createdAt = { proj . createdAt }
84
- id = { proj . id }
85
- comments = { proj . comments }
86
- /> ;
87
- // sorting the public and private dashboards based on the user's username
88
- if ( username === proj . username ) userDisplay . push ( component ) ;
89
- if ( proj . published ) {
90
-
91
- sortedProjects . push ( proj ) ;
92
- }
93
- } ) ;
89
+ let sortedProjects = projects . filter ( proj => proj . published ) ;
90
+ const userProjects = projects . filter ( proj => proj . username === username ) ;
94
91
95
- // function for selecting drop down sorting menu
96
- const optionClicked = ( value ) => {
97
- setSelectedOption ( value ) ;
98
- } ;
99
92
// checking which sorting method was selected from drop down menu and invoking correct sorting function
100
93
if ( selectedOption === 'DATE' ) sortedProjects = sortByDate ( sortedProjects ) ;
101
94
else if ( selectedOption === 'USER' ) sortedProjects = sortByUser ( sortedProjects ) ;
102
95
else if ( selectedOption === 'RATING' ) sortedProjects = sortByRating ( sortedProjects ) ;
103
96
97
+
98
+ // create array to hold the components Project of loggin-in users
99
+ // generate an array of Project components based on queried data
100
+ const userDisplay = arrToComponent ( userProjects ) ;
101
+
104
102
// create an array of components Project that will be conditionally rendered
105
- const sortedDisplay = [ ] ;
106
- sortedProjects . forEach ( ( proj , index ) => {
107
- sortedDisplay . push ( < Project
108
- key = { index }
109
- name = { proj . name }
110
- likes = { proj . likes }
111
- published = { proj . published }
112
- userId = { proj . userId }
113
- username = { proj . username }
114
- createdAt = { proj . createdAt }
115
- id = { proj . id }
116
- comments = { proj . comments }
117
- /> ) ;
118
- } ) ;
103
+ const sortedDisplay = arrToComponent ( sortedProjects ) ;
119
104
120
105
return (
121
106
< MuiThemeProvider theme = { isThemeLight ? lightTheme : darkTheme } >
122
107
< div className = "dashboardContainer" >
123
108
< NavBarDash setTheme = { setTheme } styles = { [ style , setStyle ] } isThemeLight = { isThemeLight } optionClicked = { optionClicked } />
124
109
< h1 > Public Dashboard </ h1 >
125
- < Grid className = "projectContainer" container justify = 'flex-start' wrap = 'wrap' spacing = { 3 } >
110
+ < div className = "projectContainer" >
126
111
{ sortedDisplay }
127
- </ Grid >
112
+ </ div >
128
113
< hr > </ hr >
129
114
< h1 > User Dashboard </ h1 >
130
- < Grid className = "projectContainer" container justify = 'flex-start' wrap = 'wrap' >
115
+ < div className = "projectContainer" >
131
116
{ userDisplay }
132
- </ Grid >
117
+ </ div >
133
118
</ div >
134
119
</ MuiThemeProvider >
135
120
) ;
0 commit comments