Client: minor refactor (user)

This commit is contained in:
Sam 2018-04-29 17:02:08 +02:00
parent cd90c802b5
commit 1c570ab320
5 changed files with 21 additions and 16 deletions

View File

@ -1,4 +1,4 @@
import mpwoApi from '../mpwoApi' import mpwoApiUser from '../mwpoApi/user'
import { history } from '../index' import { history } from '../index'
import { generateIds } from '../utils' import { generateIds } from '../utils'
@ -48,7 +48,7 @@ export const updateProfileFormData = (target, value) => ({
}) })
export function getProfile(dispatch) { export function getProfile(dispatch) {
return mpwoApi return mpwoApiUser
.getProfile() .getProfile()
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
@ -64,7 +64,7 @@ export function getProfile(dispatch) {
export function register(formData) { export function register(formData) {
return function(dispatch) { return function(dispatch) {
return mpwoApi return mpwoApiUser
.register( .register(
formData.username, formData.username,
formData.email, formData.email,
@ -86,7 +86,7 @@ export function register(formData) {
export function login(formData) { export function login(formData) {
return function(dispatch) { return function(dispatch) {
return mpwoApi return mpwoApiUser
.login(formData.email, formData.password) .login(formData.email, formData.password)
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
@ -165,7 +165,7 @@ export function handleProfileFormSubmit(event) {
'Password and password confirmation don\'t match.' 'Password and password confirmation don\'t match.'
)) ))
} else { } else {
return mpwoApi return mpwoApiUser
.updateProfile(state.formProfile.formProfile) .updateProfile(state.formProfile.formProfile)
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
@ -189,7 +189,7 @@ export function uploadPicture (event) {
form.append('file', event.target.picture.files[0]) form.append('file', event.target.picture.files[0])
event.target.reset() event.target.reset()
return function(dispatch) { return function(dispatch) {
return mpwoApi return mpwoApiUser
.updatePicture(form) .updatePicture(form)
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
@ -206,7 +206,7 @@ export function uploadPicture (event) {
export function deletePicture() { export function deletePicture() {
return function(dispatch) { return function(dispatch) {
return mpwoApi return mpwoApiUser
.deletePicture() .deletePicture()
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {

View File

@ -2,7 +2,7 @@ import React from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import mpwoApi from '../../mpwoApi' import { apiUrl } from '../../utils'
function NavBar (props) { function NavBar (props) {
@ -74,7 +74,7 @@ function NavBar (props) {
{props.user.picture === true && ( {props.user.picture === true && (
<img <img
alt="Profile" alt="Profile"
src={`${mpwoApi.getApiUrl()}users/${props.user.id}/picture` + src={`${apiUrl}users/${props.user.id}/picture` +
`?${Date.now()}`} `?${Date.now()}`}
className="img-fluid App-nav-profile-img" className="img-fluid App-nav-profile-img"
/> />

View File

@ -4,8 +4,8 @@ import { Helmet } from 'react-helmet'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import mpwoApi from '../../mpwoApi'
import { deletePicture, uploadPicture } from '../../actions' import { deletePicture, uploadPicture } from '../../actions'
import { apiUrl } from '../../utils'
function Profile ({ message, onDeletePicture, onUploadPicture, user }) { function Profile ({ message, onDeletePicture, onUploadPicture, user }) {
return ( return (
@ -49,7 +49,7 @@ function Profile ({ message, onDeletePicture, onUploadPicture, user }) {
<div> <div>
<img <img
alt="Profile" alt="Profile"
src={`${mpwoApi.getApiUrl()}users/${user.id}/picture` + src={`${apiUrl}users/${user.id}/picture` +
`?${Date.now()}`} `?${Date.now()}`}
className="img-fluid App-profile-img-small" className="img-fluid App-profile-img-small"
/> />

View File

@ -1,6 +1,7 @@
const apiUrl = `${process.env.REACT_APP_API_URL}/api/` import { apiUrl } from '../utils'
export default class MpwoApiUser {
export default class MpwoApi {
static login(email, password) { static login(email, password) {
const request = new Request(`${apiUrl}auth/login`, { const request = new Request(`${apiUrl}auth/login`, {
method: 'POST', method: 'POST',
@ -16,6 +17,7 @@ export default class MpwoApi {
.then(response => response.json()) .then(response => response.json())
.catch(error => error) .catch(error => error)
} }
static register(username, email, password, passwordConf) { static register(username, email, password, passwordConf) {
const request = new Request(`${apiUrl}auth/register`, { const request = new Request(`${apiUrl}auth/register`, {
method: 'POST', method: 'POST',
@ -33,6 +35,7 @@ export default class MpwoApi {
.then(response => response.json()) .then(response => response.json())
.catch(error => error) .catch(error => error)
} }
static getProfile() { static getProfile() {
const request = new Request(`${apiUrl}auth/profile`, { const request = new Request(`${apiUrl}auth/profile`, {
method: 'GET', method: 'GET',
@ -45,6 +48,7 @@ export default class MpwoApi {
.then(response => response.json()) .then(response => response.json())
.catch(error => error) .catch(error => error)
} }
static updateProfile(form) { static updateProfile(form) {
const request = new Request(`${apiUrl}auth/profile/edit`, { const request = new Request(`${apiUrl}auth/profile/edit`, {
method: 'POST', method: 'POST',
@ -66,6 +70,7 @@ export default class MpwoApi {
.then(response => response.json()) .then(response => response.json())
.catch(error => error) .catch(error => error)
} }
static updatePicture(form) { static updatePicture(form) {
const request = new Request(`${apiUrl}auth/picture`, { const request = new Request(`${apiUrl}auth/picture`, {
method: 'POST', method: 'POST',
@ -78,6 +83,7 @@ export default class MpwoApi {
.then(response => response.json()) .then(response => response.json())
.catch(error => error) .catch(error => error)
} }
static deletePicture() { static deletePicture() {
const request = new Request(`${apiUrl}auth/picture`, { const request = new Request(`${apiUrl}auth/picture`, {
method: 'DELETE', method: 'DELETE',
@ -89,7 +95,4 @@ export default class MpwoApi {
.then(response => response.json()) .then(response => response.json())
.catch(error => error) .catch(error => error)
} }
static getApiUrl() {
return apiUrl
}
} }

View File

@ -1,3 +1,5 @@
export const apiUrl = `${process.env.REACT_APP_API_URL}/api/`
export const isLoggedIn = () => !!window.localStorage.authToken export const isLoggedIn = () => !!window.localStorage.authToken
export function generateIds(arr) { export function generateIds(arr) {