Skip to content
Snippets Groups Projects
news.js 1.96 KiB
Newer Older
  • Learn to ignore specific revisions
  • Rafael László's avatar
    Rafael László committed
    import {
    
    Rafael László's avatar
    Rafael László committed
      ADD_NEWS,
      CLEAR_WRITE,
      DELETE_NEWS,
      EDIT_NEWS,
      GET_NEWS,
      SELECT_NEWS,
      WRITE_NEWS,
    
    Rafael László's avatar
    Rafael László committed
    } from './types';
    
    Rafael László's avatar
    Rafael László committed
    import axios from './session';
    
    export const getNews = () => async (dispatch) => {
      try {
        const response = await axios.get('/api/v1/news');
        dispatch({
          type: GET_NEWS,
          payload: response.data,
        });
      } catch (e) {
        console.log(e);
      }
    };
    
    export const postNews = ({ title, updated_by, text }) => async (dispatch) => {
      try {
        const response = await axios.post('/api/v1/news/', {
          updated_by,
          title,
          text,
        });
        if (response.data.id) {
          alert('Sikeres mentés!');
    
    Rafael László's avatar
    Rafael László committed
            type: ADD_NEWS,
    
    Rafael László's avatar
    Rafael László committed
        } else {
          alert('Mentés nem sikerült!');
    
    Rafael László's avatar
    Rafael László committed
      } catch (e) {
        console.log(e);
    
    Rafael László's avatar
    Rafael László committed
    };
    
    Rafael László's avatar
    Rafael László committed
    export const editNews = ({ id, title, text, updated_by }) => async (
      dispatch
    ) => {
      try {
        const response = await axios.patch(`/api/v1/news/${id}/`, {
          updated_by,
          title,
          text,
        });
        if (response.data.id) {
          alert('Sikeres mentés!');
          dispatch({
            type: EDIT_NEWS,
            payload: response.data,
    
    Rafael László's avatar
    Rafael László committed
        } else {
          alert('Mentés nem sikerült!');
    
    Rafael László's avatar
    Rafael László committed
      } catch (e) {
        console.log(e);
    
    Rafael László's avatar
    Rafael László committed
    };
    
    Rafael László's avatar
    Rafael László committed
    export const deleteNews = (news) => async (dispatch) => {
      try {
        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,
    
    Rafael László's avatar
    Rafael László committed
        } else {
          alert('A törlés nem sikerült!');
    
    Rafael László's avatar
    Rafael László committed
      } catch (e) {
        console.log(e);
    
    Rafael László's avatar
    Rafael László committed
    };
    
    Rafael László's avatar
    Rafael László committed
    export const writeNews = ({ target: { name, value } }) => (dispatch) => {
      dispatch({ type: WRITE_NEWS, payload: value, target: name });
    };
    
    Rafael László's avatar
    Rafael László committed
    export const clearWrite = () => (dispatch) => {
      dispatch({ type: CLEAR_WRITE });
    };
    
    Rafael László's avatar
    Rafael László committed
    export const setSelectedNews = (item) => (dispatch) => {
      dispatch({ type: SELECT_NEWS, payload: item });
    };