fluid/frontend/src/actions/messages.js

60 lines
1.4 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)
}
export function fetchPosts(counter) {
2019-08-18 00:33:06 +03:00
return dispatch => {
return getMessages().then((messages) => {
dispatch(receiveMessages(messages, counter));
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) => {
dispatch(receiveMessages(messages, 0));
//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-18 00:33:06 +03:00
export function receiveMessages(messages, counter) {
2019-08-16 17:20:50 +03:00
return {
type: RECEIVE_MESSAGES,
2019-08-18 00:33:06 +03:00
counter,
2019-08-16 17:20:50 +03:00
messages
};
}