Skip to content

Commit

Permalink
[FIX] Se soluciono bug de limpieza de eventos de localstorage al salir
Browse files Browse the repository at this point in the history
  • Loading branch information
damiancolaneri committed Feb 15, 2021
1 parent a1a33b8 commit c197f90
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .eslintcache

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Swal from 'sweetalert2';
import { fetchSinToken, fetchConToken } from '../helpers/fetch';
import { types } from '../types/types';
import { eventLogout } from '../actions/events';

export const startLogin = (email, password) => {
return async (dispatch) => {
Expand Down Expand Up @@ -76,6 +77,7 @@ const checkingFinish = () => ({
export const startLogout = () => {
return (dispatch) => {
localStorage.clear();
dispatch(eventLogout());
dispatch(logout());
};
};
Expand Down
40 changes: 39 additions & 1 deletion src/actions/events.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Swal from 'sweetalert2';
import { fetchConToken } from '../helpers/fetch';
import { prepareEvents } from '../helpers/prepareEvents';
import { types } from '../types/types';
Expand Down Expand Up @@ -39,11 +40,46 @@ export const eventClearActiveEvent = () => ({
type: types.eventClearActiveEvent,
});

export const eventStartUpdated = (event) => {
return async (dispatch) => {
try {
const resp = await fetchConToken(`events/${event._id}`, event, 'PUT');
const body = await resp.json();

if (body.ok) {
dispatch(eventUpdated(event));
} else {
Swal.fire('Error', body.msg);
}
} catch (error) {
console.log(error);
}
};
};

export const eventUpdated = (event) => ({
type: types.eventUpdate,
type: types.eventUpdated,
payload: event,
});

export const eventStartDelete = () => {
return async (dispatch, getState) => {
const { _id } = getState().calendar.activeEvent;
try {
const resp = await fetchConToken(`events/${_id}`, {}, 'DELETE');
const body = await resp.json();

if (body.ok) {
dispatch(eventDeleted());
} else {
Swal.fire('Error', body.msg);
}
} catch (error) {
console.log(error);
}
};
};

export const eventDeleted = () => ({
type: types.eventDeleted,
});
Expand All @@ -67,3 +103,5 @@ const eventLoaded = (events) => ({
type: types.eventLoaded,
payload: events,
});

export const eventLogout = () => ({ type: types.eventLogout });
17 changes: 3 additions & 14 deletions src/components/calendar/CalendarModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { uiCloseModal } from '../../actions/ui';
import {
eventStartAddNew,
eventClearActiveEvent,
eventUpdated,
eventStartUpdated,
} from '../../actions/events';

const customStyles = {
Expand Down Expand Up @@ -100,21 +100,10 @@ export const CalendarModal = () => {
Swal.fire('Error', 'El titulo debe tener mas de 2 caracteres', 'error');
}

//TODO: realizar grabacion en DB

if (activeEvent) {
dispatch(eventUpdated(formValues));
dispatch(eventStartUpdated(formValues));
} else {
dispatch(
eventStartAddNew({
...formValues,
id: new Date().getTime(),
user: {
_id: '123',
name: 'Damian',
},
})
);
dispatch(eventStartAddNew(formValues));
}

closeModal();
Expand Down
3 changes: 2 additions & 1 deletion src/components/calendar/CalendarScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const localizer = momentLocalizer(moment);
export const CalendarScreen = () => {
const dispatch = useDispatch();
const { events, activeEvent } = useSelector((state) => state.calendar);
const { uid } = useSelector((state) => state.auth);

const [lastView, setLastView] = useState(
localStorage.getItem('lastView') || 'month'
Expand Down Expand Up @@ -56,7 +57,7 @@ export const CalendarScreen = () => {

const eventStyleGetter = (event, start, end, isSelected) => {
const style = {
backgroundColor: '#367CF7',
backgroundColor: uid === event.user._id ? '#367CF7' : '#465660',
borderRadius: '3px',
opacity: 0.8,
display: 'block',
Expand Down
4 changes: 2 additions & 2 deletions src/components/ui/DeleteEventFab.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import { useDispatch } from 'react-redux';
import { eventDeleted } from '../../actions/events';
import { eventStartDelete } from '../../actions/events';

export const DeleteEventFab = () => {
const dispatch = useDispatch();

const handleDelete = () => {
dispatch(eventDeleted());
dispatch(eventStartDelete());
};

return (
Expand Down
5 changes: 5 additions & 0 deletions src/reducers/calendarReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export const calendarReducer = (state = initialState, action) => {
events: [...action.payload],
};

case types.eventLogout:
return {
...initialState,
};

default:
return state;
}
Expand Down
1 change: 1 addition & 0 deletions src/types/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const types = {
eventUpdated: '[event] Event Updated',
eventDeleted: '[event] Deleted event',
eventLoaded: '[event] Event loaded',
eventLogout: '[event] Event logout',

authChecking: '[auth] Checking login state',
authCheckingFinish: '[auth] Finish checking login state',
Expand Down

0 comments on commit c197f90

Please sign in to comment.