๋ฐ˜์‘ํ˜•
250x250
Recent Posts
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Hello creators ๐Ÿ™Œ

240813 ์ผ์ผ ๊ฐœ๋ฐœ ์ผ์ง€ ๋ณธ๋ฌธ

[ํ”„๋ก ํŠธ์—”๋“œ(FE) ๊ฐœ๋ฐœ] (feat. ์ฃผ๋‹ˆ์–ด)/TIL

240813 ์ผ์ผ ๊ฐœ๋ฐœ ์ผ์ง€

๋ถ€์‹œ๋งค๋‚˜_HA 2024. 8. 13. 20:53
728x90
๋ฐ˜์‘ํ˜•

done ํ•œ task ๊ธฐ๋ก

[240813] ์บ˜๋ฆฐ๋” ๊ฐ€์ ธ์˜ค๋Š” ๊ตฌ์กฐ

    1. ์‚ฌ์šฉ์ž๊ฐ€ ์ €์žฅํ•˜๋ฉด -> DB ๋ฅผ ๊ฑฐ์ณค๋‹ค๊ฐ€ -> ๊ทธ๋ ค์ง€๊ฒŒ ํ•  ๊ฒƒ ์ธ๊ฐ€ 
    2. ๊ตฌ๊ธ€ ์บ˜๋ฆฐ๋”์— ์ €์žฅ๋œ ๊ฒƒ์„ ๊ฐ€์ ธ์™€์„œ -> ๊ทธ๋ ค์ง€๊ฒŒ ํ•  ๊ฒƒ ์ธ๊ฐ€ 
  • ์ด ์ค‘ 2๊ฐ€์ง€๋ฅผ ๋‹ค ํ•œ๋‹ค.
  • ์‚ฌ์‹ค, full calendar ์ž์ฒด์— 'ํœด์ผ' ์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ธฐ๋Šฅ์ด ์žˆ์œผ๋ฉด, ๊ตณ์ด google api ์—ฐ๋™์„ ์•ˆ ํ•ด๋„ ๋œ๋‹ค.

[240813-1340] ๋”๋ฏธdate ์— event ์ƒ๊ธฐ๊ฒŒ ํ•˜๊ธฐ

  • ์ฐธ๊ณ 
  • https://bit.ly/3YGzr81 views\fullcalendar.ejs
  • ํฌ์ธํŠธ
    ```bash
  1. ์ผ์ • ์ƒ์„ฑ ๋ฒ„ํŠผ ํด๋ฆญ -> addEvent(calendarRef) ์ด ์‹คํ–‰๋จ
  2. ๋งŒ๋“ค์–ด๋†“๊ณ  -> ๋ชจ๋“ˆ๋กœ ๋นผ๋‹ˆ๊นŒ ์ข‹์Œ โญโญโญโญโญโญโญโญโญ
    • ์ด๊ฑฐ๋ฅผ ์ด์ œ ํ…Œ์ŠคํŠธ ํŒŒ์ผ์—์„œ ๋นผ์„œ -> ํ”„๋กœ์ ํŠธ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์Œ.

<br>

- addEvent ๋‚ด๋ถ€ ๊ตฌํ˜„
```jsx
/* 1. const calendarApi = calendarRef?.current?.getApi();
    - calendarRef ์ด๊ฒŒ ์ž‘๋™ํ•˜๋ ค๋ฉด, FullCalendar ์˜ ref ์†์„ฑ์— ์—ฐ๊ฒฐํ•ด์„œ, addEvent ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” 'โญ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธโญ' ์—์„œ 'FullCalenar' ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ ธ์™€์•ผ ํ•œ๋‹ค. โญโญโญโญโญ 
        - ์ง€๊ธˆ ์˜ˆ์‹œ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ์—๋Š” 'calendar.js' ์—์„œ!!! 
    - ์ด ๋ถ€๋ถ„์—์„œ ์ด๋ก ์ ์œผ๋กœ ๋ถ€์กฑํ–ˆ๋‹ค. 
*/
    <FullCalendar
        ref={calendarRef} // ์ด ๋ถ€๋ถ„ ์ถ”๊ฐ€ โœ…โœ…
        plugins={[
        resourceTimelinePlugin,
        dayGridPlugin,
        interactionPlugin,
        timeGridPlugin,
    ]}

/* 2. ๊ทธ๋ž˜์„œ, full calendar ๋‚ด๋ถ€์— ์กด์žฌํ•˜๋Š” addEvent ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. โญโญโญ  
    - ์ด addEvent ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด, useRef ๊ฐ€ ํ•„์ˆ˜์ ์ด๋‹ค. 
*/

 

  • ์ „์ฒด ์˜ˆ์‹œ ์ฝ”๋“œ
  • export function addEvent(calendarRef) { const calendarApi = calendarRef?.current?.getApi(); const dateString = '2024-08-11'; const testDate = new Date(dateString + 'T00:00:00'); calendarApi?.addEvent({ title: 'ํ…Œ์ŠคํŠธ ์ž…๋‹ˆ๋‹คโœ…โœ…', start: testDate, allday: true, color: 'blue', }); }

<br>


### [240813-1340] 'calendar์˜ ํŠน์ •์ผ์ž'๋ฅผ 'ํด๋ฆญ'ํ•˜๋ฉด, event ๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ํ•˜๊ธฐ 

``` jsx 
/* 1. 'calendar์˜ ํŠน์ •์ผ์ž'๋ฅผ 'ํด๋ฆญ'ํ•˜๋ฉด ์„ ์–ด๋–ป๊ฒŒ ์•Œ์•„? 
    - Full Calendar ๊ฐ€ ์ด๋ฏธ ๋งŒ๋“ค์–ด๋‘์—ˆ์„ ๊ฒƒ. 
    - ์ฐพ์•„๋ณด๋‹ˆ, dateClick ์†์„ฑ์ด ์žˆ์Œ 
        -> ํ•ด๋‹น ์†์„ฑ์— ๋ฐ”์ธ๋”ฉ๋˜๋Š” ํ•จ์ˆ˜(handleDateClick) ๋ฅผ ๋„ฃ์„ ์˜ˆ์ •. 
        (์ถœ์ฒ˜: https://fullcalendar.io/docs/dateClick)
*/
  const handleDateClick = (clickedDate) => {
    const {date} = clickedDate
    addEvent(calendarRef, date)
    debugger
  }

/* 2. clickedDate ๊ฐ์ฒด ๋‚ด๋ถ€๋ฅผ ์—ด์–ด๋ณด๋‹ˆโญโญโญ, date ์†์„ฑ์ด ๋‚ ์งœ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์Œ
    - ๊ทธ๋ž˜์„œ, const {date} = clickedDate ์ด๋ ‡๊ฒŒ ๊นŒ๋ณด๊ฒŒ ๋จ
    - ์ด๊ฑธ ์œ„์—์„œ ๋งŒ๋“  addEvent ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๋ฉด ๋จ
*/
  • ์ „์ฒด ์ฝ”๋“œ
    // Context๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ปค์Šคํ…€ ํ›… โญโญโญโญโญโญโญโญ
    export function useModal() {
    return useContext(ModalContext)
    }
    

export default function CalendarPage() {
const calendarRef = useRef(null) // ์บ˜๋ฆฐ๋” ์ฐธ์กฐ

const [isModalOpen, setIsModalOpen] = useState(false)

const handleDateClick = (clickedDate) => {
const {date} = clickedDate
addEvent(calendarRef, date)
debugger
}

return (
<ModalContext.Provider value={{ isModalOpen, setIsModalOpen }}>


<FullCalendar
ref={calendarRef} // ์ด ๋ถ€๋ถ„ ์ถ”๊ฐ€ โœ…โœ…
plugins={[
resourceTimelinePlugin,
dayGridPlugin,
interactionPlugin,
timeGridPlugin,
]}
// ์ปค์Šคํ…€ ๋ฒ„ํŠผ ์ถ”๊ฐ€
customButtons={{
createEventButton: {
text: '์ผ์ • ์ƒ์„ฑ',
click: function () {
addEvent(calendarRef)
// setIsModalOpen(true)
},
},
}}
headerToolbar={{
left: 'prev,next today createEventButton',
center: 'title',
right: 'resourceTimelineWeek,dayGridMonth,timeGridWeek',
}}
initialView='resourceTimelineWeek'
nowIndicator={true}
editable={true}
selectable={true}
selectMirror={true}
resources={[
{ id: 'a', title: 'Auditorium A' },
{ id: 'b', title: 'Auditorium B', eventColor: 'green' },
{ id: 'c', title: 'Auditorium C', eventColor: 'orange' },
]}
initialEvents={[
{ title: 'nice event', start: new Date(), resourceId: 'a' },
]}
dateClick={handleDateClick} // ์บ˜๋ฆฐ๋”์˜ ๋‚ ์งœ ํด๋ฆญ์‹œ 'handleDateClick' ์‹คํ–‰
/>
    {isModalOpen ? <Modal setIsModalOpen={setIsModalOpen} /> : <></>}
  </Layout>
</ModalContext.Provider>

)
}


<br>


### [240813-1400] '์ƒ์„ฑ๋ฒ„ํŠผ' ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ, event ๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ํ•˜๊ธฐ 

- ๋ณ€๊ฒฝ๋œ ์ƒ๊ฐ 
``` bash
1. ๋‚ ์งœ๋ฅผ ํด๋ฆญํ•˜๋ฉด, event ๋ฅผ ์ƒ์„ฑํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, '์ƒ์„ฑ ๋ฒ„ํŠผ' ์„ ํด๋ฆญํ•˜๋ฉด, event๊ฐ€ ์ƒ์„ฑ๋˜๊ฒŒ ํ•˜๋Š”๊ฒŒ, ์ข€ ๋” ์šฐ์„ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•จ 
    - ์™œ๋ƒ๋ฉด, ๋‹ฌ๋ ฅ์„ ๋’ค๋กœ ๋„˜๊ธฐ๋ ค๋ฉด, ์‚ฌ์‹ค ๊ท€์ฐฎ๋‹ค. 
    - ์ถ”ํ›„์—, ๋‹ฌ๋ ฅํด๋ฆญํ–ˆ์„ ๋•Œ -> ์ด๋ฒคํŠธ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ํ•˜๋Š”๊ฑธ ๋ฆฌํŒฉํ† ๋ง ํ•˜์ž
    -> ๋” ์ด์ƒ ์‹ ๊ฒฝ์“ฐ์ง€ ๋ง๊ณ  ๋„˜์–ด๊ฐ€์ž โญโญโญ | ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ƒ๊ฐ โญโญโญ 

 

// 1. Modal ์ด ์—ด๋ฆด ๋•Œ, calendarRef ์ „๋‹ฌ ํ•ด์•ผ ํ•˜๋Š”๋ฐ, 
   {isModalOpen ? (
          <Modal calendarRef={calendarRef} setIsModalOpen={setIsModalOpen} />
        ) : (
          <></>
    )}

// 2. ๋ฌธ์ œ๊ฐ€ ๋˜์—ˆ๋˜ ๊ฑด, onSubmit ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์ด 'ํด๋ฆญ' ์ด ์•„๋‹ˆ๋ผ, 'Modal ์ด ์‹คํ–‰' ๋  ๋•Œ ์˜€์Œ. 
    // ์ด๊ฑด, onClick={onSubmit(calendarRef)} ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•ด์„œ ์˜€์Œ
    <button type='submit'  onClick={()=>onSubmit(calendarRef)}> ์ œ์ถœ </button>

 

[240813-1820] google calendar ์—์„œ 'ํœด์ผ' ๊ฐ€์ ธ์˜ค๊ธฐ

  1. google api key ๋ฐœ๊ธ‰๋ฐ›๊ธฐ
    - ์ฐธ๊ณ  : https://idealist.tistory.com/92 โญโญโญ
    - ๋ฐœ๊ธ‰๋ฐ›์€ api key : AIzaSyDOp-C30NyBcZVgTUER4WYZp4zBLwAPC1s
  1. [๋ฐœ๊ธ‰๊ณผ์ •์—์„œ ๋ธ”๋กœ๊ทธ์™€ ๋‹ค๋ฅด๊ฒŒ ํ–ˆ๋˜ ๊ฒƒ] api key ๋ฐœ๊ธ‰ ๋ฐ›๋Š” ๊ฒƒ.
  2. [๋ธ”๋กœ๊ทธ์— ์—†์—ˆ๊ณ , ์‹ค์ œ ๊ฐœ๋ฐœํ•  ๋•Œ๋Š” ๋ณด์™„ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ] O Auth ์„ค์ •ํ•˜๋Š” ๊ฒƒ

 

[240813-1900] calendar ์—์„œ ๋ณด์ด๋Š” ํ™”๋ฉด์„, month ๋กœ ์กฐ์ ˆ

  • ์• ์ดˆ์˜ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Œ
      headerToolbar={{
      left: 'prev,next today createEventButton',
      center: 'title',
      right: 'resourceTimelineWeek,dayGridMonth,timeGridWeek',
      }}

 

  • ์ด๊ฑธ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •
    headerToolbar={{
      left: 'prev,next today createEventButton',
      center: 'title',
      right: 'dayGridMonth,timeGridWeek',
    }}

[240813] DB ์—์„œ ๋ฐ›์€ ์ผ์ •์„ ๊ทธ๋ ค์ฃผ๊ธฐ

  1. ๋ฐ์ดํ„ฐ๋ฅผ fetchedData ๋ณ€์ˆ˜์— ์ €์žฅ
    const fetchedData = [ 
     { title: 'test event - 1', start: '2024-08-12', resourceId: 'a' , color: 'blue'},
     { title: 'test event - 2', start: '2024-08-11', resourceId: 'a' , color: 'yellow'},
     { title: 'test event - 3', start: '2024-08-11', end : '2024-08-16' , resourceId: 'a' , color: 'green'},
    ]

 

  1. events={combinedEvents} ์ด๋ ‡๊ฒŒ events ์†์„ฑ์— ๋ฐ”์ธ๋”ฉ ๋  ์ˆ˜ ์žˆ๊ฒŒ ๋ฐฐ์—ด๋กœ ์ค€๋น„
    const combinedEvents = [
     ...fetchedData,
     ...googleCalendarEvents,
    ]

 

  1. ์ด๋ ‡๊ฒŒ events ์†์„ฑ์—, ๊ฐ์ฒด ์š”์†Œ๋กœ ๋œ ๋ฐฐ์—ด ๋กœ ๊ฝ‚์•„์ฃผ๋ฉด, ๊ทธ๋ ค์ง„๋‹ค.

Image

์ œ๋Œ€๋กœ ํ•™์Šต์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„

1. ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž ํ•™์Šต โญโญโญ

  const fetchedData = [ 
    { title: 'nice event', start: '2024-08-12', resourceId: 'a' , color: 'blue'},
    { title: 'nice event', start: '2024-08-11', resourceId: 'a' , color: 'yellow'},
    { title: 'nice event', start: '2024-08-11', end : '2024-08-16' , resourceId: 'a' , color: 'green'},
  ]

  const googleCalendarEvents = [
    {
      googleCalendarId : 'ko.south_korea#holiday@group.v.calendar.google.com', 
      className: 'holiday',
    }
  ]

  // ๐Ÿ“›๐Ÿ“›๐Ÿ“› ์ด๊ฒŒ ํ˜„์žฌ ์™œ ์ž‘๋™?
  const combinedEvents = [
    ...fetchedData,
    ...googleCalendarEvents,
  ]
<FullCalendar
            ref={calendarRef} // ์ด ๋ถ€๋ถ„ ์ถ”๊ฐ€ โœ…โœ…
            plugins={[
              resourceTimelinePlugin,
              dayGridPlugin,
              interactionPlugin,
              timeGridPlugin,
              googleCalendarPlugin, 
            ]}
            // ์ปค์Šคํ…€ ๋ฒ„ํŠผ ์ถ”๊ฐ€
            customButtons={{
              createEventButton: {
                text: '์ผ์ • ์ƒ์„ฑ',
                click: function () {
                  // addEvent(calendarRef)
                  setIsModalOpen(true)
                },
              },
            }}
            headerToolbar={{
              left: 'prev,next today createEventButton',
              center: 'title',
              right: 'resourceTimelineWeek,dayGridMonth,timeGridWeek',
            }}
            initialView='resourceTimelineWeek'
            nowIndicator={true}
            editable={true}
            selectable={true}
            selectMirror={true}
            resources={[
              { id: 'a', title: 'Auditorium A' },
              { id: 'b', title: 'Auditorium B', eventColor: 'green' },
              { id: 'c', title: 'Auditorium C', eventColor: 'orange' },
            ]}

 

2. ๋ถ€๋ชจ์—์„œ ์ž์‹์—๊ฒŒ useState ๋กœ์จ 'set ํ•จ์ˆ˜' ๋ฅผ ๋‚ด๋ ค๋ณด๋‚ด์ค€๋‹ค. ํ•˜์ง€๋งŒ, ์ด ๋ฐฉ์‹์€ flux ํŒจํ„ด์— ๋ฐ˜ํ•œ๋‹ค๊ณ  ํ–ˆ๋‹ค. ๊ทธ ๋Œ€์•ˆ์œผ๋กœ์จ context api ๊ฐ€ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๊ฑด๊ฐ€?

  • ํ˜„์žฌ gpt ์˜ ์˜๊ฒฌ์„ ๋“ฃ๊ณ  ์ž‘์„ฑํ•œ ๊ฑด, context api ๋ฅผ ๊ทธ๋ ‡๊ฒŒ ์“ด๊ฑด๋ฐ, ์ด ๋ถ€๋ถ„์€ context api ์— ๋Œ€ํ•œ ๊ณต๋ถ€๊ฐ€ ํ•„์š”

 

๋ฆฌํŒฉํ† ๋ง ํ•  ๊ฒƒ ๋“ค

1. event ์ƒ์„ฑ '๋ฒ„ํŠผ' ์ด ์•„๋‹ˆ๋ผ, '๋‹ฌ๋ ฅ' ์„ ๋ˆŒ๋ €์„ ๋•Œ๋„, ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ

728x90
๋ฐ˜์‘ํ˜•
Comments