Skip to content

Commit 19522b6

Browse files
committed
factor out settings and login local storage actions
1 parent 09d3bb1 commit 19522b6

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

front.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import UserAPI from 'front/api/user'
2+
import { mutate } from 'swr'
3+
14
export const AUTH_COOKIE_NAME = 'auth'
25
export const AUTH_LOCAL_STORAGE_NAME = 'user'
36

@@ -50,3 +53,21 @@ export function getCookiesFromString(s) {
5053
export function deleteCookie(name, path = '/') {
5154
setCookie(name, '', -1, path)
5255
}
56+
57+
export async function setupUserLocalStorage(data, setErrors) {
58+
// We fetch from /profiles/:username again because the return from /users/login above
59+
// does not contain the image placeholder.
60+
const { data: profileData, status: profileStatus } = await UserAPI.get(
61+
data.user.username
62+
)
63+
if (profileStatus !== 200) {
64+
setErrors(profileData.errors)
65+
}
66+
data.user.effectiveImage = profileData.profile.image
67+
window.localStorage.setItem(
68+
AUTH_LOCAL_STORAGE_NAME,
69+
JSON.stringify(data.user)
70+
)
71+
setCookie(AUTH_COOKIE_NAME, data.user.token)
72+
mutate(AUTH_LOCAL_STORAGE_NAME, data.user)
73+
}

front/LoginForm.tsx

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import Router from 'next/router'
22
import React from 'react'
3-
import { mutate } from 'swr'
43

4+
import { setupUserLocalStorage } from 'front'
55
import ListErrors from 'front/ListErrors'
66
import UserAPI from 'front/api/user'
7-
import { AUTH_COOKIE_NAME, AUTH_LOCAL_STORAGE_NAME, setCookie } from 'front'
87
import { useCtrlEnterSubmit } from 'front/ts'
98

109
const LoginForm = ({ register = false }) => {
@@ -39,21 +38,7 @@ const LoginForm = ({ register = false }) => {
3938
setErrors(data.errors)
4039
}
4140
if (data?.user) {
42-
// We fetch from /profiles/:username again because the return from /users/login above
43-
// does not contain the image placeholder.
44-
const { data: profileData, status: profileStatus } = await UserAPI.get(
45-
data.user.username
46-
)
47-
if (profileStatus !== 200) {
48-
setErrors(profileData.errors)
49-
}
50-
data.user.effectiveImage = profileData.profile.image
51-
window.localStorage.setItem(
52-
AUTH_LOCAL_STORAGE_NAME,
53-
JSON.stringify(data.user)
54-
)
55-
setCookie(AUTH_COOKIE_NAME, data.user.token)
56-
mutate(AUTH_LOCAL_STORAGE_NAME, data.user)
41+
await setupUserLocalStorage(data, setErrors)
5742
Router.push('/')
5843
}
5944
} catch (error) {

front/SettingsForm.tsx

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import axios from 'axios'
22
import Router from 'next/router'
33
import React from 'react'
4-
import { mutate } from 'swr'
54

5+
import { setupUserLocalStorage } from 'front'
66
import { apiPath } from 'front/config'
77
import ListErrors from 'front/ListErrors'
88
import useLoggedInUser from 'front/useLoggedInUser'
9-
import UserAPI from 'front/api/user'
109
import { useCtrlEnterSubmit } from 'front/ts'
1110

1211
const SettingsForm = () => {
@@ -49,15 +48,7 @@ const SettingsForm = () => {
4948
setErrors(data.errors.body)
5049
}
5150
if (data?.user) {
52-
const { data: profileData, status: profileStatus } = await UserAPI.get(
53-
data.user.username
54-
)
55-
if (profileStatus !== 200) {
56-
setErrors(profileData.errors)
57-
}
58-
data.user.effectiveImage = profileData.profile.image
59-
window.localStorage.setItem('user', JSON.stringify(data.user))
60-
mutate('user', data.user)
51+
await setupUserLocalStorage(data, setErrors)
6152
Router.push(`/profile/${user.username}`)
6253
}
6354
}

0 commit comments

Comments
 (0)