[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.

154 lines
6.5 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. // create-app.tsx
  2. // Copyright (C) 2020 Dwayne Harris
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. import React, { FC, useEffect } from 'react'
  14. import { useDispatch, useSelector } from 'react-redux'
  15. import { Link, useHistory } from 'react-router-dom'
  16. import { faCheckCircle, faIdCard, faLink, faAddressBook, faCodeBranch } from '@fortawesome/free-solid-svg-icons'
  17. import { checkAppAvailability, createApp } from '../../actions/apps'
  18. import { initForm, initField, setFieldNotification } from '../../actions/forms'
  19. import { showNotification } from '../../actions/notifications'
  20. import { getForm } from '../../selectors/forms'
  21. import { getIsFetching } from '../../selectors/requests'
  22. import { useTheme } from '../../hooks'
  23. import { setTitle, valueFromForm } from '../../utils'
  24. import { AppState, NotificationType, AppThunkDispatch, RequestKey } from '../../types'
  25. import Title from '../../components/title'
  26. import Section from '../../components/section'
  27. import HorizontalRule from '../../components/horizontal-rule'
  28. import PrimaryButton from '../../components/controls/primary-button'
  29. import TextField from '../../components/controls/text-field'
  30. import TextareaField from '../../components/controls/textarea-field'
  31. import CheckboxField from '../../components/controls/checkbox-field'
  32. import ImageField from '../../components/controls/image-field'
  33. import CoverImageField from '../../components/controls/cover-image-field'
  34. import IconImageField from '../../components/controls/icon-image-field'
  35. const CreateApp: FC = () => {
  36. const theme = useTheme()
  37. const form = useSelector(getForm)
  38. const fetching = useSelector<AppState, boolean>(state => getIsFetching(state, RequestKey.CreateApp))
  39. const dispatch = useDispatch<AppThunkDispatch>()
  40. const history = useHistory()
  41. const checkAvailability = (value: string) => {
  42. if (value.length > 3) {
  43. dispatch(checkAppAvailability(value))
  44. }
  45. }
  46. const handleCreate = async () => {
  47. let invalid = false
  48. const name = valueFromForm<string>(form, 'name')
  49. const about = valueFromForm<string>(form, 'about')
  50. const websiteUrl = valueFromForm<string>(form, 'websiteUrl')
  51. const companyName = valueFromForm<string>(form, 'companyName')
  52. const version = valueFromForm<string>(form, 'version')
  53. const composerUrl = valueFromForm<string>(form, 'composerUrl')
  54. const rendererUrl = valueFromForm<string>(form, 'rendererUrl')
  55. const imageUrl = valueFromForm<string>(form, 'image')
  56. const coverImageUrl = valueFromForm<string>(form, 'coverImage')
  57. const iconImageUrl = valueFromForm<string>(form, 'iconImage')
  58. const agree = valueFromForm<boolean>(form, 'agree')
  59. if (!name || name === '') {
  60. dispatch(setFieldNotification('name', NotificationType.Error, 'This is required'))
  61. invalid = true
  62. }
  63. if (!version || version === '') {
  64. dispatch(setFieldNotification('version', NotificationType.Error, 'This is required'))
  65. invalid = true
  66. }
  67. if (!agree) {
  68. dispatch(setFieldNotification('agree', NotificationType.Error, 'You must agree to the terms and conditions to continue'))
  69. dispatch(showNotification(NotificationType.Error, 'You must agree to the terms and conditions to continue.'))
  70. invalid = true
  71. }
  72. if (invalid) return
  73. const id = await dispatch(createApp({
  74. name: name!,
  75. version: version!,
  76. about,
  77. websiteUrl,
  78. companyName,
  79. composerUrl,
  80. rendererUrl,
  81. imageUrl,
  82. coverImageUrl,
  83. iconImageUrl,
  84. }))
  85. dispatch(showNotification(NotificationType.Success, 'App created successfully!'))
  86. setTimeout(() => {
  87. history.push(`/a/${id}`)
  88. }, 1000)
  89. }
  90. useEffect(() => {
  91. setTitle('Create a new App')
  92. dispatch(initForm())
  93. dispatch(initField('name', ''))
  94. dispatch(initField('about', ''))
  95. dispatch(initField('websiteUrl', ''))
  96. dispatch(initField('companyName', ''))
  97. dispatch(initField('version', ''))
  98. dispatch(initField('composerUrl', ''))
  99. dispatch(initField('rendererUrl', ''))
  100. dispatch(initField('agree', false))
  101. }, [])
  102. return (
  103. <div>
  104. <Section>
  105. <Title>Create a new App</Title>
  106. <HorizontalRule />
  107. <TextField name="name" label="Name" icon={faIdCard} placeholder="App ID/Name" onBlur={e => checkAvailability(e.target.value)} />
  108. <TextareaField name="about" label="About" placeholder="Description of this app" />
  109. <TextField name="websiteUrl" label="Website" icon={faLink} placeholder="Website URL (optional)" />
  110. <TextField name="companyName" label="Company" icon={faAddressBook} placeholder="Your company or organization (optional)" />
  111. <TextField name="version" label="Version" icon={faCodeBranch} placeholder="Current Version of the app (ex: 0.0.1beta5)" />
  112. <HorizontalRule />
  113. <ImageField name="image" />
  114. <CoverImageField name="coverImage" />
  115. <IconImageField name="iconImage" />
  116. <HorizontalRule />
  117. <TextField name="composerUrl" label="Composer URL" icon={faLink} placeholder="URL for the composer web page" />
  118. <TextField name="rendererUrl" label="Renderer URL" icon={faLink} placeholder="URL for the renderer template" />
  119. <CheckboxField name="agree">
  120. I agree to the Apps <Link style={{ color: theme.primary }} to="/terms/apps">terms and conditions</Link>.
  121. </CheckboxField>
  122. <br /><br />
  123. <PrimaryButton text="Create" icon={faCheckCircle} loading={fetching} onClick={() => handleCreate()} />
  124. </Section>
  125. </div>
  126. )
  127. }
  128. export default CreateApp