generated from 8iq/nodejs-hackathon-boilerplate-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added change and delete buttons to student page +
page for changing student name + fixes in common components
- Loading branch information
1 parent
f93689d
commit 9563762
Showing
15 changed files
with
271 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
} | ||
|
||
.deleteButton { | ||
width: 160px; | ||
width: auto; | ||
padding: 7px; | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
apps/schools/domains/student/components/changeStudentForm/constants.ts
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const STUDENT_NAME = 'student_name' |
21 changes: 21 additions & 0 deletions
21
apps/schools/domains/student/components/changeStudentForm/hooks.ts
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useMemo } from 'react' | ||
import { ValidatorsMap } from '@domains/common/redux/interfaces' | ||
import { PleaseInputStudentNameMsg } from '@domains/user/components/auth/constants/message' | ||
import { getGreaterValidator } from '@domains/common/utils/validators' | ||
import { STUDENT_NAME } from '@domains/student/components/changeStudentForm/constants' | ||
|
||
export const useChangeStudentFormValidators = () => { | ||
return useMemo<ValidatorsMap>(() => { | ||
return { | ||
[STUDENT_NAME]: [ | ||
{ | ||
required: false, | ||
message: PleaseInputStudentNameMsg, | ||
whitespace: true, | ||
type: 'string', | ||
}, | ||
getGreaterValidator(200), | ||
], | ||
} | ||
}, [this]) | ||
} |
98 changes: 98 additions & 0 deletions
98
apps/schools/domains/student/components/changeStudentForm/index.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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import router from 'next/router' | ||
import { Col, Form, Row, Spin, Typography } from 'antd' | ||
import { Button } from '@domains/common/components/button' | ||
import styles from './styles/styles.module.scss' | ||
import Image from 'next/image' | ||
import duckEmptyPage from '@public/image/duckEmptyPage.svg' | ||
|
||
import { isValidFormCheck } from '@domains/common/utils/form' | ||
import { Input } from '@domains/common/components/input' | ||
|
||
import { useGetStudentQuery } from '@domains/organization/redux/organizationApi' | ||
import { STUDENT_NAME } from '../createStudentForm/constants' | ||
import { getUuidFromUrl } from '@domains/common/utils/getUuidFromUrl' | ||
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' | ||
|
||
export function ChangeStudentForm() { | ||
const uuid = getUuidFromUrl() | ||
const { data: student, error: studentError, isFetching } = useGetStudentQuery({ student_id: uuid[0] }) | ||
const [form] = Form.useForm() | ||
const [isFormValid, setIsFormValid] = useState(false) | ||
const validators = useChangeStudentFormValidators() | ||
const [mutation] = useUpdateStudentByIdMutation() | ||
|
||
const initialValues = { | ||
[STUDENT_NAME]: student?.student.name ?? '', | ||
} | ||
|
||
useEffect(() => { | ||
if (studentError && (studentError as ErrorType).status == 404) { | ||
router.push('/student') | ||
} | ||
}, [studentError]) | ||
|
||
if (uuid.length === 0) router.push('/404') | ||
|
||
const validationCheck = () => { | ||
setIsFormValid(isValidFormCheck(form, [], initialValues)) | ||
} | ||
|
||
return ( | ||
<Row className={styles.baseRowContainer}> | ||
<Image src={duckEmptyPage} alt={'Duck with a magnifying glass'} width={190} /> | ||
<Col className={styles.infoContainer}> | ||
<Typography.Title className={styles.title} level={1}> | ||
Редактирование обучающегося | ||
</Typography.Title> | ||
|
||
{!isFetching ? ( | ||
<Form | ||
form={form} | ||
className={styles.table} | ||
colon={false} | ||
requiredMark={false} | ||
onValuesChange={validationCheck} | ||
onFinish={() => { | ||
handleSubmitForm(uuid[0], form, mutation).then((isSuccess) => { | ||
if (isSuccess) router.push(`/student/${uuid[0]}`) | ||
}) | ||
}} | ||
layout='vertical' | ||
> | ||
<Form.Item | ||
required={true} | ||
label={'Ф. И. О.'} | ||
name={STUDENT_NAME} | ||
rules={validators[STUDENT_NAME]} | ||
className={styles.label} | ||
initialValue={initialValues[STUDENT_NAME]} | ||
> | ||
<Input required={true} placeholder='Введите имя' /> | ||
</Form.Item> | ||
|
||
<Row className={styles.buttonsContainer}> | ||
<Button type='schoolDefaultAuto' htmlType='submit' disabled={!isFormValid} block> | ||
Сохранить изменения | ||
</Button> | ||
|
||
<Button | ||
type='schoolDefaultAuto' | ||
antdType={'default'} | ||
block | ||
onClick={() => router.push(`/student/${uuid[0]}`)} | ||
> | ||
Отменить | ||
</Button> | ||
</Row> | ||
</Form> | ||
) : ( | ||
<Spin /> | ||
)} | ||
</Col> | ||
</Row> | ||
) | ||
} |
20 changes: 20 additions & 0 deletions
20
apps/schools/domains/student/components/changeStudentForm/styles/styles.module.scss
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.baseRowContainer { | ||
padding-bottom: 50px; | ||
|
||
.infoContainer { | ||
margin-left: 70px; | ||
max-width: 600px; | ||
|
||
.title { | ||
margin-bottom: 50px; | ||
} | ||
|
||
.itemContainer { | ||
margin-bottom: 30px; | ||
} | ||
|
||
.buttonsContainer { | ||
gap: 20px; | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { FormInstance, message } from 'antd' | ||
import { LoadingRequestMsg, SuccessUpdateStudentMsg } from '@domains/user/components/auth/constants/message' | ||
import { removeEmpty } from '@domains/common/utils/form' | ||
import { withLoadingMessage } from '@domains/common/utils/loading' | ||
import { STUDENT_NAME } from '@domains/student/components/changeStudentForm/constants' | ||
|
||
export async function handleSubmitForm(studentId: string, form: FormInstance, mutation: any) { | ||
const isSuccess = await withLoadingMessage( | ||
LoadingRequestMsg, | ||
async () => { | ||
let response = await mutation( | ||
removeEmpty({ | ||
student_id: studentId, | ||
name: form.getFieldValue(STUDENT_NAME), | ||
}), | ||
) | ||
return 'data' in response | ||
}, | ||
[], | ||
) | ||
|
||
if (isSuccess) { | ||
message.success(SuccessUpdateStudentMsg) | ||
} | ||
|
||
return isSuccess | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface DeleteStudentData { | ||
student_id: string | ||
} | ||
|
||
export interface UpdateStudentData { | ||
student_id?: string | ||
name?: string | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { commonApi } from '@store/commonApi' | ||
import { DeleteStudentData, UpdateStudentData } from './interfaces' | ||
import { GetStudent } from '@domains/common/redux/serializers' | ||
|
||
const organizationApi = commonApi.injectEndpoints({ | ||
endpoints: (build) => ({ | ||
deleteStudent: build.mutation<{}, DeleteStudentData>({ | ||
query: (data) => ({ | ||
url: `/students-management/students/${data.student_id}`, | ||
method: 'DELETE', | ||
}), | ||
invalidatesTags: [{ type: 'Student', id: 'LIST' }], | ||
}), | ||
updateStudentById: build.mutation<{ student: GetStudent }, UpdateStudentData>({ | ||
query: (data) => ({ | ||
url: `/students-management/students/${data.student_id}`, | ||
method: 'PATCH', | ||
body: data, | ||
}), | ||
invalidatesTags: (result, error, arg) => [ | ||
{ type: 'Student', id: 'LIST' }, | ||
{ type: 'Student', id: arg.student_id }, | ||
], | ||
}), | ||
}), | ||
}) | ||
|
||
export const { useDeleteStudentMutation, useUpdateStudentByIdMutation } = organizationApi |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react' | ||
import Head from 'next/head' | ||
import { PageContent } from '@domains/common/components/containers/PageContent' | ||
import { OrganizationRequired } from '@domains/common/components/containers/OrganizationRequired' | ||
import { ChangeStudentForm } from '@domains/student/components/changeStudentForm' | ||
|
||
export const Change = () => { | ||
return ( | ||
<> | ||
<Head> | ||
<title>Редактирование обучающегося</title> | ||
</Head> | ||
<PageContent> | ||
<OrganizationRequired> | ||
<ChangeStudentForm /> | ||
</OrganizationRequired> | ||
</PageContent> | ||
</> | ||
) | ||
} | ||
|
||
export default Change |