-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from AlexStack/nextjs14
feat: add hook useClientContext
- Loading branch information
Showing
9 changed files
with
2,274 additions
and
1,838 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
This file was deleted.
Oops, something went wrong.
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,96 @@ | ||
/* eslint-disable @next/next/no-img-element */ | ||
|
||
'use client'; | ||
import { Send } from '@mui/icons-material'; | ||
import Avatar from '@mui/material/Avatar'; | ||
import Button from '@mui/material/Button'; | ||
import Stack from '@mui/material/Stack'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
import { useAlertBar } from '@/hooks/useAlertBar'; | ||
import { useClientContext } from '@/hooks/useClientContext'; | ||
|
||
import SubmitButton from '@/components/shared/SubmitButton'; | ||
|
||
const DisplayRandomPicture = () => { | ||
const [imageUrl, setImageUrl] = useState(''); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState(''); | ||
const { fetchCount, updateClientCtx } = useClientContext(); | ||
const { setAlertBarProps, renderAlertBar } = useAlertBar(); | ||
const renderCountRef = React.useRef(0); | ||
|
||
const fetchRandomPicture = async () => { | ||
setLoading(true); | ||
setError(''); | ||
|
||
try { | ||
const response = await fetch('https://picsum.photos/300/150'); | ||
if (!response.ok) { | ||
throw new Error('Error fetching the image'); | ||
} | ||
setImageUrl(response.url); | ||
updateClientCtx({ fetchCount: fetchCount + 1 }); | ||
setAlertBarProps({ | ||
message: 'A random picture fetched successfully', | ||
severity: 'info', | ||
}); | ||
} catch (error) { | ||
setError('Error fetching the image'); | ||
setAlertBarProps({ | ||
message: 'Error fetching the image', | ||
severity: 'error', | ||
}); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (renderCountRef.current === 0) { | ||
fetchRandomPicture(); | ||
} | ||
renderCountRef.current += 1; | ||
}); | ||
|
||
return ( | ||
<Stack | ||
direction='column' | ||
justifyContent='center' | ||
alignItems='center' | ||
spacing={2} | ||
> | ||
{error && <p>{error}</p>} | ||
{imageUrl && ( | ||
<Avatar | ||
alt='DisplayRandomPicture' | ||
src={imageUrl} | ||
variant='square' | ||
sx={{ width: 300, height: 150, borderRadius: '10px' }} | ||
/> | ||
)} | ||
<div> | ||
{loading && <span>Loading...</span>} Component Render Count:{' '} | ||
{renderCountRef.current} | ||
</div> | ||
|
||
<SubmitButton | ||
isSubmitting={loading} | ||
submittingText='Fetching Picture ...' | ||
> | ||
<Button | ||
variant='contained' | ||
endIcon={<Send />} | ||
onClick={fetchRandomPicture} | ||
disabled={loading} | ||
color='secondary' | ||
> | ||
Get Another Picture | ||
</Button> | ||
</SubmitButton> | ||
{renderAlertBar()} | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default DisplayRandomPicture; |
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,24 @@ | ||
'use client'; | ||
|
||
import React, { useState } from 'react'; | ||
|
||
import { AlertBar, AlertBarProps } from '@/components/shared/AlertBar'; | ||
|
||
export const useAlertBar = () => { | ||
const [alertBarProps, setAlertBarProps] = useState<AlertBarProps>({ | ||
message: '', | ||
severity: 'info', | ||
}); | ||
|
||
const renderAlertBar = () => ( | ||
<AlertBar | ||
onClose={() => setAlertBarProps({ message: '' })} | ||
{...alertBarProps} | ||
/> | ||
); | ||
|
||
return { | ||
setAlertBarProps, | ||
renderAlertBar, | ||
}; | ||
}; |
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,56 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import React, { act } from 'react'; | ||
|
||
import { ClientProvider, useClientContext } from './useClientContext'; | ||
|
||
describe('useClientContext', () => { | ||
it('should not be used outside ClientProvider', () => { | ||
const { result } = renderHook(() => useClientContext()); | ||
expect(() => { | ||
result.current.updateClientCtx({ fetchCount: 66 }); | ||
}).toThrow('Cannot be used outside ClientProvider'); | ||
}); | ||
|
||
it('should provide the correct initial context values', () => { | ||
const ctxValue = { | ||
topError: 'SWW Error', | ||
bmStatus: 'Live', | ||
fetchCount: 85, | ||
}; | ||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<ClientProvider value={ctxValue}>{children}</ClientProvider> | ||
); | ||
|
||
const { result } = renderHook(() => useClientContext(), { | ||
wrapper, | ||
}); | ||
|
||
expect(result.current.topError).toBe(ctxValue.topError); | ||
expect(result.current.fetchCount).toBe(ctxValue.fetchCount); | ||
}); | ||
|
||
it('should update the context values', () => { | ||
const ctxValue = { | ||
topError: 'SWW Error', | ||
fetchCount: 85, | ||
}; | ||
const wrapper = ({ children }: { children: React.ReactNode }) => ( | ||
<ClientProvider value={ctxValue}>{children}</ClientProvider> | ||
); | ||
|
||
const { result } = renderHook(() => useClientContext(), { | ||
wrapper, | ||
}); | ||
|
||
const newCtxValue = { | ||
topError: '', | ||
}; | ||
|
||
act(() => { | ||
result.current.updateClientCtx(newCtxValue); | ||
}); | ||
|
||
expect(result.current.topError).toBe(newCtxValue.topError); | ||
expect(result.current.fetchCount).toBe(ctxValue.fetchCount); | ||
}); | ||
}); |
Oops, something went wrong.