реализовал функционал Section

This commit is contained in:
dyakonovr 2024-03-26 12:56:05 +04:00
parent 14a7fdbf45
commit 7ccdcd4227
3 changed files with 33 additions and 12 deletions

View File

@ -1,19 +1,28 @@
import { Edit, ReferenceInput, SelectInput, SimpleForm, TextInput, useGetList, useRecordContext } from "react-admin";
import {
Edit,
ReferenceInput,
SelectInput,
SimpleForm,
TextInput,
useGetRecordId,
} from "react-admin";
import Title from "../Title";
function SectionEdit() {
// const record = useRecordContext();
// if (!record) return null;
// const { data } = useGetList("sections", { id: record.id });
const recordId = useGetRecordId();
return (
<Edit title={<Title prefixText="Роль" />} actions={false}>
<SimpleForm>
<TextInput source="name" fullWidth />
{/* <ReferenceInput source="root_section_id" reference="sections"> */}
{/* <SelectInput choices={data} optionText="name" fullWidth /> */}
{/* </ReferenceInput> */}
<ReferenceInput
source="root_section_id"
reference="sections"
filter={{ id: recordId }}
>
<SelectInput optionText="name" fullWidth />
</ReferenceInput>
</SimpleForm>
</Edit>
);

View File

@ -1,7 +1,7 @@
import { Op, type Order } from "sequelize";
import Section from "../../models/section.model";
import { ApiErrorHandler } from "../../error/api-error.handler";
import type { ISectionDto } from "./section.dto";
import type { Order } from "sequelize";
import type { NextFunction, Request, Response } from "express";
import type {
FilterQueryParamType,
@ -12,6 +12,12 @@ import type {
class SectionController {
getAll = async (req: Request, res: Response, next: NextFunction) => {
try {
// Сюда приходит id текущего товара при редактировании,
// чтобы не возвращать его в списке
const filter: FilterQueryParamType = req.query.filter
? JSON.parse(req.query.filter.toString())
: { id: -1 };
const range: RangeQueryParamType = req.query.range
? JSON.parse(req.query.range.toString())
: [0, 10];
@ -22,7 +28,12 @@ class SectionController {
const sections = await Section.findAndCountAll({
offset: range[0],
limit: range[1] - range[0] + 1,
order: [sort] as Order
order: [sort] as Order,
where: {
id: {
[Op.ne]: filter && filter.id ? filter.id : null
}
}
});
return res.json({ data: sections.rows, total: sections.count });
@ -87,12 +98,13 @@ class SectionController {
) => {
try {
const { id } = req.params;
const { name } = req.body;
const { name, root_section_id } = req.body;
const section = await Section.findByPk(id);
if (!section) return next(ApiErrorHandler.internal("Такой роли не найдено"));
section.name = name;
section.root_section_id = root_section_id;
await section.save();
return res.json(section);

View File

@ -41,7 +41,7 @@ export default class Section extends Model {
@Column({
type: DataType.INTEGER
})
declare root_section_id: number;
declare root_section_id: number | null;
@BelongsTo(() => Section, "root_section_id")
declare root_section: Section;