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

155 lines
6.2 KiB

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
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. // self.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 } from 'react'
  14. import { useSelector, useDispatch } from 'react-redux'
  15. import { useHistory } from 'react-router-dom'
  16. import { faDoorOpen, faCheckCircle, faIdCard, faEnvelope, faUserShield, faUserCircle } from '@fortawesome/free-solid-svg-icons'
  17. import { unauthenticate, updateSelf } from '../../actions/authentication'
  18. import { initForm, initField } from '../../actions/forms'
  19. import { getAuthenticatedUser } from '../../selectors/authentication'
  20. import { getForm } from '../../selectors/forms'
  21. import { handleApiError } from '../../api/errors'
  22. import { PRIVACY_OPTIONS } from '../../constants'
  23. import { useAuthenticationCheck, useDeepCompareEffect } from '../../hooks'
  24. import { setTitle, valueFromForm } from '../../utils'
  25. import Title from '../../components/title'
  26. import Subtitle from '../../components/subtitle'
  27. import Section from '../../components/section'
  28. import HorizontalRule from '../../components/horizontal-rule'
  29. import PrimaryButton from '../../components/controls/primary-button'
  30. import SecondaryButton from '../../components/controls/secondary-button'
  31. import Loading from '../../components/pages/loading'
  32. import TextField from '../../components/controls/text-field'
  33. import TextareaField from '../../components/controls/textarea-field'
  34. import SelectField from '../../components/controls/select-field'
  35. import CheckboxField from '../../components/controls/checkbox-field'
  36. import ImageField from '../../components/controls/image-field'
  37. import CoverImageField from '../../components/controls/cover-image-field'
  38. import ThemeField from '../../components/controls/theme-field'
  39. import StaticField from '../../components/controls/static-field'
  40. const Self: FC = () => {
  41. const dispatch = useDispatch()
  42. const history = useHistory()
  43. const user = useSelector(getAuthenticatedUser)
  44. const form = useSelector(getForm)
  45. useAuthenticationCheck()
  46. const handleLogout = () => {
  47. localStorage.clear()
  48. dispatch(unauthenticate())
  49. window.location.href = '/'
  50. }
  51. const handleUpdate = () => {
  52. if (!user) return
  53. const settings = user.settings ?? {}
  54. const name = valueFromForm<string>(form, 'name', '')
  55. const about = valueFromForm<string>(form, 'about', '')
  56. const requiresApproval = valueFromForm<boolean>(form, 'requiresApproval', true)
  57. const privacy = valueFromForm<string>(form, 'privacy', 'public')
  58. const imageUrl = valueFromForm<string>(form, 'image', '')
  59. const coverImageUrl = valueFromForm<string>(form, 'coverImage', '')
  60. const theme = valueFromForm<string>(form, 'theme', '')
  61. const allowThemeChange = valueFromForm<boolean>(form, 'allowThemeChange', true)
  62. try {
  63. dispatch(updateSelf({
  64. name,
  65. about,
  66. requiresApproval,
  67. privacy,
  68. imageUrl,
  69. coverImageUrl,
  70. theme,
  71. settings: {
  72. ...settings,
  73. allowThemeChange,
  74. }
  75. }))
  76. } catch (err) {
  77. handleApiError(err, dispatch, history)
  78. }
  79. }
  80. useDeepCompareEffect(() => {
  81. if (user) {
  82. setTitle(`${user.name} @${user.id}`)
  83. dispatch(initForm())
  84. dispatch(initField('name', user.name))
  85. dispatch(initField('about', user.about ?? ''))
  86. dispatch(initField('requiresApproval', user.requiresApproval))
  87. dispatch(initField('privacy', user.privacy))
  88. dispatch(initField('image', user.imageUrl ?? ''))
  89. dispatch(initField('coverImage', user.coverImageUrl ?? ''))
  90. dispatch(initField('theme', user.theme))
  91. dispatch(initField('allowThemeChange', user.settings.allowThemeChange))
  92. }
  93. }, [user])
  94. if (!user) return <Loading />
  95. return (
  96. <div>
  97. <Section>
  98. <Title>{user.name ?? user.id}</Title>
  99. <Subtitle>@{user.id}</Subtitle>
  100. <HorizontalRule />
  101. <PrimaryButton text="Your Page" icon={faUserCircle} onClick={() => history.push(`/u/${user.id}`)} />
  102. <StaticField label="ID" icon={faIdCard} value={user.id} />
  103. <StaticField label="ID" icon={faEnvelope} value={user.email} />
  104. <TextField name="name" label="Name" placeholder="Your Display Name" />
  105. <TextareaField name="about" label="About" placeholder="Your Bio" />
  106. <ThemeField name="theme" label="Color" />
  107. <SelectField name="privacy" label="Privacy" options={PRIVACY_OPTIONS} icon={faUserShield} />
  108. <ImageField name="image" label="Avatar" />
  109. <CoverImageField name="coverImage" />
  110. <CheckboxField name="requiresApproval">
  111. Approve each Subscription request from other users.
  112. </CheckboxField>
  113. <br />
  114. <CheckboxField name="allowThemeChange">
  115. Allow theme changes on User and Community pages.
  116. </CheckboxField>
  117. <HorizontalRule />
  118. <nav className="level">
  119. <div>
  120. <PrimaryButton text="Save" icon={faCheckCircle} onClick={() => handleUpdate()} />
  121. </div>
  122. <div>
  123. <SecondaryButton text="Log Out" icon={faDoorOpen} onClick={() => handleLogout()} />
  124. </div>
  125. </nav>
  126. </Section>
  127. </div>
  128. )
  129. }
  130. export default Self