[ABANDONED] React/Redux front end for the Flexor social network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
5.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import React, { FC, useEffect } from 'react'
  2. import { useDispatch, useSelector } from 'react-redux'
  3. import { Link, useHistory } from 'react-router-dom'
  4. import { faCheckCircle, faIdCard, faLink, faAddressBook, faCodeBranch } from '@fortawesome/free-solid-svg-icons'
  5. import { checkAppAvailability, createApp } from '../../actions/apps'
  6. import { initForm, initField, setFieldNotification } from '../../actions/forms'
  7. import { showNotification } from '../../actions/notifications'
  8. import { getForm } from '../../selectors/forms'
  9. import { getIsFetching } from '../../selectors/requests'
  10. import { useTheme } from '../../hooks'
  11. import { setTitle, valueFromForm } from '../../utils'
  12. import { AppState, NotificationType, AppThunkDispatch, RequestKey } from '../../types'
  13. import Title from '../../components/title'
  14. import Section from '../../components/section'
  15. import HorizontalRule from '../../components/horizontal-rule'
  16. import PrimaryButton from '../../components/controls/primary-button'
  17. import TextField from '../../components/controls/text-field'
  18. import TextareaField from '../../components/controls/textarea-field'
  19. import CheckboxField from '../../components/controls/checkbox-field'
  20. import ImageField from '../../components/controls/image-field'
  21. import CoverImageField from '../../components/controls/cover-image-field'
  22. import IconImageField from '../../components/controls/icon-image-field'
  23. const CreateApp: FC = () => {
  24. const theme = useTheme()
  25. const form = useSelector(getForm)
  26. const fetching = useSelector<AppState, boolean>(state => getIsFetching(state, RequestKey.CreateApp))
  27. const dispatch = useDispatch<AppThunkDispatch>()
  28. const history = useHistory()
  29. const checkAvailability = (value: string) => {
  30. if (value.length > 3) {
  31. dispatch(checkAppAvailability(value))
  32. }
  33. }
  34. const handleCreate = async () => {
  35. let invalid = false
  36. const name = valueFromForm<string>(form, 'name')
  37. const about = valueFromForm<string>(form, 'about')
  38. const websiteUrl = valueFromForm<string>(form, 'websiteUrl')
  39. const companyName = valueFromForm<string>(form, 'companyName')
  40. const version = valueFromForm<string>(form, 'version')
  41. const composerUrl = valueFromForm<string>(form, 'composerUrl')
  42. const rendererUrl = valueFromForm<string>(form, 'rendererUrl')
  43. const imageUrl = valueFromForm<string>(form, 'image')
  44. const coverImageUrl = valueFromForm<string>(form, 'coverImage')
  45. const iconImageUrl = valueFromForm<string>(form, 'iconImage')
  46. const agree = valueFromForm<boolean>(form, 'agree')
  47. if (!name || name === '') {
  48. dispatch(setFieldNotification('name', NotificationType.Error, 'This is required'))
  49. invalid = true
  50. }
  51. if (!version || version === '') {
  52. dispatch(setFieldNotification('version', NotificationType.Error, 'This is required'))
  53. invalid = true
  54. }
  55. if (!agree) {
  56. dispatch(setFieldNotification('agree', NotificationType.Error, 'You must agree to the terms and conditions to continue'))
  57. dispatch(showNotification(NotificationType.Error, 'You must agree to the terms and conditions to continue.'))
  58. invalid = true
  59. }
  60. if (invalid) return
  61. const id = await dispatch(createApp({
  62. name: name!,
  63. version: version!,
  64. about,
  65. websiteUrl,
  66. companyName,
  67. composerUrl,
  68. rendererUrl,
  69. imageUrl,
  70. coverImageUrl,
  71. iconImageUrl,
  72. }))
  73. dispatch(showNotification(NotificationType.Success, 'App created successfully!'))
  74. setTimeout(() => {
  75. history.push(`/a/${id}`)
  76. }, 1000)
  77. }
  78. useEffect(() => {
  79. setTitle('Create a new App')
  80. dispatch(initForm())
  81. dispatch(initField('name', ''))
  82. dispatch(initField('about', ''))
  83. dispatch(initField('websiteUrl', ''))
  84. dispatch(initField('companyName', ''))
  85. dispatch(initField('version', ''))
  86. dispatch(initField('composerUrl', ''))
  87. dispatch(initField('rendererUrl', ''))
  88. dispatch(initField('agree', false))
  89. }, [])
  90. return (
  91. <div>
  92. <Section>
  93. <Title>Create a new App</Title>
  94. <HorizontalRule />
  95. <TextField name="name" label="Name" icon={faIdCard} placeholder="App ID/Name" onBlur={e => checkAvailability(e.target.value)} />
  96. <TextareaField name="about" label="About" placeholder="Description of this app" />
  97. <TextField name="websiteUrl" label="Website" icon={faLink} placeholder="Website URL (optional)" />
  98. <TextField name="companyName" label="Company" icon={faAddressBook} placeholder="Your company or organization (optional)" />
  99. <TextField name="version" label="Version" icon={faCodeBranch} placeholder="Current Version of the app (ex: 0.0.1beta5)" />
  100. <HorizontalRule />
  101. <ImageField name="image" />
  102. <CoverImageField name="coverImage" />
  103. <IconImageField name="iconImage" />
  104. <HorizontalRule />
  105. <TextField name="composerUrl" label="Composer URL" icon={faLink} placeholder="URL for the composer web page" />
  106. <TextField name="rendererUrl" label="Renderer URL" icon={faLink} placeholder="URL for the renderer template" />
  107. <CheckboxField name="agree">
  108. I agree to the Apps <Link style={{ color: theme.primary }} to="/terms/apps">terms and conditions</Link>.
  109. </CheckboxField>
  110. <br /><br />
  111. <PrimaryButton text="Create" icon={faCheckCircle} loading={fetching} onClick={() => handleCreate()} />
  112. </Section>
  113. </div>
  114. )
  115. }
  116. export default CreateApp