5
5
useFindStudentById ,
6
6
useUpdateStudent ,
7
7
} from "@litespace/headless/student" ;
8
- import { useInfiniteTopics } from "@litespace/headless/topic" ;
8
+ import { useInfiniteTopics , useUserTopics } from "@litespace/headless/topic" ;
9
+ import { useUpdateUserTopics } from "@litespace/headless/user" ;
9
10
import { IStudent , ITopic } from "@litespace/types" ;
10
11
import { Button } from "@litespace/ui/Button" ;
11
12
import { Form } from "@litespace/ui/Form" ;
@@ -21,7 +22,7 @@ import { Typography } from "@litespace/ui/Typography";
21
22
import React , { useCallback , useMemo } from "react" ;
22
23
23
24
type IForm = {
24
- topics : ITopic . Self [ "name" ] [ "ar" ] [ ] ;
25
+ topics : ITopic . Self [ ] ;
25
26
career : string ;
26
27
level : IStudent . EnglishLevel ;
27
28
aim : string ;
@@ -31,17 +32,18 @@ export const StudentPublicInfo: React.FC<{ id: number }> = ({ id }) => {
31
32
const intl = useFormatMessage ( ) ;
32
33
const toast = useToast ( ) ;
33
34
34
- const { data } = useFindStudentById ( id ) ;
35
+ const { data : studentData } = useFindStudentById ( id ) ;
35
36
36
37
const { list : allTopics } = useInfiniteTopics ( ) ;
38
+ const { query : studentTopics } = useUserTopics ( ) ;
37
39
38
40
const onSuccess = useCallback ( ( ) => {
39
41
toast . success ( {
40
42
title : intl ( "student-settings.updated-successfully" ) ,
41
43
} ) ;
42
44
} , [ intl , toast ] ) ;
43
45
44
- const onError = useOnError ( {
46
+ const studentOnError = useOnError ( {
45
47
type : "mutation" ,
46
48
handler : ( { messageId } ) => {
47
49
toast . error ( {
@@ -51,32 +53,62 @@ export const StudentPublicInfo: React.FC<{ id: number }> = ({ id }) => {
51
53
} ,
52
54
} ) ;
53
55
54
- const updateStudent = useUpdateStudent ( { onSuccess, onError } ) ;
56
+ const topicOnError = useOnError ( {
57
+ type : "mutation" ,
58
+ handler : ( { messageId } ) => {
59
+ toast . error ( {
60
+ title : intl ( "complete-profile.update.error" ) ,
61
+ description : intl ( messageId ) ,
62
+ } ) ;
63
+ } ,
64
+ } ) ;
65
+
66
+ const updateStudent = useUpdateStudent ( {
67
+ onSuccess,
68
+ onError : studentOnError ,
69
+ } ) ;
70
+ const updateStudentTopics = useUpdateUserTopics ( {
71
+ onSuccess,
72
+ onError : topicOnError ,
73
+ } ) ;
55
74
56
75
const validators = useMakeValidators < IForm > ( {
57
76
career : { required : false } ,
58
77
level : { required : false , validate : validateEnglishLevel } ,
59
78
aim : { required : false } ,
60
79
} ) ;
61
80
81
+ const studentTopicsIds = useMemo (
82
+ ( ) => studentTopics . data ?. map ( ( topic ) => topic . id ) ,
83
+ [ studentTopics . data ]
84
+ ) ;
85
+
62
86
const form = useForm < IForm > ( {
63
87
defaults : {
64
- topics : data ?. topics . map ( ( topic ) => topic . name . ar ) || [ ] ,
65
- career : data ?. career || intl ( "labels.jobs.student" ) ,
66
- level : data ?. level || IStudent . EnglishLevel . Beginner ,
67
- aim : data ?. aim || "" ,
88
+ topics : studentTopics . data || [ ] ,
89
+ career : studentData ?. jobTitle || "" ,
90
+ level : studentData ?. englishLevel || IStudent . EnglishLevel . Beginner ,
91
+ aim : studentData ?. learningObjective || "" ,
68
92
} ,
69
93
validators,
70
94
onSubmit ( data ) {
71
95
updateStudent . mutate ( {
72
- id,
73
96
payload : {
74
- topics : data . topics ,
75
- career : data . career ,
76
- level : data . level ,
77
- aim : data . aim ,
97
+ id ,
98
+ englishLevel : data . level ,
99
+ jobTitle : data . career ,
100
+ learningObjective : data . aim ,
78
101
} ,
79
102
} ) ;
103
+ updateStudentTopics . mutate ( {
104
+ addTopics : data . topics
105
+ . filter ( ( topic ) => ! studentTopicsIds ?. includes ( topic . id ) )
106
+ . map ( ( topic ) => topic . id ) ,
107
+ removeTopics :
108
+ studentTopicsIds ?. filter (
109
+ ( topic ) => ! data . topics . map ( ( tp ) => tp . id ) . includes ( topic )
110
+ ) || [ ] ,
111
+ } ) ;
80
112
} ,
81
113
} ) ;
82
114
@@ -101,12 +133,17 @@ export const StudentPublicInfo: React.FC<{ id: number }> = ({ id }) => {
101
133
options = {
102
134
allTopics ?. map ( ( topic ) => ( {
103
135
label : topic . name . ar ,
104
- value : topic . name . ar ,
136
+ value : topic . id ,
105
137
} ) ) || [ ]
106
138
}
107
139
placeholder = { intl ( "complete-profile.topics.placeholder" ) }
108
- values = { form . state . topics }
109
- setValues = { ( values ) => form . set ( "topics" , values ) }
140
+ values = { form . state . topics . map ( ( topic ) => topic . id ) }
141
+ setValues = { ( values ) =>
142
+ form . set (
143
+ "topics" ,
144
+ allTopics ?. filter ( ( topic ) => values . includes ( topic . id ) ) || [ ]
145
+ )
146
+ }
110
147
/>
111
148
112
149
< Input
0 commit comments