Skip to content

Commit

Permalink
Use querystring for FAQ state
Browse files Browse the repository at this point in the history
  • Loading branch information
zack committed Jul 25, 2024
1 parent 0f069c7 commit f336001
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 40 deletions.
2 changes: 2 additions & 0 deletions app/FAQ.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';

import { exoFontFamily } from './ThemeProvider';

import { Box, Typography } from '@mui/material';

export default function FAQ() {
Expand Down
38 changes: 38 additions & 0 deletions app/FAQButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use client';

import CancelIcon from '@mui/icons-material/Cancel';
import HelpIcon from '@mui/icons-material/Help';
import React from 'react';
import { Typography } from '@mui/material';

import styled from '@emotion/styled';
import { useQueryState } from 'nuqs';

export default function FAQButton() {
const [faq, setFaq] = useQueryState('faq');

return faq ? (
<StyledButton onClick={() => setFaq(null)}>
<CancelIcon /> <Typography> Back </Typography>
</StyledButton>
) : (
<StyledButton onClick={() => setFaq('open')}>
<HelpIcon /> <Typography> FAQ </Typography>
</StyledButton>
);
}

const StyledButton = styled.button`
background: white;
border: 0;
cursor: pointer;
width: 70px;
&:hover {
border-radius: 5px;
outline: 3px solid #0150b4;
p {
font-weight: bold;
}
}
`;
23 changes: 23 additions & 0 deletions app/FAQContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use client';

import { Box } from '@mui/material';
import FAQ from './FAQ';
import React from 'react';
import ViewPicker from './ViewPicker';

import { useQueryState } from 'nuqs';

export default function FAQContainer() {
const [faqOpen, _] = useQueryState('faq');

return (
<>
{faqOpen && <FAQ />}

{/* Don't lose state when FAQ is opened */}
<Box sx={{ display: faqOpen ? 'none' : 'block', height: '100%' }}>
<ViewPicker />
</Box>
</>
);
}
44 changes: 4 additions & 40 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
'use client';

import CancelIcon from '@mui/icons-material/Cancel';
import FAQ from './FAQ';
import HelpIcon from '@mui/icons-material/Help';
import ViewPicker from './ViewPicker';
import FAQButton from './FAQButton';
import FAQContainer from './FAQContainer';

import { exoFontFamily } from './ThemeProvider';

import styled from '@emotion/styled';

import { Box, Grid, Typography } from '@mui/material';

import React, { memo } from 'react';

export default memo(function Home() {
const [faq, setFaq] = React.useState(false);

return (
<main>
<Grid
Expand Down Expand Up @@ -53,41 +45,13 @@ export default memo(function Home() {
</Typography>

<Box sx={{ display: 'flex-item', mb: 1 }}>
{faq ? (
<StyledButton onClick={() => setFaq(false)}>
<CancelIcon /> <Typography> Back </Typography>
</StyledButton>
) : (
<StyledButton onClick={() => setFaq(true)}>
<HelpIcon /> <Typography> FAQ </Typography>
</StyledButton>
)}
<FAQButton />
</Box>
</div>

{faq && <FAQ />}

{/* Don't lose state when FAQ is opened */}
<Box sx={{ display: faq ? 'none' : 'block', height: '100%' }}>
<ViewPicker />
</Box>
<FAQContainer />
</Grid>
</Grid>
</main>
);
});

const StyledButton = styled.button`
background: white;
border: 0;
cursor: pointer;
width: 70px;
&:hover {
border-radius: 5px;
outline: 3px solid #0150b4;
p {
font-weight: bold;
}
}
`;
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"date-fns": "^3.6.0",
"immer": "^10.1.1",
"next": "^14.2.4",
"nuqs": "^1.17.5",
"prisma-repl": "^1.4.0",
"react": "18.3.1",
"react-dom": "18.3.1",
Expand Down

0 comments on commit f336001

Please sign in to comment.