-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'tannerlinsley/master' into alpha
# Conflicts: # src/reactjs/tests/useIsFetching.test.tsx # src/reactjs/useIsFetching.ts
- Loading branch information
Showing
20 changed files
with
524 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React from 'react' | ||
import useBytesSubmit from './useBytesSubmit' | ||
|
||
export default function BytesForm() { | ||
const { state, handleSubmit, error } = useBytesSubmit() | ||
if (state === 'submitted') { | ||
return ( | ||
<p>Success! Please, check your email to confirm your subscription.</p> | ||
) | ||
} | ||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<div data-element="fields" className="grid relative"> | ||
<figure | ||
className="absolute right-0" | ||
style={{ bottom: '72px', right: '-10px' }} | ||
> | ||
<img | ||
height={40} | ||
width={40} | ||
src="/images/bytes-logo.png" | ||
alt="Bytes" | ||
/> | ||
</figure> | ||
<input | ||
className="border rounded p-2 mb-2 w-full" | ||
name="email_address" | ||
placeholder="Your email address" | ||
type="email" | ||
required="" | ||
/> | ||
<button | ||
type="submit" | ||
className="mb-4 border rounded bg-coral border-none text-white p-2" | ||
> | ||
{state !== 'loading' ? ( | ||
<span>Subscribe</span> | ||
) : ( | ||
<span>Loading...</span> | ||
)} | ||
</button> | ||
</div> | ||
<p className="text-gray-400 text-xs"> | ||
No spam. Unsubscribe at <em>any</em> time. | ||
</p> | ||
{error && <p className="text-red-600">{error}</p>} | ||
</form> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as React from 'react' | ||
import { flag } from 'country-emoji' | ||
import useLocalStorage from './useLocalStorage' | ||
import { AnimatePresence, motion } from 'framer-motion' | ||
import { IoIosClose } from 'react-icons/io' | ||
|
||
function useClientOnlyRender() { | ||
const [rendered, setRendered] = React.useState(false) | ||
React.useEffect(() => { | ||
setRendered(true) | ||
}, []) | ||
return rendered | ||
} | ||
export function PPPBanner() { | ||
const [hidden, setHidden] = useLocalStorage('pppbanner-hidden', false) | ||
const [data, setData] = useLocalStorage('pppbanner-data', null) | ||
|
||
React.useEffect(() => { | ||
// This function has CORS configured to allow | ||
// react-query.tanstack.com and tanstack.com | ||
if (!data) { | ||
fetch('https://ui.dev/api/ppp-discount') | ||
.then(res => res.json()) | ||
.then(res => { | ||
if (res?.code) { | ||
setData(res) | ||
} | ||
}) | ||
} | ||
}, [data, setData]) | ||
|
||
if (!useClientOnlyRender()) { | ||
return null | ||
} | ||
|
||
return ( | ||
<AnimatePresence initial={false}> | ||
{data && !hidden && ( | ||
<motion.div | ||
initial={{ opacity: 0, height: 0 }} | ||
animate={{ opacity: 1, height: 'auto' }} | ||
exit={{ opacity: 0, height: 0 }} | ||
className="w-full bg-coral text-white text-center py-2 relative flex items-center justify-center" | ||
> | ||
<p> | ||
{flag(data.code)} We noticed you're in{' '} | ||
<strong>{data.country}</strong>. Get{' '} | ||
<strong>{data.discount * 100}% off</strong> the Official React Query | ||
Course with code{' '} | ||
<a | ||
className="underline cursor-pointer" | ||
href={`/checkout/react-query?from=tanstack&coupon_code=${data.coupon}`} | ||
> | ||
<strong>{data.coupon}</strong> | ||
</a> | ||
. | ||
</p> | ||
<button | ||
onClick={() => setHidden(true)} | ||
className="absolute right-0" | ||
aria-label="Hide Banner" | ||
> | ||
<IoIosClose size={30} className="text-white" /> | ||
</button> | ||
</motion.div> | ||
)} | ||
</AnimatePresence> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useState } from 'react'; | ||
|
||
function sendBytesOptIn({ email, influencer, source, referral }) { | ||
return fetch(`https://bytes.dev/api/bytes-optin-cors`, { | ||
method: 'POST', | ||
body: JSON.stringify({ email, influencer, source, referral }), | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
}).then((res) => res.json()) | ||
} | ||
|
||
export default function useBytesSubmit() { | ||
const [state, setState] = useState("initial"); | ||
const [error, setError] = useState(null); | ||
|
||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
const email = e.target.email_address.value; | ||
setState("loading"); | ||
sendBytesOptIn({ email, influencer: "tanstack" }) | ||
.then(() => { | ||
setState("submitted"); | ||
}) | ||
.catch((err) => { | ||
setError(err); | ||
}); | ||
}; | ||
|
||
return { handleSubmit, state, error }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as React from 'react' | ||
export default function useLocalStorage(key, initialValue) { | ||
// State to store our value | ||
// Pass initial state function to useState so logic is only executed once | ||
const [storedValue, setStoredValue] = React.useState(() => { | ||
if (typeof window !== 'undefined') { | ||
const item = window.localStorage.getItem(key) | ||
try { | ||
// Get from local storage by key | ||
// Parse stored json or if none return initialValue | ||
return item ? JSON.parse(item) : initialValue | ||
} catch (error) { | ||
return item | ||
} | ||
} | ||
return initialValue | ||
}) | ||
// Return a wrapped version of useState's setter function that ... | ||
// ... persists the new value to localStorage. | ||
const setValue = React.useCallback( | ||
value => { | ||
try { | ||
// Allow value to be a function so we have same API as useState | ||
// Save state | ||
setStoredValue(newStoredValue => { | ||
const valueToStore = | ||
value instanceof Function ? value(newStoredValue) : value | ||
window.localStorage.setItem(key, JSON.stringify(valueToStore)) | ||
return valueToStore | ||
}) | ||
// Save to local storage | ||
} catch (error) { | ||
// A more advanced implementation would handle the error case | ||
} | ||
}, | ||
[key] | ||
) | ||
return [storedValue, setValue] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.