From 7bbde70631b078e408e6f9f92d9be33c51ac0329 Mon Sep 17 00:00:00 2001 From: Chika Date: Wed, 31 Aug 2022 16:18:10 +0300 Subject: [PATCH] project: rename services -> api api/user: get domainName from auth service --- Components/Layout/Auth.jsx | 4 ++-- services/user/index.ts => api/user/query.ts | 2 +- {services => api}/user/tools.ts | 7 +------ {services => api}/user/types.ts | 1 + mocks/handlers.js | 2 ++ pages/index.jsx | 6 +++--- process/init/get-data/get-owner-data.ts | 5 ++--- 7 files changed, 12 insertions(+), 15 deletions(-) rename services/user/index.ts => api/user/query.ts (84%) rename {services => api}/user/tools.ts (70%) rename {services => api}/user/types.ts (86%) diff --git a/Components/Layout/Auth.jsx b/Components/Layout/Auth.jsx index dad2ff9..457bb91 100644 --- a/Components/Layout/Auth.jsx +++ b/Components/Layout/Auth.jsx @@ -1,5 +1,5 @@ import { useQuery } from '@tanstack/react-query'; -import { fetchUser } from 'services/user'; +import { getUser } from 'api/user/query'; import styled from 'styled-components'; import { Flex } from 'UIKit/grid'; import { min } from 'UIKit/mq'; @@ -19,7 +19,7 @@ const UserText = styled.span` `; function User() { - const { data: user } = useQuery(['user'], () => fetchUser()); + const { data: user } = useQuery(['user'], () => getUser()); return {user?.displayName}; } diff --git a/services/user/index.ts b/api/user/query.ts similarity index 84% rename from services/user/index.ts rename to api/user/query.ts index 2ddecc9..060f18f 100644 --- a/services/user/index.ts +++ b/api/user/query.ts @@ -4,7 +4,7 @@ import axios from 'axios'; import { love } from './tools'; import type { User } from './types'; -export async function fetchUser(config: AxiosRequestConfig) { +export async function getUser(config: AxiosRequestConfig) { const user = await axios .get(process.env.NEXT_PUBLIC_URL_GET_USER || '', config) .then((res) => love(res.data)); diff --git a/services/user/tools.ts b/api/user/tools.ts similarity index 70% rename from services/user/tools.ts rename to api/user/tools.ts index 3d86dc9..a82a62c 100644 --- a/services/user/tools.ts +++ b/api/user/tools.ts @@ -1,3 +1,4 @@ +/* eslint-disable import/prefer-default-export */ import type { User } from './types'; export function love(user: User) { @@ -7,9 +8,3 @@ export function love(user: User) { return user; } - -export function getDomainName(user: User) { - const { username, domain } = user; - - return `${domain}\\${username}`; -} diff --git a/services/user/types.ts b/api/user/types.ts similarity index 86% rename from services/user/types.ts rename to api/user/types.ts index 2b1e39a..d6f1b84 100644 --- a/services/user/types.ts +++ b/api/user/types.ts @@ -5,4 +5,5 @@ export type User = { position: string; mail: string; domain: string; + domainName: string; }; diff --git a/mocks/handlers.js b/mocks/handlers.js index ef92dff..29de392 100644 --- a/mocks/handlers.js +++ b/mocks/handlers.js @@ -8,6 +8,7 @@ const users = { position: 'Бизнес-аналитик', mail: 'akalinina@evoleasing.ru', domain: 'EVOLEASING', + domainName: 'EVOLEASING\\akalinina', }, vchikalkin: { username: 'vchikalkin', @@ -16,6 +17,7 @@ const users = { domain: 'EVOLEASING', department: 'IT', position: 'Старший разработчик', + domainName: 'EVOLEASING\\vchikalkin', }, }; diff --git a/pages/index.jsx b/pages/index.jsx index 5933779..f5bbb04 100644 --- a/pages/index.jsx +++ b/pages/index.jsx @@ -1,6 +1,7 @@ /* eslint-disable object-curly-newline */ import { useApolloClient } from '@apollo/client'; import { dehydrate, QueryClient } from '@tanstack/react-query'; +import { getUser } from 'api/user/query'; import initializeApollo from 'apollo/client'; import * as Calculation from 'Components/Calculation'; import Output from 'Components/Output'; @@ -8,7 +9,6 @@ import Head from 'next/head'; import getData, { getOwnerData } from 'process/init/get-data'; import injectDefaultReactions from 'process/init/inject-reactions/default'; import { useEffect } from 'react'; -import { fetchUser } from 'services/user'; import { useStore } from 'stores/hooks'; import styled from 'styled-components'; import { Box } from 'UIKit/grid'; @@ -63,7 +63,7 @@ export const getServerSideProps = async ({ req }) => { const { cookie = '' } = req.headers; // prettier-ignore - const fetchUserQuery = () => fetchUser({ + const queryGetUser = () => getUser({ headers: { cookie, }, @@ -71,7 +71,7 @@ export const getServerSideProps = async ({ req }) => { const queryClient = new QueryClient(); - const user = await queryClient.fetchQuery(['user'], fetchUserQuery); + const user = await queryClient.fetchQuery(['user'], queryGetUser); const apolloClient = initializeApollo(); diff --git a/process/init/get-data/get-owner-data.ts b/process/init/get-data/get-owner-data.ts index e6e52e3..bf1dd34 100644 --- a/process/init/get-data/get-owner-data.ts +++ b/process/init/get-data/get-owner-data.ts @@ -1,7 +1,6 @@ import type { ApolloClient, NormalizedCache } from '@apollo/client'; import { gql } from '@apollo/client'; -import { getDomainName } from 'services/user/tools'; -import type { User } from 'services/user/types'; +import type { User } from 'api/user/types'; import type { GetOwnerData, GetOwnerDataVariables } from './__generated__/GetOwnerData'; const QUERY_GET_OWNER_DATA = gql` @@ -24,7 +23,7 @@ export default async function getOwnerData( const { data: ownerData } = await apolloClient.query({ query: QUERY_GET_OWNER_DATA, variables: { - domainname: getDomainName(user), + domainname: user.domainName, }, });