Skip to content

Commit

Permalink
refactor: dashboard with local data
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelsr committed Feb 24, 2023
1 parent 5c6926a commit 44c8d26
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 66 deletions.
2 changes: 1 addition & 1 deletion backend/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export const appDataSource = new DataSource({
database: DB_NAME,
password: DB_PASSWORD,
ssl: false,
entities: Entities.MT,
entities: Entities.LOCAL_DATA,
synchronize: false,
});
42 changes: 19 additions & 23 deletions frontend/src/components/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as React from 'react';
import { DocumentNode, useQuery } from '@apollo/client';
import { useQuery } from '@apollo/client';
import { useState } from 'react';
import { User, UserMT } from '../queries/types';
import { User } from '../queries/types';
import { GET_USERS } from '../queries/user';

type Checkbox = {
onClick: () => void;
Expand All @@ -17,17 +18,13 @@ const Checkbox: React.FC<Checkbox> = ({ onClick }) => {
);
};

type Table = {
query: DocumentNode;
}

export const Table: React.FC<Table> = ({ query }) => {
const { loading, error, data } = useQuery(query);
export const Table = () => {
const { loading, error, data } = useQuery(GET_USERS);
const [isTableShown, setIsTableShown] = useState(false);

if (loading) return <p>Loading...</p>;
if (error) return <p>Upps...There is an error. :( </p>;
const users = data?.getUsers || data?.getUsersMT;
const users = data?.getUsers;

const handleShowTable = () => {
setIsTableShown(!isTableShown);
Expand All @@ -41,24 +38,24 @@ export const Table: React.FC<Table> = ({ query }) => {
<table className="w-11/12 border border-separate table-fixed border-slate-400">
<thead>
<tr>
<th className='border border-slate-300'>id</th>
<th className='border border-slate-300'>email</th>
<th className='border border-slate-300'>first_name</th>
<th className='border border-slate-300'>last_name</th>
<th className='border border-slate-300'>job_position</th>
<th className='border border-slate-300'>role_id</th>
<th className='border border-slate-300'>First Name</th>
<th className='border border-slate-300'>Last name</th>
<th className='border border-slate-300'>Country</th>
<th className='border border-slate-300'>Role</th>
<th className='border border-slate-300'>Invited</th>
<th className='border border-slate-300'>Registered</th>
</tr>
</thead>
<tbody>
{users.map((user: User | UserMT) => {
{users.map((user: User) => {
return (
<tr key={user.id}>
<td className='border border-slate-300'>{user.id}</td>
<td className='border border-slate-300'>{user.email}</td>
<td className='border border-slate-300'>{user.first_name}</td>
<td className='border border-slate-300'>{user.last_name}</td>
<td className='border border-slate-300'>{user.job_position}</td>
<td className='border border-slate-300'>{user.role_id}</td>
<td className='border border-slate-300'>{user.name}</td>
<td className='border border-slate-300'>{user.lastName}</td>
<td className='border border-slate-300'>{user.country}</td>
<td className='border border-slate-300'>{user.role}</td>
<td className='border border-slate-300'>{user.isInvited.toString()}</td>
<td className='border border-slate-300'>{user.isRegistered.toString()}</td>
</tr>
);
})}
Expand All @@ -69,4 +66,3 @@ export const Table: React.FC<Table> = ({ query }) => {
);
};


27 changes: 16 additions & 11 deletions frontend/src/containers/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { useQuery } from '@apollo/client';
import { BarChart } from '../components/BarChart';
import { LineChart } from '../components/LineChart';
import { PieChart } from '../components/PieChart';
import { RadarChart } from '../components/RadarChart';
import { SectionTitle } from '../components/SectionTitle';
import { Table } from '../components/Table';
import { GET_USERS_MT } from '../queries/user';

import { GET_USERS_BY_COUNTRY, GET_USERS_BY_ROLE } from '../queries/user';
import { Summary } from './Summary';

export const Dashboard = () => {
// const { loading: loadingUsersByCountry, error: errorUsersByCountry, data: usersByCountry } = useQuery(GET_USERS_BY_COUNTRY);
// const { loading: loadingUsersByRole, error: errorUsersByRole, data: usersByRole } = useQuery(GET_USERS_BY_ROLE);
const { loading: loadingUsersByCountry, error: errorUsersByCountry, data: usersByCountry } = useQuery(GET_USERS_BY_COUNTRY);
const { loading: loadingUsersByRole, error: errorUsersByRole, data: usersByRole } = useQuery(GET_USERS_BY_ROLE);


// if (loadingUsersByCountry && loadingUsersByRole) return <p>Loading...</p>;
// if (errorUsersByCountry || errorUsersByRole) return <p>Upps...There is an error. :( </p>;
if (loadingUsersByCountry && loadingUsersByRole) return <p>Loading...</p>;
if (errorUsersByCountry || errorUsersByRole) return <p>Upps...There is an error. :( </p>;

return (
<div>
<SectionTitle title='Summary'></SectionTitle>
{/* <Summary /> */}
<Table query={GET_USERS_MT} />
{/*<SectionTitle title='Charts'></SectionTitle>
<div className='flex justify-center mb-24'>
<Summary />
<Table />
<SectionTitle title='Charts'></SectionTitle>
<div className='flex justify-center mb-24'>
<BarChart data={usersByCountry.getUsersByCountry} dataKey='totalCount' xAxisKeys='country' layout='horizontal'></BarChart>
<PieChart data={usersByCountry.getUsersByCountry} dataKey='percentage' nameKey='country'></PieChart>
</div>
Expand All @@ -26,7 +31,7 @@ export const Dashboard = () => {
</div>
<div className='flex justify-center mt-12'>
<RadarChart data={usersByRole.getUsersByRole}></RadarChart>
</div> */}
</div>
</div>);
};

23 changes: 5 additions & 18 deletions frontend/src/queries/types.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
// export type User = {
// id: number;
// name: string;
// lastName: string;
// role: string;

// }

export interface User extends UserMT {
export type User = {
id: number;
name: string;
lastName: string;
role: string;
country: string;
isInvited: boolean;
isRegistered: boolean;
}

export type UserMT = {
id: number;
email: string;
first_name: string;
last_name: string;
job_position: string;
role_id: number;
}
13 changes: 0 additions & 13 deletions frontend/src/queries/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ export const GET_USERS = gql`
}
`;

export const GET_USERS_MT = gql`
query GetUsersMT {
getUsersMT {
id
email
first_name
last_name
job_position
role_id
}
}
`;

export const GET_USER_REGISTRATION_STATISTICS = gql`
query GetUserRegistrationStatistics {
getUserRegistrationStatistics {
Expand Down

0 comments on commit 44c8d26

Please sign in to comment.