Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7305 | Rev 7307 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7292 stevensc 1
import React, { useEffect, useState } from 'react'
7279 stevensc 2
import { Container, Grid } from '@mui/material'
3
import { Calendar, momentLocalizer } from 'react-big-calendar'
4
import moment from 'moment'
5
import EventsList from '../../components/calendar/EventsList'
7287 stevensc 6
import ArrowBackIosIcon from '@mui/icons-material/ArrowBackIos'
7
import ArrowForwardIosIcon from '@mui/icons-material/ArrowForwardIos'
7283 stevensc 8
import 'react-big-calendar/lib/sass/styles.scss'
7292 stevensc 9
import { axios } from '../../utils'
7296 stevensc 10
import EventModal from '../../components/calendar/EventModal'
7279 stevensc 11
 
7281 stevensc 12
moment.locale('es-ES')
7279 stevensc 13
const localizer = momentLocalizer(moment)
14
 
15
const CalendarPage = () => {
7292 stevensc 16
  const [events, setEvents] = useState([])
7296 stevensc 17
  const [selectedEvent, setSelectedEvent] = useState(null)
18
  const [modalShow, setModalShow] = useState(false)
7279 stevensc 19
 
7296 stevensc 20
  const selectEvent = (event) => {
21
    setSelectedEvent(event)
22
    setModalShow(true)
23
  }
24
 
25
  const unselectEvent = () => {
26
    setSelectedEvent(null)
27
    setModalShow(false)
28
  }
29
 
7292 stevensc 30
  useEffect(() => {
31
    axios.get('/calendar/events').then((response) => {
32
      const events = response.data
33
 
7305 stevensc 34
      const formattedData = events.map((event) => {
7306 stevensc 35
        const eventStart = new Date(event.start.replace(/-/g, '/'))
7305 stevensc 36
        const eventEnd = event.allDay
37
          ? eventStart
7306 stevensc 38
          : new Date(event.end.replace(/-/g, '/'))
7292 stevensc 39
 
7305 stevensc 40
        return {
41
          ...event,
42
          start: eventStart,
43
          end: eventEnd,
44
        }
45
      })
46
 
7292 stevensc 47
      setEvents(formattedData)
48
    })
49
  }, [])
50
 
7279 stevensc 51
  return (
7296 stevensc 52
    <>
53
      <Container as="main" className="px-0">
54
        <Grid container spacing={2}>
55
          <Grid item xs={12} md={8} spacing={3}>
56
            <Calendar
57
              views={['day', 'week', 'month']}
58
              selectable
59
              localizer={localizer}
60
              defaultDate={new Date()}
61
              defaultView="month"
62
              onSelectEvent={selectEvent}
63
              events={events}
64
              messages={{
65
                next: <ArrowForwardIosIcon />,
66
                previous: <ArrowBackIosIcon />,
67
                today: 'Hoy',
68
                month: 'Mes',
69
                week: 'Semana',
70
                day: 'Día',
71
              }}
72
              eventPropGetter={(event) => {
73
                const backgroundColor = event.backgroundColor
74
                  ? event.backgroundColor
75
                  : 'blue'
76
                const color = event.textColor ? event.textColor : '#fff'
77
                return { style: { backgroundColor, color } }
78
              }}
79
            />
80
          </Grid>
81
          <Grid item xs={12} md={4}>
82
            <EventsList />
83
          </Grid>
7279 stevensc 84
        </Grid>
7296 stevensc 85
      </Container>
86
      <EventModal
87
        show={modalShow}
88
        event={selectedEvent}
89
        onClose={unselectEvent}
90
      />
91
    </>
7279 stevensc 92
  )
93
}
94
 
95
export default CalendarPage