-
Notifications
You must be signed in to change notification settings - Fork 1
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 #102 from jadeallencook/feat/form-toggle
feat: added basic components for form toggling
- Loading branch information
Showing
10 changed files
with
266 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { child, get, getDatabase, ref } from 'firebase/database'; | ||
|
||
const dbRef = ref(getDatabase()); | ||
|
||
const getFormStatus = async (): Promise<boolean> => { | ||
try { | ||
return await get(child(dbRef, 'requestFormStatus')).then((snapshot) => | ||
snapshot.exists() ? snapshot.val() : false | ||
); | ||
} catch (err) { | ||
console.log(err); | ||
return null; | ||
} | ||
}; | ||
|
||
export default getFormStatus; |
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,8 @@ | ||
import { getDatabase, ref, set } from 'firebase/database'; | ||
|
||
const updateFormStatus = async (status: boolean) => { | ||
const db = getDatabase(); | ||
return await set(ref(db, 'requestFormStatus'), status); | ||
}; | ||
|
||
export default updateFormStatus; |
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,35 @@ | ||
import { createContext, useEffect, useState } from 'react'; | ||
import getFormStatus from '@api/get-form-status'; | ||
|
||
interface RequestFormStatusContext {} | ||
|
||
const RequestFormStatusContext = createContext<{ | ||
requestFormStatus: boolean; | ||
hasFetchedRequestFormStatus: boolean; | ||
}>(null); | ||
|
||
export const RequestFormStatusProvider = ({ children }) => { | ||
const [hasFetchedRequestFormStatus, setHasFetchedRequestFormStatus] = | ||
useState<boolean>(false); | ||
const [requestFormStatus, setRequestFormStatus] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
getFormStatus().then((response) => { | ||
setRequestFormStatus(response); | ||
setHasFetchedRequestFormStatus(true); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<RequestFormStatusContext.Provider | ||
value={{ | ||
requestFormStatus, | ||
hasFetchedRequestFormStatus, | ||
}} | ||
> | ||
{children} | ||
</RequestFormStatusContext.Provider> | ||
); | ||
}; | ||
|
||
export default RequestFormStatusContext; |
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,66 @@ | ||
import { Badge, InputGroup, Spinner } from 'react-bootstrap'; | ||
import updateFormStatus from '@api/update-form-status'; | ||
import getFormStatus from '@api/get-form-status'; | ||
import { useQuery } from 'react-query'; | ||
|
||
const FormStatusForm = () => { | ||
const { data: status, refetch } = useQuery({ | ||
queryKey: ['getFormStatus'], | ||
queryFn: () => getFormStatus(), | ||
}); | ||
|
||
const handleToggle = async () => { | ||
await updateFormStatus(!status); | ||
await refetch(); | ||
}; | ||
|
||
return ( | ||
<> | ||
{status !== undefined && ( | ||
<InputGroup.Checkbox | ||
checked={status} | ||
aria-label="" | ||
onChange={handleToggle} | ||
style={{ cursor: 'pointer' }} | ||
/> | ||
)} | ||
<InputGroup.Text> | ||
{status === undefined ? ( | ||
<> | ||
<Spinner | ||
as="span" | ||
animation="border" | ||
size="sm" | ||
role="status" | ||
aria-hidden="true" | ||
style={{ marginRight: '15px' }} | ||
/> | ||
Getting Request Form Status | ||
</> | ||
) : ( | ||
<> | ||
{' '} | ||
Request Form Status{' '} | ||
{status ? ( | ||
<Badge | ||
bg="danger" | ||
style={{ marginLeft: '5px', width: '50px' }} | ||
> | ||
Live | ||
</Badge> | ||
) : ( | ||
<Badge | ||
bg="dark" | ||
style={{ marginLeft: '5px', width: '50px' }} | ||
> | ||
Offline | ||
</Badge> | ||
)} | ||
</> | ||
)} | ||
</InputGroup.Text> | ||
</> | ||
); | ||
}; | ||
|
||
export default FormStatusForm; |
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
Oops, something went wrong.
4c3f8a1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
communalists – ./
www.communalists.org
communalists.org
communalists-git-main-jadeallencook.vercel.app
communalists-jadeallencook.vercel.app
communalists.vercel.app