Skip to content
Snippets Groups Projects
auth.js 1.53 KiB
Newer Older
  • Learn to ignore specific revisions
  • // TODO: Separate actions
    
    
    import ax from 'axios';
    
    import { GET_USERDATA, PROFILE_CHANGE, GROUP_CHANGE, LOGOUT, GET_NEWS } from './types';
    
    
    const axios = ax.create({
      xsrfCookieName: 'csrftoken',
      xsrfHeaderName: 'X-CSRFToken',
    });
    
    export const getUserData = () => (
      async (dispatch) => {
        const user = await axios.get('/api/v1/profiles/me');
    
    Tamás Szabó's avatar
    Tamás Szabó committed
        const {
          id, join_date: joinDate, nick, motivation, signed, groups,
        } = user.data;
        dispatch({
          type: GET_USERDATA,
          payload: {
            id, joinDate, nick, motivation, signed, groups,
          },
        });
    
    export const getNews = () => (
      async (dispatch) => {
        const news = await axios.get('/api/v1/news');
        dispatch({
          type: GET_NEWS,
          payload: news,
        });
      }
    );
    
    
    export const textChange = ({ target: { name, value } }) => (
      (dispatch) => {
        dispatch({ type: PROFILE_CHANGE, payload: value, target: name });
      }
    );
    
    export const groupChange = groups => (
      dispatch => (dispatch({ type: GROUP_CHANGE, payload: groups }))
    );
    
    
    Tamás Szabó's avatar
    Tamás Szabó committed
    export const submitRegistration = ({
      nick, groups, signed, motivation, id,
    }) => (
    
      async (dispatch) => {
        const response = await axios.patch(`/api/v1/profiles/${id}/`, {
          nick, groups, signed, motivation,
        });
        if (response.data.id === id) {
          alert('Sikeres mentés!');
        } else {
          alert('Mentés nem sikerült!');
        }
      }
    );
    
    Tamás Szabó's avatar
    Tamás Szabó committed
    
    export const logout = () => (
      async (dispatch) => {
        const response = await axios.get('/api/v1/logout/');
        if (response) {
          dispatch({ action: LOGOUT });
        }
      }
    );