2019-08-18 00:33:06 +03:00
|
|
|
import {getMessages, saveMessage} from "../utils/api";
|
2019-08-16 17:20:50 +03:00
|
|
|
import { showLoading, hideLoading } from "react-redux-loading";
|
|
|
|
|
|
|
|
export const ADD_MESSAGE = "ADD_MESSAGE";
|
|
|
|
export const RECEIVE_MESSAGES = "RECEIVE_MESSAGES";
|
|
|
|
|
|
|
|
function addMessage(message) {
|
|
|
|
return {
|
|
|
|
type: ADD_MESSAGE,
|
|
|
|
message
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-08-18 00:33:06 +03:00
|
|
|
export function fetchPosts(counter) {
|
|
|
|
return dispatch => {
|
|
|
|
|
|
|
|
return getMessages().then((messages) => {
|
|
|
|
dispatch(receiveMessages(messages, counter));
|
|
|
|
dispatch(hideLoading());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|