Newer
Older
import axios from './session';
Chif Gergő
committed
import { GET_NEWS, WRITE_NEWS, ADD_NEWS, DELETE_NEWS,
CLEAR_WRITE, SELECT_NEWS, EDIT_NEWS } from './types';
export const getNews = () => (
async (dispatch) => {
try {
const response = await axios.get('/api/v1/news');
dispatch({
type: GET_NEWS,
payload: response.data,
});
}
}
);
export const postNews = ({ title, author, text }) => (
async (dispatch) => {
try {
const response = await axios.post('/api/v1/news/', {
if (response.data.id) {
alert('Sikeres mentés!');
Chif Gergő
committed
dispatch({
type: ADD_NEWS,
payload: response.data,
});
} else {
alert('Mentés nem sikerült!');
}
}
);
export const editNews = ({ id, title, text, updated_by}) => (
Chif Gergő
committed
async (dispatch) => {
try {
const response = await axios.patch(`/api/v1/news/${id}/`, {
Chif Gergő
committed
title,
text,
});
if (response.data.id) {
alert('Sikeres mentés!');
dispatch({
type: EDIT_NEWS,
payload: response.data,
});
} else {
alert('Mentés nem sikerült!');
}
} catch (e) {
console.log(e);
}
}
);
export const deleteNews = news => (
async (dispatch) => {
try {
Chif Gergő
committed
const response = await axios.delete(`/api/v1/news/${news.id}/`);
if (!response.data.id) {
alert('Sikeres törlés!');
dispatch({
type: DELETE_NEWS,
payload: news,
});
} else {
alert('A törlés nem sikerült!');
}
Chif Gergő
committed
console.log(e);
}
});
export const writeNews = ({ target: { name, value } }) => (
(dispatch) => {
dispatch({ type: WRITE_NEWS, payload: value, target: name });
}
);
Chif Gergő
committed
export const clearWrite = () => (
(dispatch) => {
dispatch({ type: CLEAR_WRITE });
}
);
Chif Gergő
committed
export const setSelectedNews = item => (
(dispatch) => {
dispatch({ type: SELECT_NEWS, payload: item });
}
);