-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
User feedback mechanism with login (#790)
- Loading branch information
Showing
38 changed files
with
1,957 additions
and
304 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 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 |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
# Refer: https://nextjs.org/docs/basic-features/environment-variables | ||
|
||
NEXT_PUBLIC_OONI_API=https://api.ooni.io | ||
NEXT_PUBLIC_USER_FEEDBACK_API=https://api.ooni.io | ||
NEXT_PUBLIC_SENTRY_DSN=https://[email protected]/1427510 | ||
NEXT_PUBLIC_EXPLORER_URL=https://explorer.ooni.org |
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
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,107 @@ | ||
import React, { useState, useCallback, useEffect } from 'react' | ||
import { useForm, Controller } from 'react-hook-form' | ||
import { Flex, Box, Input, Button, Modal, Text, Heading, Container } from 'ooni-components' | ||
import styled from 'styled-components' | ||
import NLink from 'next/link' | ||
|
||
import { registerUser } from '/lib/api' | ||
import { useRouter } from 'next/router' | ||
import { FormattedMessage } from 'react-intl' | ||
import SpinLoader from 'components/vendor/SpinLoader' | ||
|
||
const StyledError = styled.small` | ||
color: ${props => props.theme.colors.red5}; | ||
` | ||
|
||
const StyledInputContainer = styled(Box).attrs({ | ||
width: '100%', | ||
mt: 3, | ||
})` | ||
position: relative; | ||
& ${StyledError} { | ||
position: absolute; | ||
top: -10px; | ||
right: 0px; | ||
} | ||
` | ||
|
||
export const LoginForm = ({ onLogin, redirectTo }) => { | ||
const router = useRouter() | ||
const [submitting, setSubmitting] = useState(false) | ||
const [loginError, setError] = useState(null) | ||
|
||
const { handleSubmit, control, formState, reset } = useForm({ | ||
mode: 'onTouched', | ||
defaultValues: { email_address: '' } | ||
}) | ||
|
||
const { errors, isValid, isDirty } = formState | ||
|
||
const onSubmit = useCallback((data) => { | ||
const { email_address } = data | ||
const registerApi = async (email_address) => { | ||
try { | ||
await registerUser(email_address, redirectTo) | ||
if (typeof onLogin === 'function') { | ||
onLogin() | ||
} | ||
} catch (e) { | ||
setError(e.message) | ||
// Reset form to mark `isDirty` as false | ||
reset({}, { keepValues: true }) | ||
} finally { | ||
setSubmitting(false) | ||
} | ||
} | ||
setSubmitting(true) | ||
registerApi(email_address) | ||
}, [onLogin, reset, redirectTo]) | ||
|
||
useEffect(() => { | ||
// Remove previous errors when form becomes dirty again | ||
if (isDirty) { | ||
setError(null) | ||
} | ||
}, [isDirty]) | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<Flex | ||
flexDirection={['column']} | ||
> | ||
<StyledInputContainer> | ||
<Controller | ||
render={({field}) => ( | ||
<Input | ||
placeholder='Email *' | ||
{...field} | ||
/> | ||
)} | ||
rules={{ | ||
pattern: { | ||
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, | ||
}, | ||
required: true, | ||
}} | ||
name='email_address' | ||
control={control} | ||
/> | ||
<StyledError>{errors?.email_address?.message}</StyledError> | ||
</StyledInputContainer> | ||
{loginError && | ||
<Box mt={1}> | ||
<StyledError>{loginError}</StyledError> | ||
</Box> | ||
} | ||
<Box mt={3} alignSelf='center'> | ||
{!submitting ? | ||
<Button disabled={!isValid} type='submit'><FormattedMessage id="General.Login" /></Button> : | ||
<SpinLoader size={3} margin='1px' /> | ||
} | ||
</Box> | ||
</Flex> | ||
</form> | ||
) | ||
} | ||
|
||
export default LoginForm |
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.
2e4d5d7
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:
explorer – ./
explorer-one.vercel.app
explorer-ooni1.vercel.app
explorer-git-master-ooni1.vercel.app