-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(time): apply useModalAction & useModalState hook at TimeTabl…
…e components (#97)
- Loading branch information
Showing
5 changed files
with
185 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
import { memo } from 'react'; | ||
|
||
import { TimeTableFilter } from '@/widgets/time-table'; | ||
|
||
function TimeTableHeader() { | ||
export default function TimeTableHeader() { | ||
return ( | ||
<div className="flex"> | ||
<TimeTableFilter /> | ||
</div> | ||
); | ||
} | ||
|
||
export default memo(TimeTableHeader); | ||
|
||
TimeTableHeader.displayName = 'TimeTableHeader'; |
111 changes: 64 additions & 47 deletions
111
apps/time/src/widgets/time-table/ui/TimeTableLayout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,102 @@ | ||
'use client'; | ||
|
||
import { createContext, useCallback, useState } from 'react'; | ||
import { | ||
createContext, | ||
useCallback, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
import { DAY_VALUE_ARRAY } from '@/shared/constants'; | ||
import { DayKor } from '@/shared/types'; | ||
import { | ||
DayPeriod, | ||
DayStatus, | ||
NightPeriod, | ||
TimeTable, | ||
TimeTableHeader, | ||
TimeTableModal, | ||
useTimeTableParams, | ||
} from '@/widgets/time-table'; | ||
import { SearchParamsActionType } from '@/widgets/time-table/model/hooks/useTimeTableParams'; | ||
|
||
interface TimeTableContextType { | ||
state: { | ||
dayStatus: DayStatus; | ||
period: string; | ||
day: DayKor | ''; | ||
modalVisible: boolean; | ||
}; | ||
action: { | ||
searchParamsAction: SearchParamsActionType; | ||
buttonClickAction: ({ | ||
period, | ||
idx, | ||
}: { | ||
period: string; | ||
idx: number; | ||
}) => void; | ||
modalCloseAction: () => void; | ||
}; | ||
interface TimeTableStateContextType { | ||
dayStatus: DayStatus; | ||
period: DayPeriod | NightPeriod; | ||
day: DayKor | ''; | ||
} | ||
|
||
interface TimeTableActionContextType { | ||
searchParamsAction: SearchParamsActionType; | ||
buttonClickAction: ({ | ||
period, | ||
idx, | ||
}: { | ||
period: DayPeriod | NightPeriod; | ||
idx: number; | ||
}) => void; | ||
} | ||
|
||
export const TimeTableContext = createContext<TimeTableContextType>({ | ||
state: { dayStatus: 'day', period: '', day: '', modalVisible: false }, | ||
action: { | ||
export const TimeTableStateContext = createContext<TimeTableStateContextType>({ | ||
dayStatus: 'day', | ||
period: '' as DayPeriod | NightPeriod, | ||
day: '', | ||
}); | ||
|
||
export const TimeTableActionContext = createContext<TimeTableActionContextType>( | ||
{ | ||
searchParamsAction: {} as SearchParamsActionType, | ||
buttonClickAction: () => {}, | ||
modalCloseAction: () => {}, | ||
}, | ||
}); | ||
); | ||
|
||
export default function TimeTableLayout() { | ||
const { dayStatus, searchParamsAction } = useTimeTableParams(); | ||
const [visible, setVisible] = useState<boolean>(false); | ||
const [selectedDay, setSelectedDay] = useState<DayKor>(); | ||
const [selectedPeriod, setSelectedPeriod] = useState<string>(); | ||
const [selectedPeriod, setSelectedPeriod] = useState< | ||
DayPeriod | NightPeriod | ||
>(); | ||
const [defaultStateContextValue, setDefaultStateContextValue] = | ||
useState<TimeTableStateContextType>({ | ||
dayStatus, | ||
period: '' as DayPeriod | NightPeriod, | ||
day: '', | ||
}); | ||
|
||
const buttonClickAction = useCallback( | ||
({ period, idx }: { period: string; idx: number }) => { | ||
({ period, idx }: { period: DayPeriod | NightPeriod; idx: number }) => { | ||
setSelectedDay(DAY_VALUE_ARRAY[idx]); | ||
setSelectedPeriod(period); | ||
setVisible(true); | ||
}, | ||
[setSelectedDay, setSelectedPeriod, setVisible], | ||
[], | ||
); | ||
|
||
const modalCloseAction = useCallback(() => { | ||
setSelectedDay(undefined); | ||
setSelectedPeriod(undefined); | ||
setVisible(false); | ||
}, [setVisible]); | ||
|
||
const defaultContextValue: TimeTableContextType = { | ||
state: { | ||
useEffect(() => { | ||
setDefaultStateContextValue({ | ||
dayStatus, | ||
period: selectedPeriod ?? '', | ||
period: selectedPeriod!, | ||
day: selectedDay ?? '', | ||
modalVisible: visible, | ||
}, | ||
action: { searchParamsAction, buttonClickAction, modalCloseAction }, | ||
}; | ||
}); | ||
}, [dayStatus, selectedPeriod, selectedDay]); | ||
|
||
const defaultActionContextValue: TimeTableActionContextType = useMemo( | ||
() => ({ | ||
searchParamsAction, | ||
buttonClickAction, | ||
}), | ||
[searchParamsAction, buttonClickAction], | ||
); | ||
|
||
return ( | ||
<div className="flex flex-col items-center justify-center gap-y-8 p-4"> | ||
<TimeTableContext.Provider value={defaultContextValue}> | ||
<TimeTableHeader /> | ||
<TimeTable /> | ||
{visible && <TimeTableModal />} | ||
</TimeTableContext.Provider> | ||
<TimeTableStateContext.Provider value={defaultStateContextValue}> | ||
<TimeTableActionContext.Provider value={defaultActionContextValue}> | ||
<TimeTableHeader /> | ||
<TimeTable /> | ||
<TimeTableModal /> | ||
</TimeTableActionContext.Provider> | ||
</TimeTableStateContext.Provider> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.