summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/user/containers
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2021-03-05 22:35:02 +0100
committerJules Laplace <julescarbon@gmail.com>2021-03-05 22:35:02 +0100
commite1cd664c663b960c600a9e4a2f3f11fddbbb4dc8 (patch)
treefd616f01b1e489477ed75206cef0e583de81e69b /animism-align/frontend/app/views/user/containers
parent501f0ab74ea7d8b1ee0d5ea4cdc84d200fa04d35 (diff)
user pages
Diffstat (limited to 'animism-align/frontend/app/views/user/containers')
-rw-r--r--animism-align/frontend/app/views/user/containers/user.edit.js70
-rw-r--r--animism-align/frontend/app/views/user/containers/user.index.js79
-rw-r--r--animism-align/frontend/app/views/user/containers/user.new.js76
3 files changed, 225 insertions, 0 deletions
diff --git a/animism-align/frontend/app/views/user/containers/user.edit.js b/animism-align/frontend/app/views/user/containers/user.edit.js
new file mode 100644
index 0000000..5d51792
--- /dev/null
+++ b/animism-align/frontend/app/views/user/containers/user.edit.js
@@ -0,0 +1,70 @@
+import React, { Component } from 'react'
+import { connect } from 'react-redux'
+
+import { history } from 'app/store'
+import actions from 'app/actions'
+
+import { Loader } from 'app/common'
+
+import UserForm from '../components/user.form'
+import UserMenu from '../components/user.menu'
+
+class UserEdit extends Component {
+ componentDidMount() {
+ console.log(this.props.match.params.id)
+ actions.user.show(this.props.match.params.id)
+ }
+
+ handleSubmit(data) {
+ actions.user.update(data)
+ .then(response => {
+ // response
+ console.log(response)
+ history.push('/users/')
+ })
+ .catch(err => {
+ console.log(err)
+ if (!err.errors) {
+ if (err.error) {
+ alert(err.error)
+ return
+ }
+ alert("There was an error saving this user.")
+ return
+ }
+ const errorStr = Object.keys(err.errors)
+ .map(key => (
+ err.errors[key].map(error =>
+ key + ": " + error
+ ).join("\n")
+ )).join("\n")
+ alert("There was an error updating this user.\n" + errorStr)
+ })
+ }
+
+ render() {
+ const { show } = this.props.user
+ if (show.loading || !show.res) {
+ return (
+ <div className='form'>
+ <Loader />
+ </div>
+ )
+ }
+ return (
+ <div className='row formContainer'>
+ <UserMenu userActions={this.props.userActions} />
+ <UserForm
+ data={show.res}
+ onSubmit={this.handleSubmit.bind(this)}
+ />
+ </div>
+ )
+ }
+}
+
+const mapStateToProps = state => ({
+ user: state.user,
+})
+
+export default connect(mapStateToProps)(UserEdit)
diff --git a/animism-align/frontend/app/views/user/containers/user.index.js b/animism-align/frontend/app/views/user/containers/user.index.js
new file mode 100644
index 0000000..8b207e0
--- /dev/null
+++ b/animism-align/frontend/app/views/user/containers/user.index.js
@@ -0,0 +1,79 @@
+import React, { Component } from 'react'
+import { Link } from 'react-router-dom'
+import { connect } from 'react-redux'
+
+import { Loader } from 'app/common'
+import actions from 'app/actions'
+
+import UserMenu from '../components/user.menu'
+
+// const { result, collectionLookup } = this.props
+
+class UserIndex extends Component {
+ componentDidMount() {
+ this.fetch()
+ }
+
+ fetch() {
+ actions.user.index()
+ }
+
+ render() {
+ const { currentUser } = this.props
+ const { loading, lookup, order } = this.props.user.index
+ if (loading) {
+ return (
+ <section>
+ <Loader />
+ </section>
+ )
+ }
+ if (!lookup || !order.length) {
+ return (
+ <section>
+ <div className="row user-index">
+ <UserMenu />
+ <div>
+ <h1>Users</h1>
+ <p className='gray'>
+ {"No users"}
+ </p>
+ </div>
+ </div>
+ </section>
+ )
+ }
+ return (
+ <section>
+ <div className="row user-index">
+ <UserMenu />
+ <div className="user-list">
+ <h1>Users</h1>
+ {order.map(id => {
+ const user = lookup[id]
+ return (
+ <div key={id}>
+ {(currentUser.is_admin || currentUser.id === user.id)
+ ? <Link to={"/users/" + id + "/edit/"}>
+ {user.username}
+ </Link>
+ : user.username
+ }
+ {user.is_admin && " (admin)"}
+ </div>
+ )
+ })}
+ </div>
+ </div>
+ {order.length >= 50 && <button className='loadMore' onClick={() => this.fetch(true)}>Load More</button>}
+ </section>
+ )
+ }
+}
+
+const mapStateToProps = state => ({
+ user: state.user,
+ currentUser: state.auth.user,
+})
+
+export default connect(mapStateToProps)(UserIndex)
diff --git a/animism-align/frontend/app/views/user/containers/user.new.js b/animism-align/frontend/app/views/user/containers/user.new.js
new file mode 100644
index 0000000..9d80e0a
--- /dev/null
+++ b/animism-align/frontend/app/views/user/containers/user.new.js
@@ -0,0 +1,76 @@
+import React, { Component } from 'react'
+import { Link } from 'react-router-dom'
+import { connect } from 'react-redux'
+
+import { history } from 'app/store'
+import actions from 'app/actions'
+
+import UserForm from '../components/user.form'
+import UserMenu from '../components/user.menu'
+
+class UserNew extends Component {
+ state = {
+ loading: true,
+ initialData: {},
+ }
+
+ componentDidMount() {
+ this.setState({ loading: false })
+ }
+
+ handleSubmit(data) {
+ console.log(data)
+ actions.user.create(data)
+ .then(res => {
+ console.log(res)
+ if (res.res && res.res.id) {
+ history.push('/users/')
+ }
+ })
+ .catch(err => {
+ if (!err.errors) {
+ if (err.error) {
+ alert(err.error)
+ return
+ }
+ alert("There was an error saving this user.")
+ return
+ }
+ const errorStr = Object.keys(err.errors)
+ .map(key => (
+ err.errors[key].map(error =>
+ key + ": " + error
+ ).join("\n")
+ )).join("\n")
+ alert("There was an error creating this user.\n" + errorStr)
+ })
+ }
+
+ render() {
+ if (this.state.loading) {
+ return (
+ <div className='row formContainer' />
+ )
+ }
+ return (
+ <div className='row formContainer'>
+ <UserMenu />
+ <UserForm
+ isNew
+ data={this.state.initialData}
+ onSubmit={this.handleSubmit.bind(this)}
+ />
+ </div>
+ )
+ }
+}
+
+const mapStateToProps = state => ({
+ user: state.user,
+})
+
+const mapDispatchToProps = dispatch => ({
+ // uploadActions: bindActionCreators({ ...uploadActions }, dispatch),
+})
+
+export default connect(mapStateToProps, mapDispatchToProps)(UserNew)