From 84ea622bc996b85f1916263b2c8897d6c3102419 Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Tue, 6 Jul 2021 16:35:10 +0200 Subject: [PATCH] Work on shared pages --- ui/src/Pages/SharePage/MediaSharePage.tsx | 6 +- .../SharePage/PasswordProtectedShare.tsx | 88 +++++++++++++++++++ ui/src/Pages/SharePage/SharePage.tsx | 70 +-------------- ui/src/primitives/form/Input.tsx | 15 +++- 4 files changed, 108 insertions(+), 71 deletions(-) create mode 100644 ui/src/Pages/SharePage/PasswordProtectedShare.tsx diff --git a/ui/src/Pages/SharePage/MediaSharePage.tsx b/ui/src/Pages/SharePage/MediaSharePage.tsx index 3873e0d8..5c60f7a6 100644 --- a/ui/src/Pages/SharePage/MediaSharePage.tsx +++ b/ui/src/Pages/SharePage/MediaSharePage.tsx @@ -13,13 +13,13 @@ import { MediaType } from '../../../__generated__/globalTypes' import { exhaustiveCheck } from '../../helpers/utils' const DisplayPhoto = styled(ProtectedImage)` - width: 100%; + /* width: 100%; */ max-height: calc(80vh); object-fit: contain; ` const DisplayVideo = styled(ProtectedVideo)` - width: 100%; + /* width: 100%; */ max-height: calc(80vh); ` @@ -54,7 +54,7 @@ const MediaSharePage = ({ media }: MediaSharePageType) => { return (
-

{media.title}

+

{media.title}

diff --git a/ui/src/Pages/SharePage/PasswordProtectedShare.tsx b/ui/src/Pages/SharePage/PasswordProtectedShare.tsx new file mode 100644 index 00000000..bf6847b4 --- /dev/null +++ b/ui/src/Pages/SharePage/PasswordProtectedShare.tsx @@ -0,0 +1,88 @@ +import React, { useState } from 'react' +import { useForm } from 'react-hook-form' +import { useTranslation } from 'react-i18next' +import { TextField } from '../../primitives/form/Input' +import { MessageContainer } from './SharePage' +// import { Message, Header, Form, Input, Icon } from 'semantic-ui-react' + +type ProtectedTokenEnterPasswordProps = { + refetchWithPassword(password: string): void + loading: boolean +} + +const PasswordProtectedShare = ({ + refetchWithPassword, + loading = false, +}: ProtectedTokenEnterPasswordProps) => { + const { t } = useTranslation() + + const { + register, + watch, + formState: { errors }, + handleSubmit, + } = useForm() + + // const [passwordValue, setPasswordValue] = useState('') + const [invalidPassword, setInvalidPassword] = useState(false) + + const onSubmit = () => { + refetchWithPassword(watch('password')) + setInvalidPassword(true) + } + + let errorMessage = undefined + if (invalidPassword && !loading) { + errorMessage = t( + 'share_page.wrong_password', + 'Wrong password, please try again.' + ) + } else if (errors.password?.type === 'required') { + errorMessage = t( + 'share_page.protected_share.password_required_error', + 'Password is required' + ) + } + + return ( + +

+ {t('share_page.protected_share.title', 'Protected share')} +

+

+ {t( + 'share_page.protected_share.description', + 'This share is protected with a password.' + )} +

+ + {/* + + + event.key == 'Enter' && onSubmit() + } + onChange={e => setPasswordValue(e.target.value)} + placeholder={t('login_page.field.password', 'Password')} + type="password" + icon={} + /> + + {errorMessage} */} +
+ ) +} + +export default PasswordProtectedShare diff --git a/ui/src/Pages/SharePage/SharePage.tsx b/ui/src/Pages/SharePage/SharePage.tsx index f2c752d5..e07d4c14 100644 --- a/ui/src/Pages/SharePage/SharePage.tsx +++ b/ui/src/Pages/SharePage/SharePage.tsx @@ -1,8 +1,7 @@ import PropTypes from 'prop-types' -import React, { useState } from 'react' +import React from 'react' import { useQuery, gql } from '@apollo/client' import { match as MatchType, Route, Switch } from 'react-router-dom' -import { Form, Header, Icon, Input, Message } from 'semantic-ui-react' import styled from 'styled-components' import { getSharePassword, @@ -11,6 +10,7 @@ import { import AlbumSharePage from './AlbumSharePage' import MediaSharePage from './MediaSharePage' import { useTranslation } from 'react-i18next' +import PasswordProtectedShare from './PasswordProtectedShare' export const SHARE_TOKEN_QUERY = gql` query SharePageToken($token: String!, $password: String) { @@ -133,73 +133,11 @@ AuthorizedTokenRoute.propTypes = { match: PropTypes.object.isRequired, } -const MessageContainer = styled.div` +export const MessageContainer = styled.div` max-width: 400px; margin: 100px auto 0; ` -type ProtectedTokenEnterPasswordProps = { - refetchWithPassword(password: string): void - loading: boolean -} - -const ProtectedTokenEnterPassword = ({ - refetchWithPassword, - loading = false, -}: ProtectedTokenEnterPasswordProps) => { - const { t } = useTranslation() - - const [passwordValue, setPasswordValue] = useState('') - const [invalidPassword, setInvalidPassword] = useState(false) - - const onSubmit = () => { - refetchWithPassword(passwordValue) - setInvalidPassword(true) - } - - let errorMessage = null - if (invalidPassword && !loading) { - errorMessage = ( - - - {t('share_page.wrong_password', 'Wrong password, please try again.')} - - - ) - } - - return ( - -
- {t('share_page.protected_share.title', 'Protected share')} -
-

- {t( - 'share_page.protected_share.description', - 'This share is protected with a password.' - )} -

-
- - - - event.key == 'Enter' && onSubmit() - } - onChange={e => setPasswordValue(e.target.value)} - placeholder={t('login_page.field.password', 'Password')} - type="password" - icon={} - /> - - {errorMessage} -
-
- ) -} - interface TokenRouteMatch { token: string } @@ -248,7 +186,7 @@ const TokenRoute = ({ match }: MatchProps) => { if (data && data.shareTokenValidatePassword == false) { return ( - { saveSharePassword(token, password) refetch({ token, password }) diff --git a/ui/src/primitives/form/Input.tsx b/ui/src/primitives/form/Input.tsx index 72b7f39a..e18aa0da 100644 --- a/ui/src/primitives/form/Input.tsx +++ b/ui/src/primitives/form/Input.tsx @@ -11,6 +11,7 @@ type TextFieldProps = { className?: ClassNamesArg wrapperClassName?: ClassNamesArg sizeVariant?: 'default' | 'big' + fullWidth?: boolean action?: () => void loading?: boolean } & Omit, 'className'> @@ -23,6 +24,7 @@ export const TextField = forwardRef( className, wrapperClassName, sizeVariant, + fullWidth, action, loading, ...inputProps @@ -58,6 +60,7 @@ export const TextField = forwardRef( 'block border rounded-md focus:ring-2 focus:outline-none px-2', variant, sizeVariant == 'big' ? 'py-2' : 'py-1', + { 'w-full': fullWidth }, className )} {...inputProps} @@ -77,7 +80,11 @@ export const TextField = forwardRef( ) } else if (action) { input = ( -
+
{input}
)