1+ import { useState , useEffect } from "react" ;
2+ import { useUnit , useStoreMap } from "effector-react" ;
3+ import { useParams , useNavigate } from "react-router-dom" ;
4+ import { formatDistanceToNow } from "date-fns" ;
5+ import { ru } from "date-fns/locale" ;
6+
17import { useReportPageSocket } from "@/hooks/useReportPageSocket" ;
28import { useSocketEvent } from "@/hooks/useSocketEvent" ;
39import {
@@ -9,19 +15,12 @@ import {
915 saveTitleEvent ,
1016 updateReportPathIdEvent ,
1117} from "@/store/report" ;
12- import { SocketEvent } from "@/webSocketApi/models" ;
13- import { formatDistanceToNow } from "date-fns" ;
14- import { ru } from "date-fns/locale" ;
15- import { useUnit , useStoreMap } from "effector-react" ;
16- import { useEffect } from "react" ;
17- import { useParams , useNavigate } from "react-router-dom" ;
18- import Bug from "./components/Bug/Bug" ;
1918import { $bugsData } from "@/store/bugs" ;
19+ import { SocketEvent } from "@/webSocketApi/models" ;
2020import { BugEntity } from "@/types/bug" ;
21+ import { BugStatuses } from "@/const" ;
2122
22- function sortBugsByDate ( a : BugEntity , b : BugEntity ) {
23- return new Date ( b . createdAt ) . getTime ( ) - new Date ( a . createdAt ) . getTime ( ) ;
24- }
23+ import Bug from "./components/Bug/Bug" ;
2524
2625const ReportPage = ( ) => {
2726 const navigate = useNavigate ( ) ;
@@ -30,24 +29,31 @@ const ReportPage = () => {
3029 const title = useUnit ( $titleStore ) ;
3130 const creatorUserName = useUnit ( $creatorUserNameStore ) ;
3231
33- const bugs = useStoreMap ( {
32+ // баги существующие только на фронте
33+ const [ onlyUIBugs , setOnlyUIBugs ] = useState < BugEntity [ ] > ( [ ] ) ;
34+
35+ const bugsFromStore = useStoreMap ( {
3436 store : $bugsData ,
3537 keys : [ reportId ] ,
3638 fn : ( { bugs, reportBugs } , [ currentReportId ] ) => {
3739 if ( ! currentReportId ) return [ ] ;
3840 const bugIds = reportBugs [ Number ( currentReportId ) ] || [ ] ;
39- return bugIds
40- . map ( ( id ) => bugs [ id ] )
41- . filter ( Boolean )
42- . sort ( sortBugsByDate ) ;
41+ return bugIds . map ( ( id ) => bugs [ id ] ) . filter ( Boolean ) ;
4342 } ,
4443 } ) ;
4544
45+ const allBugs = [ ...bugsFromStore , ...onlyUIBugs ] ;
46+
4647 useReportPageSocket ( ) ;
4748 useSocketEvent ( SocketEvent . ReportPatch , ( patch ) =>
4849 patchReportSocketEvent ( patch )
4950 ) ;
5051
52+ const handleRemoveTemporaryBug = ( bugId : number ) => {
53+ // удаляем с ui фронтовый баг и обновляем тем, что с бэкенда
54+ setOnlyUIBugs ( ( prev ) => prev . filter ( ( bug ) => bug . id !== bugId ) ) ;
55+ } ;
56+
5157 // состояние страницы
5258 useEffect ( ( ) => {
5359 if ( reportId ) {
@@ -64,6 +70,29 @@ const ReportPage = () => {
6470 }
6571 } , [ initialReport ?. id , reportId , navigate ] ) ;
6672
73+ const handleAddBugClick = ( ) => {
74+ if ( ! reportId ) return ;
75+
76+ const currentISODate = new Date ( ) . toISOString ( ) ;
77+
78+ // Создаем локальный новый баг на фронте
79+ const newLocalBug : BugEntity = {
80+ id : Date . now ( ) ,
81+ receive : "" ,
82+ expect : "" ,
83+ status : BugStatuses . ACTIVE ,
84+ createdAt : currentISODate ,
85+ updatedAt : currentISODate ,
86+ creatorUserId : "" ,
87+ reportId : Number ( reportId ) ,
88+ attachments : null ,
89+ comments : null ,
90+ isLocalOnly : true ,
91+ } ;
92+
93+ setOnlyUIBugs ( ( prev ) => [ ...prev , newLocalBug ] ) ;
94+ } ;
95+
6796 return (
6897 < >
6998 < input
@@ -85,9 +114,21 @@ const ReportPage = () => {
85114 пользователем < strong > { creatorUserName || "Загрузка..." } </ strong >
86115 </ div >
87116 < div className = "flex flex-col gap-2" >
88- { bugs ?. map ( ( bug ) => (
89- < Bug key = { bug . id } bug = { bug } />
117+ { allBugs ?. map ( ( bug : BugEntity ) => (
118+ < Bug
119+ key = { bug . id }
120+ bug = { bug }
121+ onRemoveTemporary = {
122+ bug . isLocalOnly ? handleRemoveTemporaryBug : undefined
123+ }
124+ />
90125 ) ) }
126+ < button
127+ className = "btn btn-outline btn-primary font-normal ml-auto"
128+ onClick = { handleAddBugClick }
129+ >
130+ Добавить баг
131+ </ button >
91132 </ div >
92133 </ >
93134 ) ;
0 commit comments