Skip to content

Commit

Permalink
fix: move slices to features
Browse files Browse the repository at this point in the history
  • Loading branch information
chanwoo00106 committed Aug 22, 2023
1 parent 7718352 commit fb22e1d
Show file tree
Hide file tree
Showing 22 changed files with 99 additions and 98 deletions.
7 changes: 3 additions & 4 deletions packages/app/src/features/auth/hook/useLogout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import TokenManager from '@lib/TokenManager'
import { useDialog } from '@hooks'
import { useToast } from '@features/toast'
import { useDispatch } from 'react-redux'
import { resetPage } from '@store/studentParamSlice'
import { resetStudents } from '@store/studentListSlice'
import { actions } from '@features/student/stores'
import { useStudent } from '@features/student'
import useLoggedIn from './useLoggedIn'

Expand All @@ -32,8 +31,8 @@ const useLogout = () => {
TokenManager.clearToken()

addToast('success', '로그아웃에 성공했습니다')
dispatch(resetPage())
dispatch(resetStudents())
dispatch(actions.resetPage())
dispatch(actions.resetStudents())
refetchStudents({ page: 1, size: 20 })
router.push('/')
refetchLoggedIn()
Expand Down
7 changes: 3 additions & 4 deletions packages/app/src/features/auth/hook/useWithdraw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { useToast } from '@features/toast'
import { useDialog } from '@hooks'
import TokenManager from '@lib/TokenManager'
import { useDispatch } from 'react-redux'
import { resetPage } from '@store/studentParamSlice'
import { resetStudents } from '@store/studentListSlice'
import { actions } from '@features/student/stores'
import { useStudent } from '@features/student'
import useLoggedIn from './useLoggedIn'

Expand All @@ -31,8 +30,8 @@ const useWithdraw = () => {

TokenManager.clearToken()
addToast('success', '회원탈퇴에 성공했습니다')
dispatch(resetPage())
dispatch(resetStudents())
dispatch(actions.resetPage())
dispatch(actions.resetStudents())
refetchStudents({ page: 1, size: 20 })
router.push('/')
refetchLoggedIn()
Expand Down
6 changes: 3 additions & 3 deletions packages/app/src/features/dialog/molecules/DialogProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Dialog } from '@sms/shared'
import { useDispatch, useSelector } from 'react-redux'
import { RootState } from '@store'
import { setIsShow } from '@store/dialogSlice'
import { actions } from '@features/dialog/stores'
import Portal from '@features/dialog/atoms/Portal'

const DialogProvider = () => {
Expand All @@ -12,7 +12,7 @@ const DialogProvider = () => {

const onClose = () => {
dialog.emitter.emit('dialog', false)
dispatch(setIsShow())
dispatch(actions.setIsShow())
}

return dialog.isShow ? (
Expand All @@ -23,7 +23,7 @@ const DialogProvider = () => {
cancelText={dialog.cancelText || '취소'}
confirmText={dialog.confirmText || '확인'}
emitter={dialog.emitter}
onClose={() => dispatch(setIsShow())}
onClose={() => dispatch(actions.setIsShow())}
/>
</Portal>
) : null
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions packages/app/src/features/dialog/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as dialogActions from './dialogSlice'

export { default as dialogSlice } from './dialogSlice'

export const actions = {
...dialogActions,
}
6 changes: 3 additions & 3 deletions packages/app/src/features/modal/hooks/useModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { close, show } from '@store/modalSlice'
import { actions } from '@features/modal/stores'
import { ReactElement } from 'react'
import { useDispatch } from 'react-redux'
import { useRouter } from 'next/router'
Expand All @@ -9,11 +9,11 @@ const useModal = () => {

const onClose = () => {
router.push('/', '/')
dispatch(close())
dispatch(actions.close())
}

const onShow = (element: ReactElement) => {
dispatch(show(element))
dispatch(actions.show(element))
}

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/features/modal/hooks/usePortal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState, MouseEvent } from 'react'
import { useRouter } from 'next/router'
import { useDispatch } from 'react-redux'
import { close } from '@store/modalSlice'
import { actions } from '@features/modal/stores'

const usePortal = () => {
const router = useRouter()
Expand Down Expand Up @@ -36,7 +36,7 @@ const usePortal = () => {

const onClose = () => {
router.push('/', '/')
dispatch(close())
dispatch(actions.close())
}

return {
Expand Down
7 changes: 7 additions & 0 deletions packages/app/src/features/modal/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as modalActions from './modalSlice'

export const actions = {
...modalActions,
}

export { default as modalSlice } from './modalSlice'
29 changes: 29 additions & 0 deletions packages/app/src/features/modal/stores/modalSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit'
import { ReactElement } from 'react'

interface InitialState {
name: string | null
value: boolean

element: ReactElement | null
props?: object
}

const initialState: InitialState = { name: null, value: false, element: null }

const modalSlice = createSlice({
name: 'modal',
initialState,
reducers: {
show: (state, { payload }: PayloadAction<ReactElement>) => {
state.element = payload
},
close: (state) => {
state.element = null
},
},
})

export const { close, show } = modalSlice.actions

export default modalSlice
15 changes: 7 additions & 8 deletions packages/app/src/features/student/hooks/useStudent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import errors from '@features/student/service/errors'
import { useToast } from '@features/toast'
import ErrorMapper from '@lib/ErrorMapper'
import { RootState } from '@store'
import { addStudents, setTotoalSize } from '@store/studentListSlice'
import { nextStop, setIsError, setLoading } from '@store/studentParamSlice'
import { actions } from '@features/student/stores'
import { useDispatch, useSelector } from 'react-redux'
import studentListApi from '@features/student/service/studentListApi'

Expand All @@ -26,19 +25,19 @@ const useStudent = () => {
const refetchStudents = async (params: StudentsParams) => {
if (studentParam.nextStop) return

dispatch(setLoading(true))
dispatch(actions.setLoading(true))
const { data, isError, error } = await studentListApi({
...params,
})
dispatch(setLoading(false))
dispatch(actions.setLoading(false))

if (isError) {
dispatch(setIsError(isError))
dispatch(actions.setIsError(isError))
return addToast('error', ErrorMapper(error, errors))
}
dispatch(setTotoalSize(data.totalSize))
if (data?.content) dispatch(addStudents(data.content))
if (data.last) return dispatch(nextStop())
dispatch(actions.setTotoalSize(data.totalSize))
if (data?.content) dispatch(actions.addStudents(data.content))
if (data.last) return dispatch(actions.nextStop())
}

return {
Expand Down
9 changes: 4 additions & 5 deletions packages/app/src/features/student/hooks/useStudentsFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { useRouter } from 'next/router'
import { useForm } from 'react-hook-form'
import { useDispatch, useSelector } from 'react-redux'
import { RootState } from '@store'
import { resetPage, setParam } from '@store/studentParamSlice'
import { resetStudents } from '@store/studentListSlice'
import { actions } from '@features/student/stores'
import ParamsFilter from '@lib/ParamsFilter'
import { useModal } from '@features/modal/hooks'
import useStudent from './useStudent'
Expand Down Expand Up @@ -35,9 +34,9 @@ const useStudentsFilter = () => {
})

const onSubmit = handleSubmit(async (form) => {
dispatch(resetPage())
dispatch(resetStudents())
dispatch(setParam(form))
dispatch(actions.resetPage())
dispatch(actions.resetStudents())
dispatch(actions.setParam(form))
refetchStudents({ ...form, size: studentParam.size, page: 1 })

await router.push('/', {
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/features/student/hooks/useStudentsParam.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ParsedUrlQuery } from 'querystring'
import { setParam } from '@store/studentParamSlice'
import { actions } from '@features/student/stores'
import { useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import {
Expand Down Expand Up @@ -40,7 +40,7 @@ const useStudentsParam = ({ query }: Props) => {
} = query

dispatch(
setParam({
actions.setParam({
grade: toNumberArray(grade),
majors: toStringArray(majors),
classNum: toNumberArray(classNum),
Expand Down
10 changes: 10 additions & 0 deletions packages/app/src/features/student/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export { default as studentListSlice } from './studentListSlice'
export { default as studentParamSlice } from './studentParamSlice'

import * as studentListActions from './studentListSlice'
import * as studentParamActions from './studentParamSlice'

export const actions = {
...studentListActions,
...studentParamActions,
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Toast } from '@sms/shared'
import { RootState } from '@store'
import { ToastType } from '@store/toastSlice'
import { ToastType } from '@features/toast/stores/toastSlice'
import { useSelector } from 'react-redux'

import * as S from './style'
Expand Down
8 changes: 4 additions & 4 deletions packages/app/src/features/toast/hooks/useToast.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { addToast, hideToast, removeToast } from '@store/toastSlice'
import { actions } from '@features/toast/stores'
import { useDispatch } from 'react-redux'

const useToast = () => {
const dispatch = useDispatch()

const createToast = (type: 'success' | 'error', comment: string) => {
const id = Symbol('toast')
dispatch(addToast({ id, type, comment, milliseconds: 3000 }))
dispatch(actions.addToast({ id, type, comment, milliseconds: 3000 }))

setTimeout(() => {
dispatch(hideToast(id))
dispatch(actions.hideToast(id))
}, 3000)

setTimeout(() => {
dispatch(removeToast(id))
dispatch(actions.removeToast(id))
}, 3400)
}

Expand Down
7 changes: 7 additions & 0 deletions packages/app/src/features/toast/stores/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export { default as toastSlice } from './toastSlice'

import * as toastActions from './toastSlice'

export const actions = {
...toastActions,
}
File renamed without changes.
6 changes: 3 additions & 3 deletions packages/app/src/hooks/useDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setIsShow, setLogInfo } from '@store/dialogSlice'
import { actions } from '@features/dialog/stores'
import { useDispatch } from 'react-redux'
import { Emitter } from '@sms/shared'
import { SetLogInfoType } from '@/types/store/dialogSlice'
Expand All @@ -12,8 +12,8 @@ const useDialog = () => {
const dialog = (dialogInfo: Omit<SetLogInfoType, 'emitter'>) =>
new Promise((resolve) => {
emitter.on(eventName, (result) => resolve(result))
dispatch(setLogInfo({ ...dialogInfo, emitter }))
dispatch(setIsShow())
dispatch(actions.setLogInfo({ ...dialogInfo, emitter }))
dispatch(actions.setIsShow())
})

return { dialog }
Expand Down
9 changes: 4 additions & 5 deletions packages/app/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { rtkApi } from '@api'
import { AnyAction, combineReducers, configureStore } from '@reduxjs/toolkit'
import { HYDRATE, createWrapper } from 'next-redux-wrapper'
import dialogSlice from './dialogSlice'
import studentParamSlice from './studentParamSlice'
import modalSlice from './modalSlice'
import toastSlice from './toastSlice'
import studentListSlice from './studentListSlice'
import { dialogSlice } from '@features/dialog/stores'
import { modalSlice } from '@features/modal/stores'
import { studentListSlice, studentParamSlice } from '@features/student/stores'
import { toastSlice } from '@features/toast/stores'

const reducers = combineReducers({
dialog: dialogSlice.reducer,
Expand Down
54 changes: 0 additions & 54 deletions packages/app/src/store/modalSlice.ts

This file was deleted.

0 comments on commit fb22e1d

Please sign in to comment.