Skip to content

Commit 7f18558

Browse files
update code snippet
1 parent 65410ac commit 7f18558

File tree

1 file changed

+77
-102
lines changed

1 file changed

+77
-102
lines changed

articles/azure-app-configuration/howto-variant-feature-flags-javascript.md

Lines changed: 77 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -277,123 +277,98 @@ In this tutorial, you use a variant feature flag to manage experiences for diffe
277277
import { FaHeart, FaRegHeart } from "react-icons/fa";
278278
279279
function Home() {
280-
const location = useLocation();
281-
const params = new URLSearchParams(location.search);
282-
const currentUser = params.get("userId");
283-
const [liked, setLiked] = useState(false);
284-
const [message, setMessage] = useState(undefined);
285-
286-
useEffect(() => {
287-
const init = async () => {
288-
const response = await fetch(
289-
`/api/getGreetingMessage?userId=${currentUser ?? ""}`,
290-
{
291-
method: "GET",
280+
const location = useLocation();
281+
const params = new URLSearchParams(location.search);
282+
const currentUser = params.get("userId");
283+
const [liked, setLiked] = useState(false);
284+
const [message, setMessage] = useState(undefined);
285+
286+
useEffect(() => {
287+
const init = async () => {
288+
const response = await fetch(`/api/getGreetingMessage?userId=${currentUser ?? ""}`, {
289+
method: "GET"
290+
});
291+
if (response.ok) {
292+
const result = await response.json();
293+
setMessage(result.message ?? "Quote of the Day"); // default message is "Quote of the Day"
294+
}
295+
else {
296+
console.error("Failed to get greeting message.");
297+
}
298+
setLiked(false);
299+
};
300+
init();
301+
}, []);
302+
303+
const handleClick = async () => {
304+
if (!liked) {
305+
const response = await fetch("/api/like", {
306+
method: "POST",
307+
headers: { "Content-Type": "application/json" },
308+
body: JSON.stringify({ UserId: currentUser ?? "" }),
309+
});
310+
311+
if (response.ok) {
312+
console.log("Like the quote successfully.");
313+
} else {
314+
console.error("Failed to like the quote.");
315+
}
292316
}
293-
);
294-
if (response.ok) {
295-
const result = await response.json();
296-
setMessage(result.message ?? "Quote of the Day"); // default message is "Quote of the Day"
297-
} else {
298-
console.error("Failed to get greeting message.");
299-
}
300-
setLiked(false);
317+
setLiked(!liked);
301318
};
302319
303-
init();
304-
}, []);
305-
306-
const handleClick = async () => {
307-
if (!liked) {
308-
try {
309-
const response = await fetch("/api/like", {
310-
method: "POST",
311-
headers: {
312-
"Content-Type": "application/json",
313-
},
314-
body: JSON.stringify({ UserId: currentUser ?? "" }),
315-
});
316-
317-
if (response.ok) {
318-
console.log("Like the quote successfully.");
319-
} else {
320-
console.error("Failed to like the quote.");
321-
}
322-
} catch (error) {
323-
console.error("Error:", error);
324-
}
325-
}
326-
setLiked(!liked);
327-
};
328-
329-
return (
330-
<div className="quote-card">
331-
{ message != undefined ?
332-
(
333-
<>
334-
<h2>
335-
<>{message}</>
336-
</h2>
337-
<blockquote>
338-
<p>"You cannot change what you are, only what you do."</p>
339-
<footer>— Philip Pullman</footer>
340-
</blockquote>
341-
<div className="vote-container">
342-
<button className="heart-button" onClick={handleClick}>
343-
{liked ? <FaHeart /> : <FaRegHeart />}
344-
</button>
320+
return (
321+
<div className="quote-page">
322+
<header className="navbar">
323+
<div className="navbar-left">
324+
<div className="logo">QuoteOfTheDay</div>
325+
</div>
326+
</header>
327+
328+
<main className="quote-container">
329+
<div className="quote-card">
330+
{ message != undefined ?
331+
(
332+
<>
333+
<h2>
334+
<>{message}</>
335+
</h2>
336+
<blockquote>
337+
<p>"You cannot change what you are, only what you do."</p>
338+
<footer>— Philip Pullman</footer>
339+
</blockquote>
340+
<div className="vote-container">
341+
<button className="heart-button" onClick={handleClick}>
342+
{liked ? <FaHeart /> : <FaRegHeart />}
343+
</button>
344+
</div>
345+
</>
346+
)
347+
: <p>Loading</p>
348+
}
349+
</div>
350+
</main>
345351
</div>
346-
</>
347-
)
348-
: <p>Loading</p>
349-
}
350-
</div>
351-
);
352+
);
352353
}
353354
354-
export default Home
355-
```
356-
357-
1. Create a new file named *Layout.jsx*.
358-
359-
```jsx
360-
const Layout = ({ children }) => {
361-
return (
362-
<div className="quote-page">
363-
<header className="navbar">
364-
<div className="navbar-left">
365-
<div className="logo">QuoteOfTheDay</div>
366-
</div>
367-
</header>
368-
369-
<main className="quote-container">
370-
{children}
371-
</main>
372-
373-
</div>
374-
);
375-
};
376-
377-
export default Layout;
355+
export default Home;
378356
```
379357
380358
1. Update the *App.jsx* file.
381359
382360
```jsx
383361
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
384-
import Layout from "./Layout";
385362
import Home from "./Home";
386363
387364
function App() {
388-
return (
389-
<Router>
390-
<Layout>
391-
<Routes>
392-
<Route path="/" element={<Home />} />
393-
</Routes>
394-
</Layout>
395-
</Router>
396-
);
365+
return (
366+
<Router>
367+
<Routes>
368+
<Route path="/" element={<Home />} />
369+
</Routes>
370+
</Router>
371+
);
397372
}
398373
399374
export default App;

0 commit comments

Comments
 (0)