Skip to content

Commit

Permalink
Merge pull request #121 from LamArt/feature/SCHOOL-795/routings-with-…
Browse files Browse the repository at this point in the history
…enums

add routerPath enum and change all router.push
  • Loading branch information
zavarin-michael authored Apr 19, 2024
2 parents aaf5c10 + 062b327 commit 60d419e
Show file tree
Hide file tree
Showing 30 changed files with 161 additions and 109 deletions.
7 changes: 4 additions & 3 deletions apps/schools/domains/circle/components/circleList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import EmptyWrapper from '@domains/common/components/containers/EmptyWrapper'
import { mapReturnedData } from '@domains/common/redux/utils'
import { HighlightText } from '@domains/common/components/table/forming'
import { getVarsForAddressColumn } from '@domains/common/utils/geo'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

export function CircleList() {
const [searchRequestText, setSearchRequestText] = useState('')
Expand All @@ -37,7 +38,7 @@ export function CircleList() {
pageTitle={'Кружки'}
data={circles}
isLoading={isFetching}
handleRunTask={() => router.push('/circle/create')}
handleRunTask={() => router.push(RoutePath[AppRoutes.CIRCLE_CREATE])}
searchTrigger={searchRequestText}
>
<div className={styles.header}>
Expand All @@ -46,7 +47,7 @@ export function CircleList() {
type='schoolDefault'
block
className={styles.button}
onClick={() => router.push('/circle/create')}
onClick={() => router.push(RoutePath[AppRoutes.CIRCLE_CREATE])}
>
Добавить кружок
</Button>
Expand All @@ -59,7 +60,7 @@ export function CircleList() {
]}
data={reformattedData}
isLoading={isFetching}
mainRoute={'/circle'}
mainRoute={RoutePath[AppRoutes.CIRCLE_LIST]}
searchFields={['name', 'address']}
customFields={{
address: ({ text, searchText }) => {
Expand Down
11 changes: 6 additions & 5 deletions apps/schools/domains/circle/components/currentCircle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import styles from './styles/styles.module.scss'
import { getVarsForAddressColumn } from '@domains/common/utils/geo'
import { QueryStatuses } from '@domains/common/constants/Enums'
import { ErrorType } from '@store/commonApi'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

const CurrentCircle = () => {
const [isModalVisible, setIsModalVisible] = useState(false)
Expand Down Expand Up @@ -64,12 +65,12 @@ const CurrentCircle = () => {

useEffect(() => {
if (circleError && (circleError as ErrorType).status == 404) {
router.push('/circle')
router.push(RoutePath[AppRoutes.CIRCLE_LIST])
}
}, [circleError])

if (isDeleteFinished.isSuccess) return null
if (uuid.length === 0) router.push('/404')
if (uuid.length === 0) router.push(RoutePath[AppRoutes.NOT_FOUND])

return (
<>
Expand Down Expand Up @@ -138,7 +139,7 @@ const CurrentCircle = () => {
['Телефон родителя', 'parent_phones'],
]}
data={reformattedData}
mainRoute={'/student'}
mainRoute={RoutePath[AppRoutes.STUDENT_LIST]}
isLoading={isLoading}
needNumbering={true}
searchFields={['student_name', 'student_phone', 'parent_names', 'parent_phones']}
Expand All @@ -151,7 +152,7 @@ const CurrentCircle = () => {
actions={[
<Button
className={styles.changeButton}
onClick={() => router.push(`/circle/${uuid[0]}/change`)}
onClick={() => router.push(`${RoutePath[AppRoutes.CIRCLE_LIST]}/${uuid[0]}/change`)}
>
Редактировать данные кружка
</Button>,
Expand All @@ -174,7 +175,7 @@ const CurrentCircle = () => {
setIsModalVisible={setIsModalVisible}
titleText={'Удалить кружок?'}
buttonText={'Удалить кружок'}
urlAfterDelete={'/circle'}
urlAfterDelete={RoutePath[AppRoutes.CIRCLE_LIST]}
dataField={'circle_id'}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getUuidFromUrl } from '@domains/common/utils/getUuidFromUrl'
import { mapSteps } from '@domains/circle/interfaces/mapStepsType'
import { FormMapSteps } from '@domains/circle/constants/Enums'
import router from 'next/router'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

interface MapComponentProps {
mode: 'Change' | 'Create'
Expand Down Expand Up @@ -175,11 +176,11 @@ const ConfirmMap = (props: MapComponentProps) => {
onClick={() => {
if (mode === 'Create') {
handleSubmitCreateForm(organizationId, mainForm, mutation).then((isSucceed) => {
if (isSucceed) router.push('/circle')
if (isSucceed) router.push(RoutePath[AppRoutes.CIRCLE_LIST])
})
} else if (mode === 'Change') {
handleSubmitUpdateForm(circleId, mainForm, mutation).then((isSucceed) => {
if (isSucceed) router.push(`/circle/${circleId}`)
if (isSucceed) router.push(`${RoutePath[AppRoutes.CIRCLE_LIST]}/${circleId}`)
})
}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Link from 'next/link'
import { Row } from 'antd'
import { CustomLogo } from '@domains/common/components/Logo'
import getConfig from 'next/config'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

const {
publicRuntimeConfig: {
Expand All @@ -16,7 +17,7 @@ export const EmptyLayout: React.FC<any> = (props) => {

return (
<div className={styles.container}>
<Link className={styles.logoContainer} href={'/'}>
<Link className={styles.logoContainer} href={RoutePath[AppRoutes.MAIN]}>
<Row className={styles.rowWithGap}>
<CustomLogo minified={true} />
<div className={styles.logoText}>
Expand Down
37 changes: 37 additions & 0 deletions apps/schools/domains/common/constants/routerEnums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const enum AppRoutes {
AUTH = 'auth',
AUTH_SIGN_IN = 'auth_signin',
AUTH_REGISTER = 'auth_register',
AUTH_FORGOT = 'auth_forgot',
CIRCLE_LIST = 'circle',
CIRCLE_CREATE = 'circle_create',
EMPLOYEE_LIST = 'employee_list',
EMPLOYEE_CREATE = 'employee_create',
MAIN = 'main',
MOBILE_RECAPTCHA = 'mobile_recaptcha',
NOT_FOUND = 'not_found',
QUERY_LIST = 'query_list',
STUDENT_LIST = 'student_list',
STUDENT_CREATE = 'student_create',
USER_LIST = 'user_list',
USER_EDIT = 'user_edit',
}

export const RoutePath: Record<AppRoutes, string> = {
[AppRoutes.AUTH]: '/auth',
[AppRoutes.AUTH_SIGN_IN]: '/auth/signin',
[AppRoutes.AUTH_REGISTER]: '/auth/register',
[AppRoutes.AUTH_FORGOT]: '/auth/forgot',
[AppRoutes.CIRCLE_LIST]: '/circle',
[AppRoutes.CIRCLE_CREATE]: '/circle/create',
[AppRoutes.EMPLOYEE_LIST]: '/employee',
[AppRoutes.EMPLOYEE_CREATE]: '/employee/create',
[AppRoutes.MAIN]: '/',
[AppRoutes.MOBILE_RECAPTCHA]: '/mobile-recaptcha',
[AppRoutes.NOT_FOUND]: '/404',
[AppRoutes.QUERY_LIST]: '/query',
[AppRoutes.STUDENT_LIST]: '/student',
[AppRoutes.STUDENT_CREATE]: '/student/create',
[AppRoutes.USER_LIST]: '/user',
[AppRoutes.USER_EDIT]: '/user/edit',
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useGetEmployeeQuery, useUpdateEmployeeByIdMutation } from '@domains/emp
import { useChangeEmployeeFormValidators } from '@domains/employee/components/changeEmployeeForm/hooks'
import { EMPLOYEE_NAME, EMPLOYEE_POSITION } from '@domains/employee/components/changeEmployeeForm/constants'
import { handleSubmitForm } from '@domains/employee/handlers/employeePatch'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

export function ChangeEmployeeForm() {
const uuid = getUuidFromUrl()
Expand All @@ -32,11 +33,11 @@ export function ChangeEmployeeForm() {

useEffect(() => {
if (employeeError && (employeeError as ErrorType).status == 404) {
router.push('/employee')
router.push(RoutePath[AppRoutes.EMPLOYEE_LIST])
}
}, [employeeError])

if (uuid.length === 0) router.push('/404')
if (uuid.length === 0) router.push(RoutePath[AppRoutes.NOT_FOUND])

const validationCheck = () => {
setIsFormValid(isValidFormCheck(form, [], initialValues))
Expand All @@ -59,7 +60,7 @@ export function ChangeEmployeeForm() {
onValuesChange={validationCheck}
onFinish={() => {
handleSubmitForm(uuid[0], form, mutation).then((isSuccess) => {
if (isSuccess) router.push(`/employee/${uuid[0]}`)
if (isSuccess) router.push(`${RoutePath[AppRoutes.EMPLOYEE_LIST]}/${uuid[0]}`)
})
}}
layout='vertical'
Expand Down Expand Up @@ -95,7 +96,7 @@ export function ChangeEmployeeForm() {
type='schoolDefaultAuto'
antdType={'default'}
block
onClick={() => router.push(`/employee/${uuid[0]}`)}
onClick={() => router.push(`${RoutePath[AppRoutes.EMPLOYEE_LIST]}/${uuid[0]}`)}
>
Отменить
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useCreateEmployeeFormValidators } from './hooks'
import { useInviteEmployeeMutation } from '@domains/organization/redux/organizationApi'
import { useOrganization } from '@domains/organization/providers/organizationProvider'
import { handleSubmitForm } from '@domains/employee/handlers/employeeCreate'
import { useRouter } from 'next/router'
import router, { useRouter } from 'next/router'
import { WithTooltip } from '@domains/common/components/tooltip/withTooltip'
import { TOOLTIP_MARGIN } from '@domains/employee/components/createEmployeeForm/styles/styles'
import { isValidFormCheck } from '@domains/common/utils/form'
Expand All @@ -17,6 +17,7 @@ import {
EMPLOYEE_PHONE,
EMPLOYEE_POSITION,
} from '@domains/employee/components/createEmployeeForm/constants'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

export const CreateEmployeeForm = () => {
const validators = useCreateEmployeeFormValidators()
Expand All @@ -39,7 +40,7 @@ export const CreateEmployeeForm = () => {
onValuesChange={validationCheck}
onFinish={() => {
handleSubmitForm(organizationId, form, mutation).then((isSucceed) => {
if (isSucceed) router.push('/employee')
if (isSucceed) router.push(RoutePath[AppRoutes.EMPLOYEE_LIST])
})
}}
layout='vertical'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Field } from '@domains/common/components/field'
import { ActionBar } from '@domains/common/components/stickyBlock/actionBar'
import DeleteModal from '@domains/common/components/deleteModal'
import { useDeleteEmployeeByIdMutation, useGetEmployeeQuery } from '@domains/employee/redux/employeeApi'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

const CurrentEmployee = () => {
const [isModalVisible, setIsModalVisible] = useState(false)
Expand All @@ -29,12 +30,12 @@ const CurrentEmployee = () => {

useEffect(() => {
if (employeeError && (employeeError as ErrorType).status == 404) {
router.push('/employee')
router.push(RoutePath[AppRoutes.EMPLOYEE_LIST])
}
}, [employeeError])

if (isDeleteFinished.isSuccess) return null
if (uuid.length === 0) router.push('/404')
if (uuid.length === 0) router.push(RoutePath[AppRoutes.NOT_FOUND])

return isLoading ? (
<Spin />
Expand Down Expand Up @@ -70,7 +71,9 @@ const CurrentEmployee = () => {
<Button
key={'edit'}
className={styles.changeButton}
onClick={() => router.push(`/employee/${uuid[0]}/change`)}
onClick={() =>
router.push(`${RoutePath[AppRoutes.EMPLOYEE_LIST]}/${uuid[0]}/change`)
}
>
Редактировать профиль
</Button>,
Expand All @@ -95,7 +98,7 @@ const CurrentEmployee = () => {
setIsModalVisible={setIsModalVisible}
titleText={'Удалить сотрудника?'}
buttonText={'Удалить сотрудника'}
urlAfterDelete={'/employee'}
urlAfterDelete={RoutePath[AppRoutes.EMPLOYEE_LIST]}
dataField={'employee_id'}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createSearchTextForRequest } from '@domains/common/utils/searchText'
import { GetListEmployee } from '@domains/employee/redux/interfaces'
import { RowType } from '@domains/employee/components/employeeList/interfaces'
import { searchColumns } from '@domains/employee/components/employeeList/constants'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

export function EmployeeList() {
const [searchRequestText, setSearchRequestText] = useState('')
Expand All @@ -29,7 +30,7 @@ export function EmployeeList() {
block
style={{ width: '14%' }}
className={styles.button}
onClick={() => router.push('/employee/create')}
onClick={() => router.push(RoutePath[AppRoutes.EMPLOYEE_CREATE])}
>
Добавить сотрудника
</Button>
Expand All @@ -42,7 +43,7 @@ export function EmployeeList() {
]}
data={data}
isLoading={isLoading}
mainRoute={'/employee'}
mainRoute={RoutePath[AppRoutes.EMPLOYEE_LIST]}
searchFields={searchColumns}
searchRequestText={searchRequestText}
setSearchRequestText={setSearchRequestText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRouter } from 'next/router'
import { useGetAllOrganizationsQuery } from '../redux/organizationApi'
import { OrganizationInfo } from '../interfaces/organizationProvider'
import { EventKey, useEventBus } from '@domains/common/providers/eventBusProvider'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

export const UUID_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i

Expand Down Expand Up @@ -65,7 +66,11 @@ export const OrganizationProvider: React.FC<OrganizationProviderProps> = ({ chil
}

if (data.count === 0) {
if (!router.asPath.endsWith('/user') && !router.asPath.includes('/auth/')) router.push('/user')
if (
!router.asPath.endsWith(RoutePath[AppRoutes.USER_LIST]) &&
!router.asPath.includes(`${RoutePath[AppRoutes.AUTH]}/`)
)
router.push(RoutePath[AppRoutes.USER_LIST])
} else {
const firstOrganization = data?.results[0]
if (organizationId === '' && firstOrganization && firstOrganization.id) {
Expand Down
3 changes: 2 additions & 1 deletion apps/schools/domains/query/components/queryList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { BubbleFilter } from '@domains/common/components/bubbleFilter'
import { BubbleFilterListItem } from '@domains/common/components/bubbleFilter/interface'
import { useQueryState } from 'next-usequerystate'
import { parseAsArrayOf, parseAsString } from 'next-usequerystate'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

export function QueryList() {
const [searchRequestText, setSearchRequestText] = useState('')
Expand Down Expand Up @@ -144,7 +145,7 @@ export function QueryList() {
]}
data={reformattedData}
isLoading={isQueriesLoading}
mainRoute={'/query'}
mainRoute={RoutePath[AppRoutes.QUERY_LIST]}
searchFields={[
'created_at',
'student_name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { handleSubmitForm } from '@domains/student/handlers/studentPatch'
import { useUpdateStudentByIdMutation } from '@domains/student/redux/studentApi'
import { useChangeStudentFormValidators } from '@domains/student/components/changeStudentForm/hooks'
import { ErrorType } from '@store/commonApi'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

export function ChangeStudentForm() {
const uuid = getUuidFromUrl()
Expand All @@ -31,11 +32,11 @@ export function ChangeStudentForm() {

useEffect(() => {
if (studentError && (studentError as ErrorType).status == 404) {
router.push('/student')
router.push(RoutePath[AppRoutes.STUDENT_LIST])
}
}, [studentError])

if (uuid.length === 0) router.push('/404')
if (uuid.length === 0) router.push(RoutePath[AppRoutes.NOT_FOUND])

const validationCheck = () => {
setIsFormValid(isValidFormCheck(form, [], initialValues))
Expand All @@ -58,7 +59,7 @@ export function ChangeStudentForm() {
onValuesChange={validationCheck}
onFinish={() => {
handleSubmitForm(uuid[0], form, mutation).then((isSuccess) => {
if (isSuccess) router.push(`/student/${uuid[0]}`)
if (isSuccess) router.push(`${RoutePath[AppRoutes.STUDENT_LIST]}/${uuid[0]}`)
})
}}
layout='vertical'
Expand All @@ -83,7 +84,7 @@ export function ChangeStudentForm() {
type='schoolDefaultAuto'
antdType={'default'}
block
onClick={() => router.push(`/student/${uuid[0]}`)}
onClick={() => router.push(`${RoutePath[AppRoutes.STUDENT_LIST]}/${uuid[0]}`)}
>
Отменить
</Button>
Expand Down
Loading

0 comments on commit 60d419e

Please sign in to comment.