-
Notifications
You must be signed in to change notification settings - Fork 0
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 #155 from IFRCGo/feature/login-page
Add login page
- Loading branch information
Showing
11 changed files
with
354 additions
and
8 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { defineConfig, Schema } from '@julr/vite-plugin-validate-env'; | ||
|
||
// TODO: Integrate .env for CI and remove optional() call on required fields | ||
export default defineConfig({ | ||
APP_TITLE: Schema.string.optional(), | ||
APP_MAPBOX_ACCESS_TOKEN: Schema.string(), | ||
APP_GRAPHQL_ENDPOINT: Schema.string.optional(), | ||
APP_GOOGLE_ANALYTICS_ID: Schema.string.optional(), | ||
}) | ||
APP_HCAPTCHA_SITEKEY: Schema.string.optional(), | ||
}); |
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,85 @@ | ||
import React, { useCallback } from 'react'; | ||
import HCaptcha from '@hcaptcha/react-hcaptcha'; | ||
import { | ||
InputContainer, | ||
InputContainerProps, | ||
} from '@ifrc-go/ui'; | ||
|
||
import { hCaptchaKey } from '#config'; | ||
|
||
export type HCaptchaProps<T> = Omit<InputContainerProps, 'input'> & { | ||
name: T, | ||
onChange: (value: string | undefined, name: T) => void; | ||
elementRef?: React.RefObject<HCaptcha>; | ||
}; | ||
|
||
function HCaptchaInput<T extends string>(props: HCaptchaProps<T>) { | ||
const { | ||
actions, | ||
actionsContainerClassName, | ||
className, | ||
disabled, | ||
error, | ||
errorContainerClassName, | ||
hint, | ||
hintContainerClassName, | ||
icons, | ||
iconsContainerClassName, | ||
inputSectionClassName, | ||
label, | ||
readOnly, | ||
name, | ||
onChange, | ||
elementRef, | ||
} = props; | ||
|
||
const handleVerify = useCallback( | ||
(token: string) => { | ||
onChange(token, name); | ||
}, | ||
[onChange, name], | ||
); | ||
const handleError = useCallback( | ||
(err: string) => { | ||
// eslint-disable-next-line no-console | ||
console.error(err); | ||
onChange(undefined, name); | ||
}, | ||
[onChange, name], | ||
); | ||
const handleExpire = useCallback( | ||
() => { | ||
onChange(undefined, name); | ||
}, | ||
[onChange, name], | ||
); | ||
|
||
return ( | ||
<InputContainer | ||
actions={actions} | ||
actionsContainerClassName={actionsContainerClassName} | ||
className={className} | ||
disabled={disabled} | ||
error={error} | ||
errorContainerClassName={errorContainerClassName} | ||
hint={hint} | ||
hintContainerClassName={hintContainerClassName} | ||
icons={icons} | ||
iconsContainerClassName={iconsContainerClassName} | ||
inputSectionClassName={inputSectionClassName} | ||
label={label} | ||
readOnly={readOnly} | ||
input={hCaptchaKey && ( | ||
<HCaptcha | ||
ref={elementRef} | ||
sitekey={hCaptchaKey} | ||
onVerify={handleVerify} | ||
onError={handleError} | ||
onExpire={handleExpire} | ||
/> | ||
)} | ||
/> | ||
); | ||
} | ||
|
||
export default HCaptchaInput; |
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,22 @@ | ||
{ | ||
"namespace": "login", | ||
"strings": { | ||
"loginTitle":"IFRC GO - Login", | ||
"loginHeader":"Login", | ||
"loginSubHeader":"If you are staff, member or volunteer of the Red Cross Red Crescent Movement (National Societies, the IFRC and the ICRC) login with you email and password.", | ||
"loginEmailUsername":"Email", | ||
"loginPassword":"Password", | ||
"loginRecoverTitle":"Recover password", | ||
"loginShowUsernameTitle":"Show me my username", | ||
"loginResendValidation":"Re-send validation email", | ||
"loginResendValidationTitle":"I didn't get my validation email", | ||
"loginForgotUserPass":"Forgot your password?", | ||
"loginInvalid":"Invalid username or password", | ||
"loginErrorMessage":"Error: {message}", | ||
"loginButton":"Login", | ||
"loginDontHaveAccount":"Don’t have an account? {signUpLink}", | ||
"loginCreateAccountTitle":"Create new account", | ||
"loginSignUp":"Sign up", | ||
"loginFailureMessage": "Sorry, failed to login!" | ||
} | ||
} |
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,155 @@ | ||
import { useMemo } from 'react'; | ||
import { | ||
Button, | ||
PasswordInput, | ||
TextInput, | ||
} from '@ifrc-go/ui'; | ||
import { useTranslation } from '@ifrc-go/ui/hooks'; | ||
import { resolveToComponent } from '@ifrc-go/ui/utils'; | ||
import { | ||
createSubmitHandler, | ||
type ObjectSchema, | ||
requiredStringCondition, | ||
useForm, | ||
} from '@togglecorp/toggle-form'; | ||
|
||
import HCaptcha from '#components/Captcha'; | ||
import Link from '#components/Link'; | ||
import Page from '#components/Page'; | ||
|
||
import i18n from './i18n.json'; | ||
import styles from './styles.module.css'; | ||
|
||
interface FormFields { | ||
email: string; | ||
password: string; | ||
captcha: string; | ||
} | ||
type PartialFormFields = Partial<FormFields>; | ||
type FormSchema = ObjectSchema<PartialFormFields>; | ||
type FormSchemaFields = ReturnType<FormSchema['fields']>; | ||
|
||
const defaultFormValue: PartialFormFields = {}; | ||
|
||
const formSchema: FormSchema = { | ||
fields: (): FormSchemaFields => ({ | ||
email: { | ||
required: true, | ||
requiredValidation: requiredStringCondition, | ||
}, | ||
password: { | ||
required: true, | ||
requiredValidation: requiredStringCondition, | ||
}, | ||
captcha: { | ||
required: true, | ||
requiredValidation: requiredStringCondition, | ||
}, | ||
}), | ||
}; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export function Component() { | ||
const strings = useTranslation(i18n); | ||
const { | ||
value: formValue, | ||
setFieldValue, | ||
setError, | ||
validate, | ||
} = useForm(formSchema, { value: defaultFormValue }); | ||
|
||
const fieldError: PartialFormFields = {}; | ||
|
||
const handleFormSubmit = useMemo( | ||
() => createSubmitHandler( | ||
validate, | ||
setError, | ||
// FIXME: Add form submission logic here | ||
() => {}, | ||
), | ||
[validate, setError], | ||
); | ||
|
||
const signupInfo = resolveToComponent( | ||
strings.loginDontHaveAccount, | ||
{ | ||
signUpLink: ( | ||
<Link | ||
to="login" // FIXME :add Register | ||
withUnderline | ||
> | ||
{strings.loginSignUp} | ||
</Link> | ||
), | ||
}, | ||
); | ||
|
||
return ( | ||
<Page | ||
className={styles.login} | ||
title={strings.loginTitle} | ||
heading={strings.loginHeader} | ||
description={strings.loginSubHeader} | ||
mainSectionClassName={styles.mainSection} | ||
> | ||
<form | ||
className={styles.form} | ||
onSubmit={handleFormSubmit} | ||
> | ||
<div className={styles.fields}> | ||
<TextInput | ||
name="email" | ||
label={strings.loginEmailUsername} | ||
value={formValue.email} | ||
error={fieldError?.email} | ||
onChange={setFieldValue} | ||
withAsterisk | ||
autoFocus | ||
/> | ||
<PasswordInput | ||
name="password" | ||
label={strings.loginPassword} | ||
value={formValue.password} | ||
error={fieldError?.password} | ||
onChange={setFieldValue} | ||
withAsterisk | ||
/> | ||
</div> | ||
<div className={styles.utilityLinks}> | ||
<Link | ||
to="login" // FIXME: ForgotPassword | ||
title={strings.loginRecoverTitle} | ||
withUnderline | ||
> | ||
{strings.loginForgotUserPass} | ||
</Link> | ||
<Link | ||
to="login" // FIXME :LoginResendValidation | ||
title={strings.loginResendValidationTitle} | ||
withUnderline | ||
> | ||
{strings.loginResendValidation} | ||
</Link> | ||
</div> | ||
<div className={styles.actions}> | ||
<HCaptcha | ||
name="captcha" | ||
onChange={setFieldValue} | ||
/> | ||
<Button | ||
name={undefined} | ||
type="submit" | ||
onClick={handleFormSubmit} | ||
> | ||
{strings.loginButton} | ||
</Button> | ||
<div className={styles.signUp}> | ||
{signupInfo} | ||
</div> | ||
</div> | ||
</form> | ||
</Page> | ||
); | ||
} | ||
|
||
Component.displayName = 'Login'; |
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,39 @@ | ||
.login { | ||
.main-section { | ||
display: flex; | ||
justify-content: center; | ||
|
||
.form { | ||
display: flex; | ||
flex-direction: column; | ||
flex-grow: 1; | ||
gap: var(--go-ui-spacing-xl); | ||
max-width: var(--go-ui-width-content-max); | ||
|
||
.fields { | ||
display: flex; | ||
flex-direction: column; | ||
gap: var(--go-ui-spacing-xl); | ||
} | ||
|
||
.utility-links { | ||
display: flex; | ||
flex-direction: column; | ||
gap: var(--go-ui-spacing-sm); | ||
align-items: flex-end; | ||
} | ||
|
||
.actions { | ||
display: flex; | ||
flex-direction: column; | ||
gap: var(--go-ui-spacing-lg); | ||
align-items: center; | ||
|
||
.sign-up { | ||
display: flex; | ||
gap: var(--go-ui-spacing-sm); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.