@@ -6,11 +6,24 @@ import {
66 sample ,
77} from "effector" ;
88import { fetchReport , createReport , updateReport } from "@/api/reports" ;
9- import { NewReport , Report } from "@/types/report" ;
10- import { User } from "@/types/user" ;
119import { ReportStatuses } from "@/const" ;
10+ import { User } from "@/types/user" ;
11+ import { NewReport , ReportFormUIData , Report } from "@/types/report" ;
1212import { ReportResponse } from "@/api/reports/models" ;
1313
14+ const defaultFormData = {
15+ id : null ,
16+ title : "" ,
17+ status : ReportStatuses . IN_PROGRESS ,
18+ responsible : null ,
19+ participants : [ ] ,
20+ responsibleId : "" ,
21+ creator : null ,
22+ createdAt : null ,
23+ updatedAt : null ,
24+ bugs : [ ] ,
25+ } ;
26+
1427const convertBackResponseToStoreModel = ( reportResponse : ReportResponse ) => ( {
1528 id : reportResponse . id ,
1629 title : reportResponse . title || "" ,
@@ -47,15 +60,22 @@ export const createReportFx = createEffect(async (newReport: NewReport) => {
4760 return data ;
4861} ) ;
4962
50- export const updateReportFx = createEffect ( async ( currentReport : Report ) => {
51- if ( ! currentReport . responsible || ! currentReport . id ) return ;
52- const payload = {
53- title : currentReport . title ,
54- status : currentReport . status ,
55- responsibleUserId : currentReport . responsible . id ,
56- } ;
57- return await updateReport ( payload , currentReport . id ) ;
58- } ) ;
63+ export const updateReportFx = createEffect (
64+ async ( report : {
65+ id : number | null ;
66+ title : string | null ;
67+ status : ReportStatuses | null ;
68+ responsibleId : string | null ;
69+ } ) => {
70+ if ( ! report . id ) return ;
71+ const payload = {
72+ title : report . title ,
73+ status : report . status ,
74+ responsible : report . responsibleId ,
75+ } ;
76+ return await updateReport ( payload , report . id ) ;
77+ }
78+ ) ;
5979
6080export const updateReportEvent = createEvent ( ) ;
6181export const clearReport = createEvent ( ) ;
@@ -65,47 +85,31 @@ export const updateTitle = createEvent<string>();
6585export const updateStatus = createEvent < number > ( ) ;
6686export const updateResponsible = createEvent < User | null > ( ) ;
6787
68- export const setIsNewReport = createEvent < boolean > ( ) ;
88+ export const setIsNewReport = createEvent ( ) ;
6989
70- export const $isNewReport = createStore < boolean > ( true )
90+ export const $isNewReport = createStore ( true )
7191 . on ( setIsNewReport , ( _ , isNew ) => isNew )
7292 . reset ( clearReport ) ;
7393
7494export const $isReportChanged = createStore ( false ) . reset ( clearReport ) ;
7595
76- export const $initialReportForm = createStore < Report > ( {
77- id : null ,
78- title : "" ,
79- status : ReportStatuses . IN_PROGRESS ,
80- responsible : null ,
81- responsibleId : "" ,
82- creator : null ,
83- createdAt : null ,
84- updatedAt : null ,
85- participants : [ ] ,
86- bugs : [ ] ,
87- } )
96+ export const $initialReportForm = createStore < Report > ( defaultFormData )
8897 . on ( fetchReportFx . doneData , ( _ , report ) =>
8998 convertBackResponseToStoreModel ( report )
9099 )
91100 . reset ( clearReport ) ;
92101
93- // Текущее состояние репорта (изменяемые данные, вводимые в форму)
94- export const $reportForm = createStore < Report > ( {
95- id : null ,
96- title : "" ,
97- status : ReportStatuses . IN_PROGRESS ,
98- responsible : null ,
99- participants : [ ] ,
100- responsibleId : "" ,
101- creator : null ,
102- createdAt : null ,
103- updatedAt : null ,
104- bugs : [ ] ,
105- } )
106- . on ( fetchReportFx . doneData , ( _ , report ) =>
107- convertBackResponseToStoreModel ( report )
108- )
102+ // Текущее состояние репорта (изменяемые данные)
103+ export const $reportForm = createStore < ReportFormUIData > ( defaultFormData )
104+ . on ( fetchReportFx . doneData , ( _ , report ) => {
105+ return {
106+ id : report . id ,
107+ title : report . title || "" ,
108+ status : report . status || 0 ,
109+ responsible : report . responsible || { } ,
110+ participants : report . participants || [ ] ,
111+ } ;
112+ } )
109113 . on ( updateTitle , ( state , title ) => ( { ...state , title } ) )
110114 . on ( updateResponsible , ( state , responsible ) => ( { ...state , responsible } ) )
111115 . on ( updateStatus , ( state , status ) => ( { ...state , status } ) )
@@ -152,32 +156,26 @@ sample({
152156} ) ;
153157
154158sample ( {
155- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
156- // @ts -ignore
157- // todo fix ts-ignore
158- source : $combinedForm , // Здесь объект { reportForm, initialForm }
159+ source : $combinedForm , // { reportForm, initialForm }
159160 clock : updateReportEvent ,
160161 fn : ( { reportForm, initialForm } ) => {
161- // Используем правильные имена
162- if ( ! initialForm ) return null ;
162+ const responsibleId = reportForm . responsible ?. id || null ;
163+ const initialResponsibleId = initialForm . responsible ?. id || null ;
163164
164- const updatedReport : Partial < typeof reportForm > = {
165+ const diff = {
165166 id : reportForm . id ,
166- title :
167- reportForm . title !== initialForm . title ? reportForm . title : undefined ,
167+ title : reportForm . title !== initialForm . title ? reportForm . title : null ,
168168 status :
169- reportForm . status !== initialForm . status
170- ? reportForm . status
171- : undefined ,
172- responsible :
173- reportForm . responsible ?. id !== initialForm . responsible ?. id
174- ? reportForm . responsible
175- : undefined ,
169+ reportForm . status !== initialForm . status ? reportForm . status : null ,
170+ responsibleId :
171+ responsibleId &&
172+ initialResponsibleId &&
173+ initialResponsibleId !== responsibleId
174+ ? responsibleId
175+ : null ,
176176 } ;
177177
178- return Object . values ( updatedReport ) . some ( ( value ) => value !== undefined )
179- ? updatedReport
180- : null ;
178+ return diff ;
181179 } ,
182180 target : updateReportFx ,
183181} ) ;
0 commit comments