fluid/frontend/src/actions/messages.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-08-18 15:34:52 +03:00
import {changeConnection, getMessages, saveMessage} from "../fluence/api";
2019-08-18 15:09:33 +03:00
import { showLoading, hideLoading } from "react-redux-loading-bar";
2019-08-16 17:20:50 +03:00
export const ADD_MESSAGE = "ADD_MESSAGE";
export const RECEIVE_MESSAGES = "RECEIVE_MESSAGES";
function addMessage(message) {
return {
type: ADD_MESSAGE,
message
};
}
2019-08-19 11:14:14 +03:00
export async function toggleConnection(devnet, appId = 413) {
2019-08-18 15:34:52 +03:00
return changeConnection(devnet, appId)
}
2019-08-20 14:35:57 +07:00
let lastResponseRequestTime = 0;
export function fetchPosts() {
2019-08-18 00:33:06 +03:00
return dispatch => {
2019-08-20 14:35:57 +07:00
const currentRequestTime = Date.now();
2019-08-18 00:33:06 +03:00
return getMessages().then((messages) => {
2019-08-20 14:35:57 +07:00
if (currentRequestTime > lastResponseRequestTime) {
lastResponseRequestTime = currentRequestTime;
dispatch(receiveMessages(messages));
}
2019-08-18 15:34:52 +03:00
})
2019-08-18 00:33:06 +03:00
};
}
export function handleInitialData() {
return dispatch => {
//before retrieving info, show loading bar
dispatch(showLoading());
return getMessages().then((messages) => {
2019-08-20 14:35:57 +07:00
dispatch(receiveMessages(messages));
2019-08-18 00:33:06 +03:00
//after everything has loaded, hide loading bar
dispatch(hideLoading());
});
};
}
2019-08-16 17:20:50 +03:00
export function handleAddMessage(text, name) {
return (dispatch) => {
dispatch(showLoading());
return saveMessage({
text: text,
name: name
})
.then(message => dispatch(addMessage(message)))
.then(() => dispatch(hideLoading()));
};
}
//action creator
2019-08-20 14:35:57 +07:00
export function receiveMessages(messages) {
2019-08-16 17:20:50 +03:00
return {
type: RECEIVE_MESSAGES,
messages
};
}