1
1
import "./styles/HomeView.css" ;
2
- import { Outlet , useLocation , useNavigate , useParams } from "react-router-dom" ;
2
+ import { useLocation , useNavigate , useParams } from "react-router-dom" ;
3
3
import { useContext , useEffect , useState } from "react" ;
4
4
import HomeViewHeader from "./HomeViewHeader" ;
5
5
import HomeViewFooter from "./HomeViewFooter" ;
6
6
import AppContext from "../contexts/AppContext" ;
7
7
import AppConfig from "../AppConfig.json" ;
8
8
import LandingPage from "./LandingPage" ;
9
+ import HomeViewContent from "./HomeViewContent" ;
9
10
10
11
interface Props { }
11
12
@@ -19,88 +20,106 @@ const HomeView = ({}: Props) => {
19
20
const param2_Opts = [ "portfolio" , "blog" , "introduction" , "intro" ] ;
20
21
const param3_Opts = [ "gamedev" , "webdev" ] ;
21
22
const param4_Opts = AppConfig . projectURL_Params ;
22
- // useStates for Interfacing with URL Params
23
23
const [ param1 , setParam1 ] = useState < string > ( "" ) ;
24
24
const [ param2 , setParam2 ] = useState < string > ( "" ) ;
25
25
const [ param3 , setParam3 ] = useState < string > ( "" ) ;
26
26
const [ param4 , setParam4 ] = useState < string > ( "" ) ;
27
- const [ allParamsObj , setAllParamsObj ] = useState < any > ( {
27
+ const [ allParamsArray , setAllParamsArray ] = useState < string [ ] > ( [
28
28
param1 ,
29
29
param2 ,
30
30
param3 ,
31
31
param4 ,
32
- } ) ;
33
- const setParamsArray = [ setParam1 , setParam2 , setParam3 , setParam4 ] ;
32
+ ] ) ;
33
+ const paramStateSet = [ setParam1 , setParam2 , setParam3 , setParam4 ] ;
34
34
// check if this is the landing page
35
35
const [ isLanding , setIsLanding ] = useState < boolean > ( false ) ;
36
36
// - - - - CONTEXT - - - -
37
37
const { hueRotation, setHueDuration } = useContext ( AppContext ) ;
38
38
// - - - - - TITLES AND TEXT - - - - -
39
- const [ currentContent , setCurrentContent ] = useState < string > ( "Deerfall" ) ;
40
- const [ title , setTitle ] = useState < string > ( "Dev Blog" ) ;
41
39
const [ subtitle , setSubtitle ] = useState < string > ( "Welcome! " ) ;
42
40
const [ subEmoji , setSubEmoji ] = useState < string > ( " 🙂" ) ;
43
41
// - - - - - PROJECTS - - - - -
44
- const gameDevProjList = [ "Deerfall " ] ;
45
- const webDevProjList = [ "MediaMatchup " ] ;
42
+ const gameDevProjList = [ "deerfall " ] ;
43
+ const webDevProjList = [ "mediamatchup " ] ;
46
44
47
45
// - - - - - FUNCTIONS - - - - -
48
- const checkURL = ( ) => {
46
+
47
+ const validateURL = ( ) => {
48
+ // INITIAL SEGMENT VALIDATION
49
+ let tempParams = allParamsArray ;
49
50
let URLSegments = [ landingOrHome , category1 , gameOrWeb , project ] ;
50
51
let paramArray = [ param1_Opts , param2_Opts , param3_Opts , param4_Opts ] ;
52
+ // checks for landing page
51
53
if ( location . pathname === "/" ) {
54
+ tempParams = [ "" ] ;
52
55
navigate ( "/landing" ) ;
53
56
setIsLanding ( true ) ;
54
- } else if ( location . pathname === "/landing" ) {
57
+ } else if ( landingOrHome === "landing" ) {
58
+ tempParams = [ "/landing" ] ;
55
59
setIsLanding ( true ) ;
56
60
}
61
+ // loops through each segment and validates it individually
57
62
for ( let i = 0 ; i < URLSegments . length ; i ++ ) {
58
63
if ( URLSegments [ i ] ) {
59
64
let found = paramArray [ i ] . find ( ( item ) => item === URLSegments [ i ] ) ;
60
65
if ( found === undefined ) {
61
66
navigate ( "/404NotFound" ) ;
62
67
} else {
63
- setParamsArray [ i ] ( found ) ;
68
+ // setParamsArray[i](found);
69
+ paramStateSet [ i ] ( found ) ;
70
+ tempParams [ i ] = found ;
64
71
}
65
72
}
66
73
}
74
+ // Sets Default Project if none is selected in this category
75
+ let isGameDev = tempParams [ 2 ] === "gamedev" ;
76
+ let isWebDev = tempParams [ 2 ] === "webdev" ;
77
+ let isGameDevProj = gameDevProjList . includes ( tempParams [ 3 ] ) ;
78
+ let isWebDevProj = webDevProjList . includes ( tempParams [ 3 ] ) ;
79
+
80
+ if ( isGameDev && ! isGameDevProj ) {
81
+ setParam4 ( gameDevProjList [ 0 ] ) ;
82
+ tempParams [ 3 ] = gameDevProjList [ 0 ] ;
83
+ } else if ( isWebDev && ! isWebDevProj ) {
84
+ setParam4 ( webDevProjList [ 0 ] ) ;
85
+ tempParams [ 3 ] = webDevProjList [ 0 ] ;
86
+ }
87
+
88
+ let construct_URL = `/${ tempParams [ 0 ] } /${ tempParams [ 1 ] } /${ tempParams [ 2 ] } /${ tempParams [ 3 ] } ` ;
89
+ let isSame_URL = construct_URL === location . pathname ;
90
+ let isIntroduction_URL = tempParams [ 1 ] === "introduction" ;
91
+
92
+ if ( tempParams [ 3 ] && ! isSame_URL && ! isIntroduction_URL ) {
93
+ navigate ( construct_URL , { replace : true } ) ;
94
+ }
67
95
} ;
68
96
69
97
// - - - - - useEffects - - - - -
70
98
useEffect ( ( ) => {
71
- setAllParamsObj ( { param1, param2, param3, param4 } ) ;
72
- } , [ param1 , param2 , param3 , param4 ] ) ;
73
-
74
- useEffect ( ( ) => {
99
+ validateURL ( ) ;
75
100
if ( ! isLanding ) {
76
- checkURL ( ) ;
77
101
if ( hueRotation != AppConfig . hueAnimDuration_Slow ) {
78
102
setHueDuration ( AppConfig . hueAnimDuration_Slow ) ;
79
103
}
80
104
}
81
- console . log ( category1 ) ;
82
105
} , [ location ] ) ;
83
106
107
+ // Necessary to update props. Otherwise, props are always a step behind.
108
+ useEffect ( ( ) => {
109
+ setAllParamsArray ( [ param1 , param2 , param3 , param4 ] ) ;
110
+ } , [ param1 , param2 , param3 , param4 ] ) ;
111
+
84
112
// - - - - - JSX - - - - -
85
113
return (
86
114
< div className = 'HomeView' >
87
- { isLanding ? (
88
- < LandingPage
89
- setIsLanding = { setIsLanding }
90
- setParamsArray = { setParamsArray }
91
- />
92
- ) : (
93
- ""
94
- ) }
115
+ { isLanding ? < LandingPage setIsLanding = { setIsLanding } /> : "" }
95
116
< HomeViewHeader
96
117
subtitle = { subtitle }
97
118
subEmoji = { subEmoji }
98
- currentContent = { currentContent }
99
- allParamsObj = { allParamsObj }
100
- isLanding = { isLanding }
119
+ allParams = { allParamsArray }
101
120
/>
102
- < Outlet />
103
- < HomeViewFooter currentContent = { "deerfall" } allParamsObj = { allParamsObj } />
121
+ < HomeViewContent currentContent = { param4 } />
122
+ < HomeViewFooter allParams = { allParamsArray } />
104
123
</ div >
105
124
) ;
106
125
} ;
0 commit comments