Skip to content

Commit

Permalink
add use Window helper
Browse files Browse the repository at this point in the history
  • Loading branch information
NickFoden committed Aug 12, 2021
1 parent 6ec33de commit 9bae776
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 6 deletions.
16 changes: 11 additions & 5 deletions pages/api/hello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ type Data = {
};

export default (req: NextApiRequest, res: NextApiResponse<Data>) => {
if (req.method === "POST") {
//@ts-expect-error
res.status(200).json({ name: process.env.API_KEY });
} else {
res.status(200).json({ name: "John Doe" });
console.log(req.query)
const hasAnimal = req.query["animal"]

if(hasAnimal){
send other mailchimp
}
// if (req.method === "POST") {
// //@ts-expect-error
// res.status(200).json({ name: process.env.API_KEY });
// } else {
res.status(200).json({ name: JSON.stringify(req.query) });
// }
};
96 changes: 96 additions & 0 deletions pages/contact.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React, { useEffect, useRef, useState } from "react";

const ContactPage = () => {
const [state, setState] = useState({
myName: "",
otherState: false,
});

const aComplexValue = () => {
return Math.random();
};

const [expensive] = useState(aComplexValue());

const myRef = useRef(null);
console.log(expensive);

function emailIsValid (email:string) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)
}

const handleChange = (e ) => {
console.dir(e)
const { name, value } = e.target;

if(e){

}
setState((s) => ({ ...s, [name]: value }));
};

const handleSubmit = async () => {
const payload = encodeURI("cool stuff yes many spaces ")

await fetch(`/api/hello?animal=${payload}`)

};
// prevState
// setState((s) => ({ ...s, "myName": value }));

// setState({"myName": value });
// };

const validateField = () => {
// if (state.myName.length < 5 || !state.myName.includes("@")) {
// alert("Invalid email");
// }
};

// useEffect(() => {
// if (state.myName.length > 5) {
// alert("Over 5");
// }
// }, [state]);

// useEffect(() => {
// //@ts-expect-error
// if ( myRef?.current?.value?.length > 4) {
// alert("Over 4 ref");
// }
// }, [myRef]);

return (
<div>
<label>
Email:
<input
aria-label="email for sign up"
name="myName"
// type="text"
onChange={handleChange}
onBlur={validateField}
placeholder="mike"
// value={state.myName}
/>
</label>
{/* <input name="name2" type="text"
onChange={handleChange}
style={{...styles, fontSize:"50px"}}
placeholder="mike"
// value={state.myName}
/> */}
<button
onClick={() => {
handleSubmit()
console.dir(myRef.current);
console.dir(myRef);
}}
>
Submit
</button>
</div>
);
};

export default ContactPage;
43 changes: 43 additions & 0 deletions src/hooks/useWindowSize.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useState, useEffect } from "react";
// Usage
// function App() {
// const size = useWindowSize();
// return (
// <div>
// {size.width}px / {size.height}px
// </div>
// );
// }


// Hook
function useWindowSize() {
// Initialize state with undefined width/height so server and client renders match
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
const [windowSize, setWindowSize] = useState< {
width: undefined | number,
height: undefined| number,
}>({
width: undefined,
height: undefined,
});
useEffect(() => {
// Handler to call on window resize
function handleResize() {
// Set window width/height to state
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
// Add event listener
typeof window !== undefined && window.addEventListener("resize", handleResize);
// Call handler right away so state gets updated with initial window size
handleResize();
// Remove event listener on cleanup
return () => window.removeEventListener("resize", handleResize);
}, []); // Empty array ensures that effect is only run on mount
return windowSize;
}

export default useWindowSize
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "pages/random.js", "pages/random.js"],
"exclude": ["node_modules"]
}

0 comments on commit 9bae776

Please sign in to comment.