Skip to content

Commit e7dfffa

Browse files
committed
feat: no more need to login again when sending multilples messages
1 parent c856c7f commit e7dfffa

File tree

7 files changed

+43
-19
lines changed

7 files changed

+43
-19
lines changed

src/Components/Feedy.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ class Feedy extends Component {
4949
ReactDom.findDOMNode(this).style.bottom = 0;
5050
}
5151
else {
52-
5352
ReactDom.findDOMNode(this).style.bottom = `${-this.getInnerHeight()}px`;
5453
}
5554
}
5655

5756
getInnerHeight() {
58-
return outerHeight(ReactDom.findDOMNode(this)) - outerHeight(this.refs.header)
57+
return outerHeight(ReactDom.findDOMNode(this)) - outerHeight(this.refs.header);
5958
}
6059

6160
componentDidMount() {

src/Components/SendMessage.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,21 @@ class SendMessage extends Component {
5757
}
5858
}
5959

60+
logout() {
61+
this.props.dispatch(actions.logout());
62+
}
63+
6064
render() {
6165
return (
6266
<div>
6367
<p>
6468
Laissez-nous un message. Nous vous contacterons dès que possible.
6569
</p>
66-
<p>
70+
{!this.props.logged && <p>
6771
Pas de compte ?
6872
{' '}
6973
<Link to="/createAccount">Créez en un !</Link>
70-
</p>
74+
</p>}
7175
<form ref="form" onSubmit={this.sendMessage.bind(this)}>
7276
<div className={s.error} id="sendMessageError">{this.props.errorMsg}</div>
7377
{!this.props.logged && <div className={s.group}>
@@ -86,6 +90,10 @@ class SendMessage extends Component {
8690
required
8791
onChange={this.resetErrorMsg.bind(this)}></input>
8892
</div>}
93+
{this.props.logged && <div className={s.group}>
94+
Bonjour {this.props.email} !
95+
<div><a onClick={this.logout.bind(this)}>Se déconnecter</a></div>
96+
</div>}
8997
<div className={s.group}>
9098
<label htmlFor="textareaContent">Message</label>
9199
<textarea id="textareaContent" ref="message" rows="6" required></textarea>
@@ -104,5 +112,6 @@ class SendMessage extends Component {
104112

105113
export default connect(state => ({
106114
logged: state.main.logged,
115+
email: state.main.auth && state.main.auth.email,
107116
errorMsg: state.main.sendMessageError
108117
}))(SendMessage);

src/actions/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export const toggleVisibility = createAction('TOGGLE_VISIBILITY');
66
export const sendMessage = createAction('SEND_MESSAGE');
77
export const sendMessageError = createAction('SEND_MESSAGE_ERROR');
88
export const loginAndSendMessage = createAction('LOGIN_AND_SEND_MESSAGE');
9+
export const logout = createAction('LOGOUT');
10+
export const logged = createAction('LOGGED');
11+
export const unlogged = createAction('UNLOGGED');
912
export const createUser = createAction('CREATE_USER');
10-
export const userCreated = createAction('USER_CREATED');
1113
export const createUserError = createAction('CREATE_USER_ERROR');
1214
export const resetErrorMsg = createAction('RESET_ERROR_MSG');

src/reducers/main.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ export default function(state=initialState, {type, payload}) {
1616
case 'LOGGED':
1717
return {
1818
...state,
19-
logged: true
19+
logged: true,
20+
auth: {
21+
email: payload.email,
22+
uid: payload.uid
23+
}
2024
};
2125
case 'UNLOGGED':
2226
return {
2327
...state,
24-
logged: false
28+
logged: false,
29+
email: undefined
2530
};
2631
case 'SEND_MESSAGE_ERROR':
2732
return {
@@ -39,13 +44,6 @@ export default function(state=initialState, {type, payload}) {
3944
createUserError: null,
4045
sendMessageError: null
4146
};
42-
case 'USER_CREATED':
43-
return {
44-
...state,
45-
auth: {
46-
...payload.auth
47-
}
48-
};
4947
default:
5048
return state;
5149
}

src/sagas/index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ import {
44
init,
55
login as webcomLogin,
66
createAccount,
7-
logout,
7+
logout as logoutWebcom,
88
push,
99
getUserData,
1010
screenshot
1111
} from 'services';
1212
import { Components, Component } from 'actions';
1313
import * as actions from 'actions';
14+
import { getAuth } from './selectors';
1415

1516
let appRouter;
1617

1718
export function* login(email, password) {
1819
try {
1920
const auth = yield call(webcomLogin, email, password);
20-
yield put(actions.logged, auth);
21+
yield put(actions.logged({email: auth.email, uid: auth.uid}));
2122
return auth;
2223
}
2324
catch (e) {
@@ -45,7 +46,9 @@ export function* sendMessage(message, auth, takeScreenshot) {
4546
};
4647
}
4748

48-
yield call(push, 'message', data);
49+
const path = `${encodeURIComponent(data.domain)}}/messages`;
50+
51+
yield call(push, path, data);
4952
yield call(appRouter.push, '/messageSent');
5053
}
5154

@@ -58,13 +61,15 @@ export function* watchInit() {
5861
export function* watchSendMessage() {
5962
while (true) {
6063
const { payload: {
61-
content,
64+
message,
6265
takeScreenshot }
6366
} = yield take('SEND_MESSAGE');
6467

68+
const auth = yield select(getAuth);
69+
6570
// get auth
6671
if ( auth ) {
67-
yield call(sendMessage, content, auth, takeScreenshot);
72+
yield call(sendMessage, message, auth, takeScreenshot);
6873
}
6974
}
7075
}
@@ -105,9 +110,18 @@ export function* watchCreateUser() {
105110
}
106111
}
107112

113+
export function* watchLogout() {
114+
while (true) {
115+
yield take('LOGOUT');
116+
yield call(logoutWebcom);
117+
yield put(actions.unlogged());
118+
}
119+
}
120+
108121
export default function* root() {
109122
yield [
110123
fork(watchInit),
124+
fork(watchLogout),
111125
fork(watchSendMessage),
112126
fork(watchLoginAndSendMessage),
113127
fork(watchCreateUser)

src/sagas/selectors/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const getAuth = state => state.main.auth;

src/services/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const removeUndefined = obj => {
7171
export function getUserData() {
7272
return {
7373
url: window.location.toString(),
74+
domain: window.location.host,
7475
userAgent: removeUndefined((new uaParser()).getResult())
7576
};
7677
}

0 commit comments

Comments
 (0)