11import "./styles/HomeView.css" ;
2- import { Outlet , useLocation , useNavigate , useParams } from "react-router-dom" ;
2+ import { useLocation , useNavigate , useParams } from "react-router-dom" ;
33import { useContext , useEffect , useState } from "react" ;
44import HomeViewHeader from "./HomeViewHeader" ;
55import HomeViewFooter from "./HomeViewFooter" ;
66import AppContext from "../contexts/AppContext" ;
77import AppConfig from "../AppConfig.json" ;
88import LandingPage from "./LandingPage" ;
9+ import HomeViewContent from "./HomeViewContent" ;
910
1011interface Props { }
1112
@@ -19,88 +20,106 @@ const HomeView = ({}: Props) => {
1920 const param2_Opts = [ "portfolio" , "blog" , "introduction" , "intro" ] ;
2021 const param3_Opts = [ "gamedev" , "webdev" ] ;
2122 const param4_Opts = AppConfig . projectURL_Params ;
22- // useStates for Interfacing with URL Params
2323 const [ param1 , setParam1 ] = useState < string > ( "" ) ;
2424 const [ param2 , setParam2 ] = useState < string > ( "" ) ;
2525 const [ param3 , setParam3 ] = useState < string > ( "" ) ;
2626 const [ param4 , setParam4 ] = useState < string > ( "" ) ;
27- const [ allParamsObj , setAllParamsObj ] = useState < any > ( {
27+ const [ allParamsArray , setAllParamsArray ] = useState < string [ ] > ( [
2828 param1 ,
2929 param2 ,
3030 param3 ,
3131 param4 ,
32- } ) ;
33- const setParamsArray = [ setParam1 , setParam2 , setParam3 , setParam4 ] ;
32+ ] ) ;
33+ const paramStateSet = [ setParam1 , setParam2 , setParam3 , setParam4 ] ;
3434 // check if this is the landing page
3535 const [ isLanding , setIsLanding ] = useState < boolean > ( false ) ;
3636 // - - - - CONTEXT - - - -
3737 const { hueRotation, setHueDuration } = useContext ( AppContext ) ;
3838 // - - - - - TITLES AND TEXT - - - - -
39- const [ currentContent , setCurrentContent ] = useState < string > ( "Deerfall" ) ;
40- const [ title , setTitle ] = useState < string > ( "Dev Blog" ) ;
4139 const [ subtitle , setSubtitle ] = useState < string > ( "Welcome! " ) ;
4240 const [ subEmoji , setSubEmoji ] = useState < string > ( " 🙂" ) ;
4341 // - - - - - PROJECTS - - - - -
44- const gameDevProjList = [ "Deerfall " ] ;
45- const webDevProjList = [ "MediaMatchup " ] ;
42+ const gameDevProjList = [ "deerfall " ] ;
43+ const webDevProjList = [ "mediamatchup " ] ;
4644
4745 // - - - - - FUNCTIONS - - - - -
48- const checkURL = ( ) => {
46+
47+ const validateURL = ( ) => {
48+ // INITIAL SEGMENT VALIDATION
49+ let tempParams = allParamsArray ;
4950 let URLSegments = [ landingOrHome , category1 , gameOrWeb , project ] ;
5051 let paramArray = [ param1_Opts , param2_Opts , param3_Opts , param4_Opts ] ;
52+ // checks for landing page
5153 if ( location . pathname === "/" ) {
54+ tempParams = [ "" ] ;
5255 navigate ( "/landing" ) ;
5356 setIsLanding ( true ) ;
54- } else if ( location . pathname === "/landing" ) {
57+ } else if ( landingOrHome === "landing" ) {
58+ tempParams = [ "/landing" ] ;
5559 setIsLanding ( true ) ;
5660 }
61+ // loops through each segment and validates it individually
5762 for ( let i = 0 ; i < URLSegments . length ; i ++ ) {
5863 if ( URLSegments [ i ] ) {
5964 let found = paramArray [ i ] . find ( ( item ) => item === URLSegments [ i ] ) ;
6065 if ( found === undefined ) {
6166 navigate ( "/404NotFound" ) ;
6267 } else {
63- setParamsArray [ i ] ( found ) ;
68+ // setParamsArray[i](found);
69+ paramStateSet [ i ] ( found ) ;
70+ tempParams [ i ] = found ;
6471 }
6572 }
6673 }
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+ }
6795 } ;
6896
6997 // - - - - - useEffects - - - - -
7098 useEffect ( ( ) => {
71- setAllParamsObj ( { param1, param2, param3, param4 } ) ;
72- } , [ param1 , param2 , param3 , param4 ] ) ;
73-
74- useEffect ( ( ) => {
99+ validateURL ( ) ;
75100 if ( ! isLanding ) {
76- checkURL ( ) ;
77101 if ( hueRotation != AppConfig . hueAnimDuration_Slow ) {
78102 setHueDuration ( AppConfig . hueAnimDuration_Slow ) ;
79103 }
80104 }
81- console . log ( category1 ) ;
82105 } , [ location ] ) ;
83106
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+
84112 // - - - - - JSX - - - - -
85113 return (
86114 < div className = 'HomeView' >
87- { isLanding ? (
88- < LandingPage
89- setIsLanding = { setIsLanding }
90- setParamsArray = { setParamsArray }
91- />
92- ) : (
93- ""
94- ) }
115+ { isLanding ? < LandingPage setIsLanding = { setIsLanding } /> : "" }
95116 < HomeViewHeader
96117 subtitle = { subtitle }
97118 subEmoji = { subEmoji }
98- currentContent = { currentContent }
99- allParamsObj = { allParamsObj }
100- isLanding = { isLanding }
119+ allParams = { allParamsArray }
101120 />
102- < Outlet />
103- < HomeViewFooter currentContent = { "deerfall" } allParamsObj = { allParamsObj } />
121+ < HomeViewContent currentContent = { param4 } />
122+ < HomeViewFooter allParams = { allParamsArray } />
104123 </ div >
105124 ) ;
106125} ;
0 commit comments