Skip to content

Commit

Permalink
feat: allow rerendering of elements on add/remove after refresh by us…
Browse files Browse the repository at this point in the history
…ing an array
  • Loading branch information
gregoryduckworth committed Oct 20, 2023
1 parent 408b9e1 commit 33dbe1f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"description": "Dynamically add and remove elements from the page",
"information": "As well as being able to dynamically add and remove elements from the page, you can also add `?initialElements=X` onto the url to generate elements on load.",
"add-element": "Add Element",
"clear-storage": "Clear Storage",
"remove-element": "Remove Element"
},
"drag-and-drop": {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"description": "Ajouter et supprimer dynamiquement des éléments de la page",
"information": "En plus de pouvoir ajouter et supprimer dynamiquement des éléments de la page, vous pouvez également ajouter `?initialElements=X` à l'URL pour générer des éléments au chargement.",
"add-element": "Ajouter un élément",
"clear-storage": "Effacer le stockage",
"remove-element": "Supprimer un élément"
},
"drag-and-drop": {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locales/pt-br.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"description": "Adicione e remova dinamicamente elementos da página",
"information": "Além de poder adicionar e remover dinamicamente elementos da página, você também pode adicionar `?initialElements=X` à URL para gerar elementos no carregamento.",
"add-element": "Adicionar Elemento",
"clear-storage": "Limpar Armazenamento",
"remove-element": "Remover Elemento"
},
"drag-and-drop": {
Expand Down
34 changes: 27 additions & 7 deletions src/scenarios/AddRemove.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import PageSetup from '../components/PageSetup'

const AddRemove = (): JSX.Element => {
const { t } = useTranslation()
const [elements, setElements] = useState<number[]>([])
const [elements, setElements] = useState<number[]>(() => {
// Retrieve elements from sessionStorage
const storedElements = sessionStorage.getItem('elements')
return storedElements !== null ? JSON.parse(storedElements) : []
})
const [nextIndex, setNextIndex] = useState(() => {
const storedIndex = sessionStorage.getItem('nextIndex')
return storedIndex !== null ? parseInt(storedIndex, 10) : 0
Expand All @@ -28,6 +32,11 @@ const AddRemove = (): JSX.Element => {
}
}, [elements, initialElementsParam])

useEffect(() => {
// Save elements to sessionStorage whenever it changes
sessionStorage.setItem('elements', JSON.stringify(elements))
}, [elements])

const addElement = (): void => {
setNextIndex((prevIndex) => {
const newIndex = prevIndex + 1
Expand All @@ -48,6 +57,12 @@ const AddRemove = (): JSX.Element => {
})
}

const clearStorage = (): void => {
sessionStorage.clear()
setElements([]) // Clear elements in state as well
setNextIndex(0)
}

const ElementComponent = ({
index,
removeElement
Expand All @@ -60,8 +75,8 @@ const AddRemove = (): JSX.Element => {
return (
<>
<Button
variant="contained"
color="error"
variant='contained'
color='error'
onClick={removeElement}
data-testid={`remove-element-${index + 1}`}
>
Expand All @@ -78,10 +93,15 @@ const AddRemove = (): JSX.Element => {
description={t('scenarios.add-remove.description')}
information={t('scenarios.add-remove.information')}
/>
<Button variant="contained" onClick={addElement} sx={{ mb: 4 }} data-testid="add-element">
{t('scenarios.add-remove.add-element')}
</Button>
<Box display="grid" gridTemplateColumns="repeat(auto-fill, minmax(200px, 1fr))" gap={2}>
<Box display="flex" mb={4}>
<Button variant="contained" onClick={addElement} data-testid="add-element" sx={{ mr: 2 }}>
{t('scenarios.add-remove.add-element')}
</Button>
<Button variant="contained" onClick={clearStorage} data-testid="clear-storage" color="secondary">
{t('scenarios.add-remove.clear-storage')}
</Button>
</Box>
<Box display='grid' gridTemplateColumns='repeat(auto-fill, minmax(200px, 1fr))' gap={2}>
{elements.map((element, index) => (
<div key={index}>
<ElementComponent
Expand Down
21 changes: 21 additions & 0 deletions src/tests/AddRemove.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,25 @@ describe('AddRemove component', () => {
const removeSecondElementButton = screen.getByTestId('remove-element-2')
expect(removeSecondElementButton).toBeInTheDocument()
})

it('clears storage when the "Clear Storage" button is clicked', () => {
TestRenderer(<AddRemove />)

// Add an element first
const addButton = screen.getByTestId('add-element')
fireEvent.click(addButton)
const removeElementButton = screen.queryByTestId('remove-element-1')
expect(removeElementButton).toBeInTheDocument()

// Click the "Clear Storage" button
const clearStorageButton = screen.getByTestId('clear-storage')
fireEvent.click(clearStorageButton)

// Check if storage is cleared
expect(sessionStorage.getItem('elements')).toEqual('[]')
expect(sessionStorage.getItem('nextIndex')).toBeNull()

// Check if state is updated
expect(removeElementButton).not.toBeInTheDocument()
})
})

0 comments on commit 33dbe1f

Please sign in to comment.