Skip to content

Commit

Permalink
♻️ Rewrite Ticker View to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
batabana committed Jun 24, 2022
1 parent 62a78e8 commit 07e06d2
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 313 deletions.
269 changes: 0 additions & 269 deletions src/components/MessageForm.js

This file was deleted.

97 changes: 97 additions & 0 deletions src/components/MessageForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React, {
ChangeEvent,
FC,
FormEvent,
useCallback,
useEffect,
useState,
} from 'react'
import { postMessage } from '../api/Message'
import {
Button,
Form,
Message as Error,
TextAreaProps,
} from 'semantic-ui-react'
import { Ticker } from '../api/Ticker'
import { SubmitHandler, useForm } from 'react-hook-form'
import { useQueryClient } from 'react-query'
import MessageFormCounter from './MessageFormCounter'

interface Props {
ticker: Ticker
}

interface FormValues {
message: string
}

export const MESSAGE_LIMIT = 280

const MessageForm: FC<Props> = ({ ticker }) => {
const { handleSubmit, register, setValue, watch } = useForm<FormValues>()
const queryClient = useQueryClient()
const watchMessage = watch('message', '')

const [errorMessage, setErrorMessage] = useState<string>('')

const onChange = useCallback(
(e: ChangeEvent | FormEvent, { name, value }: TextAreaProps) => {
setValue(name, value)
if (watchMessage?.length > MESSAGE_LIMIT) {
setErrorMessage(
`The message is too long. You must remove ${
watchMessage?.length - MESSAGE_LIMIT
} characters.`
)
} else if (errorMessage !== '') {
setErrorMessage('')
}
},
[errorMessage, setValue, watchMessage?.length]
)

const onSubmit: SubmitHandler<FormValues> = data => {
postMessage(ticker.id.toString(), data.message, null, []).finally(() => {
queryClient.invalidateQueries('messages')
})
}

useEffect(() => {
register('message')
})

// TODO: attachments + map

return (
<Form id="sendMessage" onSubmit={handleSubmit(onSubmit)}>
<Form.Field>
<Form.TextArea
defaultValue=""
name="message"
onChange={onChange}
placeholder="Write a message"
rows="5"
/>
</Form.Field>
<Error
content={errorMessage}
header="Error"
hidden={!errorMessage}
icon="ban"
negative
/>
<Button
color="teal"
content="Send"
disabled={errorMessage !== '' || watchMessage === ''}
form="sendMessage"
icon="send"
type="submit"
/>
<MessageFormCounter letterCount={watchMessage?.length || 0} />
</Form>
)
}

export default MessageForm
Loading

0 comments on commit 07e06d2

Please sign in to comment.