Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
- fetch users' data with RTK
  • Loading branch information
elrouss committed Apr 12, 2024
1 parent 2fbba70 commit 5330351
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 128 deletions.
File renamed without changes.
41 changes: 0 additions & 41 deletions src/api/fetchUsersData.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/components/blocks/gallery-users/gallery-users.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import styles from './gallery-users.module.scss';
import { UserCard, IUserCardProps } from '../user-card/user-card';
import { UserCard } from '../user-card/user-card';
import { IUsersDataExtended } from '@/services/features/users/types';

interface IGalleryUsersProps {
data: IUserCardProps[] | null;
data: IUsersDataExtended[] | null;
}

export const GalleryUsers = ({ data }: IGalleryUsersProps) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import styles from './user-additional-info.module.scss';
import { TitleWithBorder } from '../title-with-border/title-with-border';
import { IUserCardProps } from '../user-card/user-card';
import { Category, ICategoryProps } from '@/components/ui/category/category';
import { IFormValues } from '@/hooks/useFormWithValidation';
import { IUserProfile } from '@/services/features/user-profile/types';

interface IUserAdditionalInfo {
title?: string;
selectedUser: IUserCardProps;
selectedUser: IUserProfile;
categories: ICategoryProps[];
values: IFormValues;
handleChange: React.ChangeEventHandler<HTMLInputElement>;
Expand Down
29 changes: 11 additions & 18 deletions src/components/blocks/user-card/user-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,21 @@ import {
unarchiveUser,
hideUser,
} from '@/services/features/users/slice';
import { IUsersDataExtended } from '@/services/features/users/types';

export interface IUserCardProps {
id: number;
img: string;
name: string;
username: string;
company: string;
city: string;
email: string;
phone: string;
isArchived?: boolean;
workspace?: string;
privacy?: string;
security?: string;
}

export const UserCard = (data: IUserCardProps) => {
export const UserCard = (data: IUsersDataExtended) => {
const [isDropdownShown, setIsDropdownShown] = useState(false);
const dispatch = useAppDispatch();
const navigate = useNavigate();

const { id, img, username, company, city, isArchived = false } = data;
const {
id,
img,
username,
company: { name },
address: { city },
isArchived = false,
} = data;

const onBtnMoreClick = () => {
setIsDropdownShown((prevState) => !prevState);
Expand All @@ -45,7 +38,7 @@ export const UserCard = (data: IUserCardProps) => {
<header className={styles.header}>
<div className={styles.info}>
<h3 className={styles.title}>{username}</h3>
<p className={styles.company}>{company}</p>
<p className={styles.company}>{name}</p>
</div>
<div className={styles.dropdownWrapper}>
<ButtonIcon
Expand Down
4 changes: 2 additions & 2 deletions src/components/blocks/user-form/user-form.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect, useState } from 'react';
import styles from './user-form.module.scss';
import { UserAdditionalInfo } from '../user-additional-info/user-additional-info';
import { IUserCardProps } from '../user-card/user-card';
import { UserMainInfo } from '../user-main-info/user-main-info';
import { ICategoryProps } from '@/components/ui/category/category';
import { IInputProps } from '@/components/ui/input/input';
Expand All @@ -10,12 +9,13 @@ import { ModalWrapper } from '@/components/ui/modal-wrapper/modal-wrapper';
import { handleFormInitialState } from '@/helpers/handleFormInitialState';
import useFormWithValidation from '@/hooks/useFormWithValidation';
import { useAppDispatch } from '@/services/app/hooks';
import { IUserProfile } from '@/services/features/user-profile/types';
import { updateUserData } from '@/services/features/users/slice';
import { IModalStatus } from '@/types';

interface IUserFormProps extends React.FormHTMLAttributes<HTMLFormElement> {
title?: string;
selectedUser: IUserCardProps;
selectedUser: IUserProfile;
categories: ICategoryProps[];
inputs: Omit<IInputProps, 'onButtonClick'>[];
buttonText: string;
Expand Down
17 changes: 8 additions & 9 deletions src/components/sections/layout-root/layout-root.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ReactNode, useEffect, useState } from 'react';
import { fetchUsersData } from '@/api/fetchUsersData';
import { Preloader } from '@/components/common/preloader/preloader';
import { getUsersData } from '@/helpers/data/getUsersData';
import { useScrollToTop } from '@/hooks/useScrollToTop';
import { useAppDispatch } from '@/services/app/hooks';
import { setInitialUsers } from '@/services/features/users/slice';
import { useAppDispatch, useAppSelector } from '@/services/app/hooks';
import { error } from '@/services/features/users/selectors';
import { fetchUsersData } from '@/services/features/users/slice';

interface ILayoutProps {
children?: ReactNode;
Expand All @@ -19,16 +18,16 @@ export const LayoutRoot = ({ children }: ILayoutProps) => {
const [isAppLoaded, setIsAppLoaded] = useState(hasInitiallyLoaded);
const dispatch = useAppDispatch();

const statusError = useAppSelector(error);

useEffect(() => {
if (hadInitialDataRequest) return;

const fetchData = async () => {
try {
const data = await fetchUsersData(6);

dispatch(setInitialUsers(getUsersData(data)));
} catch (e) {
console.error(e);
dispatch(fetchUsersData());
} catch (_) {
console.error(statusError);
} finally {
hadInitialDataRequest = true;
hasInitiallyLoaded = true;
Expand Down
24 changes: 0 additions & 24 deletions src/helpers/data/getUsersData.ts

This file was deleted.

7 changes: 4 additions & 3 deletions src/pages/[id]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { IUserCardProps } from '@/components/blocks/user-card/user-card';
import { UserForm } from '@/components/blocks/user-form/user-form';
import { Header } from '@/components/common/header/header';
import { LayoutAnimation } from '@/components/sections/layout-animation/layout-animation';
Expand All @@ -10,7 +9,9 @@ import { getJsonData } from '@/helpers/data/getJsonData';
import { useAppSelector, useAppDispatch } from '@/services/app/hooks';
import { getUserProfileData } from '@/services/features/user-profile/selectors';
import { setInitialUserData } from '@/services/features/user-profile/slice';
import { IUserProfile } from '@/services/features/user-profile/types';
import { getActiveUsers } from '@/services/features/users/selectors';
import { IUsersDataExtended } from '@/services/features/users/types';

let json = null as any;
getJsonData('user-profile').then((data) => (json = data));
Expand All @@ -19,7 +20,7 @@ export const UserProfile = () => {
const location = useLocation();
const dispatch = useAppDispatch();
const users = useAppSelector(getActiveUsers);
const selectedUser = useAppSelector(getUserProfileData) as IUserCardProps;
const selectedUser = useAppSelector(getUserProfileData) as IUserProfile;

useEffect(() => {
if (selectedUser) return;
Expand All @@ -28,7 +29,7 @@ export const UserProfile = () => {
setInitialUserData(
users.find(
(user) => user.id === Number(location.pathname.slice(1))
) as IUserCardProps
) as IUsersDataExtended
)
);
}, [users]);
Expand Down
13 changes: 9 additions & 4 deletions src/services/features/user-profile/slice.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { IUserCardProps } from '@/components/blocks/user-card/user-card';
import { IUserProfile } from './types';
import { IUsersDataExtended } from '../users/types';

type TSliceState = {
user: IUserCardProps | null;
user: IUserProfile | null;
};

const initialState: TSliceState = { user: null };
Expand All @@ -11,8 +12,12 @@ const userProfileSlice = createSlice({
name: 'userProfile',
initialState,
reducers: {
setInitialUserData: (state, action: PayloadAction<IUserCardProps>) => {
state.user = action.payload;
setInitialUserData: (state, action: PayloadAction<IUsersDataExtended>) => {
state.user = {
...action.payload,
city: action.payload.address.city,
company: action.payload.company.name,
};
},

reset: () => initialState,
Expand Down
15 changes: 15 additions & 0 deletions src/services/features/user-profile/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface IUserProfile {
id: number;
name: string;
username: string;
email: string;
city: string;
phone: string;
website: string;
company: string;
img: string;
workspace?: string;
privacy?: string;
security?: string;
isArchived?: boolean;
}
2 changes: 2 additions & 0 deletions src/services/features/users/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import { RootState } from '@/services/app/store';

export const getActiveUsers = (state: RootState) => state.users.activeUsers;
export const getArchiveUsers = (state: RootState) => state.users.archiveUsers;
export const status = (state: RootState) => state.users.status;
export const error = (state: RootState) => state.users.error;
Loading

0 comments on commit 5330351

Please sign in to comment.