Skip to content

Commit

Permalink
add new invalidate tag User and perform router push only after employ…
Browse files Browse the repository at this point in the history
…ee profile update
  • Loading branch information
zavarin-michael committed Aug 29, 2024
1 parent bcc914c commit fe9cbeb
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 30 deletions.
2 changes: 1 addition & 1 deletion apps/schools/domains/employee/redux/employeeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const employeeApi = commonApi.injectEndpoints({
method: 'PATCH',
body: data,
}),
invalidatesTags: ['Employee'],
invalidatesTags: ['Employee', 'User'],
}),
deleteEmployeeById: build.mutation<{}, DeleteEmployeeByIdData>({
query: (data) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ export const RegisterForm: React.FC<IRegisterFormProps> = ({ onFinish, onError }
name: name,
email: email,
}
updateProfile(updateEmail)
onFinish()
updateProfile(updateEmail).then(() => onFinish())
}
}, [data, updateProfile])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import Link from 'next/link'
import { useChangeUserProfileFormValidators } from '@domains/user/components/profile/profileEdit/hooks'
import { useUpdateEmployeeProfileByIdMutation } from '@domains/employee/redux/employeeApi'
import { handleSubmitForm } from '@domains/user/handlers/profile/profileEdit'
import { EventKey, useEventBus } from '@domains/common/providers/eventBusProvider'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

export function ProfileEdit() {
const { emit } = useEventBus()
const { user } = useUserProfile()

const [form] = Form.useForm()
Expand Down Expand Up @@ -52,7 +50,6 @@ export function ProfileEdit() {
onFinish={() => {
handleSubmitForm(user.employee_profile?.id ?? '', form, mutation).then((isSuccess) => {
if (isSuccess) {
emit(EventKey.RefetchProfileQuery)
router.push(RoutePath[AppRoutes.USER_LIST])
}
})
Expand Down
12 changes: 1 addition & 11 deletions apps/schools/domains/user/providers/authProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useRouter } from 'next/router'
import { useGetUserQuery } from '../redux/authenticationApi'
import Cookies from 'universal-cookie'
import { GetUserProfiles } from '@domains/user/redux/interfaces'
import { EventKey, useEventBus } from '@domains/common/providers/eventBusProvider'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

export const UserProfileContext = createContext<{
Expand All @@ -24,18 +23,13 @@ export const useUserProfile = () => useContext(UserProfileContext)

export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const router = useRouter()
const { on } = useEventBus()
const [token, setToken] = useState('')
const [user, setUser] = useState({})

const { data, error, refetch } = useGetUserQuery({})
const { data, error } = useGetUserQuery({})
const cookies = new Cookies()

useEffect(() => {
const unsubscribeOnRefetchProfileQuery = on(EventKey.RefetchProfileQuery, () => {
refetch()
})

if (router.pathname === RoutePath[AppRoutes.MOBILE_RECAPTCHA]) return

const jwtToken = typeof window !== 'undefined' ? cookies.get('jwtToken') : null
Expand All @@ -45,10 +39,6 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
} else {
router.push(RoutePath[AppRoutes.AUTH_SIGN_IN])
}

return () => {
unsubscribeOnRefetchProfileQuery()
}
}, [])

useEffect(() => {
Expand Down
4 changes: 4 additions & 0 deletions apps/schools/domains/user/redux/authenticationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,31 @@ const authenticationApi = commonApi.injectEndpoints({
method: 'POST',
body: data,
}),
invalidatesTags: ['User'],
}),
logout: build.mutation<{}, {}>({
query: (data) => ({
url: '/user-management/auth/jwt/logout',
method: 'POST',
body: data,
}),
invalidatesTags: ['User'],
}),
getUser: build.query<{ user: GetUserProfiles }, {}>({
query: (data) => ({
url: '/user-management/auth/me',
method: 'GET',
data: data,
}),
providesTags: ['User'],
}),
updateUser: build.mutation<{ user: GetUser }, UpdateUserData>({
query: (data) => ({
url: '/user-management/auth/me',
method: 'PATCH',
body: data,
}),
invalidatesTags: ['User'],
}),
updatePassword: build.mutation<{}, UpdatePasswordData>({
query: (data) => ({
Expand Down
1 change: 1 addition & 0 deletions apps/schools/domains/user/redux/userApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const userApi = commonApi.injectEndpoints({
method: 'POST',
body: data,
}),
invalidatesTags: ['User'],
}),
resetPassword: build.mutation<{}, PasswordReset>({
query: (data) => ({
Expand Down
21 changes: 9 additions & 12 deletions apps/schools/pages/auth/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ import React, { useState } from 'react'
import { Dictionary } from '@reduxjs/toolkit'
import { ContainerPage } from '../_app'
import AuthLayout, { IAuthLayoutProps } from '../../domains/user/components/auth/containers/AuthLayout'
import { FormContainer } from '../../domains/user/components/auth/formContainer'
import { InputPhoneForm } from '../../domains/user/components/auth/sharedForms/InputPhoneForm'
import { TabsAuthAction } from '../../domains/user/components/auth/headerActions'
import { ValidatePhoneForm } from '../../domains/user/components/auth/sharedForms/ValidatePhoneForm'
import { RegisterForm } from '../../domains/user/components/auth/registerForm'
import { CENTRALIZED } from '../../domains/common/components/styles/constantStyles'
import { FormContainer } from '@domains/user/components/auth/formContainer'
import { InputPhoneForm } from '@domains/user/components/auth/sharedForms/InputPhoneForm'
import { TabsAuthAction } from '@domains/user/components/auth/headerActions'
import { ValidatePhoneForm } from '@domains/user/components/auth/sharedForms/ValidatePhoneForm'
import { RegisterForm } from '@domains/user/components/auth/registerForm'
import { CENTRALIZED } from '@domains/common/components/styles/constantStyles'
import { Row } from 'antd'
import {
RegistrationDisclaimer,
RegistrationPhoneButtonLabel,
} from '../../domains/user/components/auth/constants/labels'
import { FirebaseReCaptcha } from '../../domains/user/providers/firebaseReCaptchaProvider'
import { RegistrationDisclaimer, RegistrationPhoneButtonLabel } from '@domains/user/components/auth/constants/labels'
import { FirebaseReCaptcha } from '@domains/user/providers/firebaseReCaptchaProvider'
import { AppRoutes, RoutePath } from '@domains/common/constants/routerEnums'

const RegisterPage: ContainerPage<IAuthLayoutProps> = (props) => {
Expand Down Expand Up @@ -56,7 +53,7 @@ const RegisterPage: ContainerPage<IAuthLayoutProps> = (props) => {
<>
<TabsAuthAction currentActiveKey={RoutePath[AppRoutes.AUTH_REGISTER]} />
<RegisterForm
onFinish={() => (window.location.href = '/')}
onFinish={() => Router.push(RoutePath[AppRoutes.USER_LIST])}
onError={() => {
setStep('inputPhone')
Router.push(RoutePath[AppRoutes.AUTH_REGISTER])
Expand Down
11 changes: 10 additions & 1 deletion apps/schools/store/commonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ export const commonApi = createApi({
return headers
},
}) as BaseQueryFn<string | FetchArgs, unknown, ErrorType, {}>,
tagTypes: ['Circle', 'Student', 'StudentJoinCircleQuery', 'Organization', 'Employee', 'Ticket', 'TicketComments'],
tagTypes: [
'Circle',
'Student',
'StudentJoinCircleQuery',
'Organization',
'Employee',
'Ticket',
'TicketComments',
'User',
],
endpoints: (_) => ({}),
})

0 comments on commit fe9cbeb

Please sign in to comment.