-
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.
Add creating text proposal form. Add markdown viewer for proposal des…
…cription (#9) * add creating text proposal, add markdown viewer for proposal description * set stable rarimo client package, strict proposal desc length, add preview modal * add max length for proposal title
- Loading branch information
Showing
9 changed files
with
464 additions
and
103 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 |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import { | ||
Button, | ||
FormControl, | ||
FormHelperText, | ||
TextareaAutosize as BaseTextareaAutosize, | ||
TextField, | ||
Typography, | ||
} from '@mui/material' | ||
import { styled } from '@mui/system' | ||
import { useMemo, useState } from 'react' | ||
import { Controller } from 'react-hook-form' | ||
|
||
import { getClient } from '@/client' | ||
import { Dialog } from '@/components/Dialog' | ||
import FormWrapper from '@/components/Forms/FormWrapper' | ||
import MarkdownViewer from '@/components/MarkdownViewer' | ||
import { ErrorHandler } from '@/helpers' | ||
import { useForm, useWeb3 } from '@/hooks' | ||
import { useI18n } from '@/locales/client' | ||
import { FormProps } from '@/types' | ||
|
||
enum FieldNames { | ||
Title = 'title', | ||
Description = 'description', | ||
} | ||
|
||
const PROPOSAL_MAX_TITLE_LENGTH = 140 | ||
const PROPOSAL_MAX_DESC_LENGTH = 10_000 | ||
|
||
export default function SubmitTextProposalForm({ id, onSubmit, setIsDialogDisabled }: FormProps) { | ||
const t = useI18n() | ||
const { address } = useWeb3() | ||
|
||
const [isPreviewDialogOpen, setIsPreviewDialogOpen] = useState(false) | ||
|
||
const DEFAULT_VALUES = { | ||
[FieldNames.Title]: '', | ||
[FieldNames.Description]: '', | ||
} | ||
|
||
const { | ||
formState, | ||
handleSubmit, | ||
control, | ||
isFormDisabled, | ||
formErrors, | ||
disableForm, | ||
enableForm, | ||
getErrorMessage, | ||
} = useForm(DEFAULT_VALUES, yup => | ||
yup.object({ | ||
[FieldNames.Title]: yup.string().max(PROPOSAL_MAX_TITLE_LENGTH).required(), | ||
[FieldNames.Description]: yup.string().max(PROPOSAL_MAX_DESC_LENGTH).required(), | ||
}), | ||
) | ||
|
||
const client = useMemo(() => getClient(), []) | ||
|
||
const getMinAmount = async (): Promise<string> => { | ||
const { deposit_params } = await client.query.getGovParams('deposit') | ||
|
||
return ( | ||
deposit_params?.min_deposit?.find(i => i?.denom === client.config.currency.minDenom) | ||
?.amount ?? '0' | ||
) | ||
} | ||
|
||
const submit = async (formData: typeof DEFAULT_VALUES) => { | ||
disableForm() | ||
setIsDialogDisabled(true) | ||
try { | ||
const client = getClient() | ||
|
||
const amount = await getMinAmount() | ||
|
||
await client.tx.submitTextProposal( | ||
address, | ||
[ | ||
{ | ||
denom: client.config.currency.minDenom, | ||
amount, | ||
}, | ||
], | ||
formData[FieldNames.Title], | ||
formData[FieldNames.Description], | ||
) | ||
|
||
onSubmit({ | ||
message: t('submit-text-proposal-form.submitted-msg'), | ||
}) | ||
} catch (e) { | ||
ErrorHandler.process(e) | ||
} | ||
enableForm() | ||
setIsDialogDisabled(false) | ||
} | ||
|
||
return ( | ||
<FormWrapper id={id} onSubmit={handleSubmit(submit)} isFormDisabled={isFormDisabled}> | ||
<Typography variant={'body2'} color={'var(--col-txt-secondary)'}> | ||
{t('submit-text-proposal-form.helper-text')} | ||
</Typography> | ||
|
||
<Controller | ||
name={FieldNames.Title} | ||
control={control} | ||
render={({ field }) => ( | ||
<FormControl> | ||
<TextField | ||
{...field} | ||
label={t('submit-text-proposal-form.title-lbl')} | ||
error={!!formErrors[FieldNames.Title]} | ||
/> | ||
|
||
{Boolean(formErrors[FieldNames.Title]) && ( | ||
<FormHelperText error>{getErrorMessage(formErrors[FieldNames.Title])}</FormHelperText> | ||
)} | ||
</FormControl> | ||
)} | ||
/> | ||
|
||
<Controller | ||
name={FieldNames.Description} | ||
control={control} | ||
render={({ field }) => ( | ||
<FormControl> | ||
<TextareaAutosize | ||
{...field} | ||
minRows={5} | ||
maxRows={10} | ||
aria-label={`${FieldNames.Description}-textarea`} | ||
placeholder={t('submit-text-proposal-form.description-lbl')} | ||
/> | ||
|
||
{Boolean(formErrors[FieldNames.Description]) && ( | ||
<FormHelperText error> | ||
{getErrorMessage(formErrors[FieldNames.Description])} | ||
</FormHelperText> | ||
)} | ||
</FormControl> | ||
)} | ||
/> | ||
|
||
{formState[FieldNames.Description] && ( | ||
<Button onClick={() => setIsPreviewDialogOpen(true)}> | ||
{t('submit-text-proposal-form.desc-preview-btn')} | ||
</Button> | ||
)} | ||
|
||
<Dialog | ||
action={<></>} | ||
onClose={() => setIsPreviewDialogOpen(false)} | ||
isOpened={isPreviewDialogOpen} | ||
title={t('submit-text-proposal-form.desc-preview-title')} | ||
> | ||
<MarkdownViewer>{formState[FieldNames.Description]}</MarkdownViewer> | ||
</Dialog> | ||
</FormWrapper> | ||
) | ||
} | ||
|
||
const TextareaAutosize = styled(BaseTextareaAutosize)( | ||
({ theme }) => ` | ||
box-sizing: border-box; | ||
background: none; | ||
padding: ${theme.spacing(2)} ${theme.spacing(1.75)}; | ||
&:focus { | ||
outline: none; | ||
} | ||
`, | ||
) |
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,34 @@ | ||
import { Stack, Typography } from '@mui/material' | ||
import { getOverrides, MuiMarkdown } from 'mui-markdown' | ||
|
||
export default function MarkdownViewer({ children }: { children: string }) { | ||
return ( | ||
<MuiMarkdown | ||
overrides={{ | ||
...getOverrides({}), | ||
h2: { | ||
component: Typography, | ||
props: { variant: 'subtitle2', component: 'h2', sx: { mb: 2 } }, | ||
}, | ||
h3: { | ||
component: Typography, | ||
props: { variant: 'subtitle3', component: 'h3', sx: { mb: 2 } }, | ||
}, | ||
p: { | ||
component: Typography, | ||
props: { variant: 'body3', component: 'p', sx: { mb: 3 } }, | ||
}, | ||
ul: { | ||
component: Stack, | ||
props: { component: 'ul', sx: { pl: 5, mt: 0, mb: 3 } }, | ||
}, | ||
li: { | ||
component: Typography, | ||
props: { variant: 'subtitle4', component: 'li' }, | ||
}, | ||
}} | ||
> | ||
{children} | ||
</MuiMarkdown> | ||
) | ||
} |
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 { Stack } from '@mui/material' | ||
import { sleep } from '@rarimo/shared' | ||
import { useMemo, useState } from 'react' | ||
|
||
import { DialogFormWrapper } from '@/components/Dialog' | ||
import SubmitTextProposalForm from '@/components/Forms/SubmitTextProposalForm' | ||
import MultipleActionsButton from '@/components/MultipleActionsButton' | ||
import { Bus } from '@/helpers' | ||
import { useContentSectionAction } from '@/hooks' | ||
import { useI18n } from '@/locales/client' | ||
|
||
enum ProposalTypes { | ||
Text = 'proposal-submit-form', | ||
} | ||
|
||
export default function ProposalsSubmit() { | ||
const t = useI18n() | ||
|
||
const [submitType, setSubmitType] = useState<ProposalTypes>() | ||
|
||
const { closeDialog, openDialog, setIsDisabled, onSubmit, isDisabled, isDialogOpened } = | ||
useContentSectionAction(async () => { | ||
await sleep(2000) | ||
Bus.emit(Bus.eventList.reloadProposals) | ||
}) | ||
|
||
const actions = useMemo( | ||
() => { | ||
return [ | ||
{ | ||
label: t('proposals.submit-proposal-action'), | ||
handler: () => { | ||
setSubmitType(ProposalTypes.Text) | ||
openDialog() | ||
}, | ||
isDisabled: false, | ||
}, | ||
] | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[t], | ||
) | ||
|
||
return ( | ||
<Stack> | ||
<MultipleActionsButton actions={actions} isDisabled={isDisabled} /> | ||
|
||
<DialogFormWrapper | ||
formId={ProposalTypes.Text} | ||
isDisabled={isDisabled} | ||
isDialogOpened={isDialogOpened} | ||
closeDialog={closeDialog} | ||
actionBtnText={t('proposals-submit.dialog-action-btn')} | ||
title={t('proposals-submit.dialog-heading')} | ||
> | ||
{submitType === ProposalTypes.Text && ( | ||
<SubmitTextProposalForm | ||
id={ProposalTypes.Text} | ||
onSubmit={onSubmit} | ||
setIsDialogDisabled={setIsDisabled} | ||
/> | ||
)} | ||
</DialogFormWrapper> | ||
</Stack> | ||
) | ||
} |
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.