Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datatrak): RN-1408: Move ProjectSelectForm to ui-components #5910

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions packages/datatrak-web/src/components/SelectList/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/datatrak-web/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

export { PageContainer } from './PageContainer';
export * from './Icons';
export * from './SelectList';
export { Autocomplete, QuestionAutocomplete } from './Autocomplete';
export { Button } from './Button';
export { ButtonLink } from './ButtonLink';
Expand Down
20 changes: 17 additions & 3 deletions packages/datatrak-web/src/features/EntitySelector/ResultsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/

import React from 'react';
import React, { ReactNode } from 'react';
import styled from 'styled-components';
import { Typography } from '@material-ui/core';
import { FormLabelProps, Typography } from '@material-ui/core';
import RoomIcon from '@material-ui/icons/Room';
import { DatatrakWebEntityDescendantsRequest } from '@tupaia/types';
import { ListItemType, SelectList } from '../../components';
import { SelectList } from '@tupaia/ui-components';

const DARK_BLUE = '#004975';

Expand Down Expand Up @@ -43,6 +43,20 @@ export const ResultItem = ({ name, parentName }) => {
);
};

type ListItemType = Record<string, unknown> & {
children?: ListItemType[];
content: string | ReactNode;
value: string;
selected?: boolean;
icon?: ReactNode;
tooltip?: string;
button?: boolean;
disabled?: boolean;
labelProps?: FormLabelProps & {
component?: React.ElementType;
};
};

type SearchResults = DatatrakWebEntityDescendantsRequest.ResBody;
interface ResultsListProps {
value: string;
Expand Down
19 changes: 17 additions & 2 deletions packages/datatrak-web/src/features/GroupedSurveyList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* Tupaia
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/
import React, { useEffect } from 'react';
import React, { ReactNode, useEffect } from 'react';
import styled from 'styled-components';
import { FormHelperText, FormLabelProps } from '@material-ui/core';
import { Country } from '@tupaia/types';
import { ListItemType, SelectList, SurveyFolderIcon, SurveyIcon } from '../components';
import { SelectList } from '@tupaia/ui-components';
import { SurveyFolderIcon, SurveyIcon } from '../components';
import { Survey } from '../types';
import { useCurrentUserContext, useProjectSurveys } from '../api';

Expand All @@ -21,6 +22,20 @@ const ListWrapper = styled.div`
}
`;

type ListItemType = Record<string, unknown> & {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on adding this to ui-components too, since it's the same as another component uses?

children?: ListItemType[];
content: string | ReactNode;
value: string;
selected?: boolean;
icon?: ReactNode;
tooltip?: string;
button?: boolean;
disabled?: boolean;
labelProps?: FormLabelProps & {
component?: React.ElementType;
};
};

const sortAlphanumerically = (a: ListItemType, b: ListItemType) => {
return (a.content as string).trim()?.localeCompare((b.content as string).trim(), 'en', {
numeric: true,
Expand Down
1 change: 0 additions & 1 deletion packages/datatrak-web/src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/

export { ProjectSelectForm } from './ProjectSelectForm';
export {
SurveyScreen,
SurveySuccessScreen,
Expand Down
11 changes: 9 additions & 2 deletions packages/datatrak-web/src/layout/UserMenu/ProjectSelectModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import React, { useState } from 'react';
import styled from 'styled-components';
import { Paper } from '@material-ui/core';
import { ProjectSelectForm, RequestProjectAccess } from '../../features';
import { useCurrentUserContext } from '../../api';
import { ProjectSelectForm } from '@tupaia/ui-components';
import { RequestProjectAccess } from '../../features';
import { useCurrentUserContext, useEditUser, useProjects } from '../../api';
import { Modal } from '../../components';

const Wrapper = styled(Paper)`
Expand All @@ -26,6 +27,8 @@ interface ModalProps {
export const ProjectSelectModal = ({ onClose }: ModalProps) => {
const { projectId } = useCurrentUserContext();
const [requestAccessProjectCode, setRequestAccessProjectCode] = useState<string | null>(null);
const { data: projects, isLoading } = useProjects();
const { mutate: onConfirm, isLoading: isConfirming } = useEditUser(onClose);

return (
// Enable the portal so it displays over any other content and we don't get z-index issues
Expand All @@ -41,6 +44,10 @@ export const ProjectSelectModal = ({ onClose }: ModalProps) => {
projectId={projectId}
onClose={onClose}
onRequestAccess={setRequestAccessProjectCode}
projects={projects}
isLoading={isLoading}
onConfirm={onConfirm}
isConfirming={isConfirming}
/>
)}
</Modal>
Expand Down
22 changes: 20 additions & 2 deletions packages/datatrak-web/src/views/ProjectSelectPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import React from 'react';
import styled from 'styled-components';
import { Paper } from '@material-ui/core';
import { useNavigate } from 'react-router-dom';
import { ProjectSelectForm } from '@tupaia/ui-components';
import { useFromLocation } from '../utils';
import { ProjectSelectForm } from '../features';
import { ROUTES } from '../constants';
import { useEditUser, useProjects } from '../api';

const Container = styled(Paper).attrs({
variant: 'outlined',
Expand All @@ -19,9 +20,19 @@ const Container = styled(Paper).attrs({
`;

export const ProjectSelectPage = () => {
const { data: projects, isLoading } = useProjects();
const { mutate: onConfirm, isLoading: isConfirming } = useEditUser();

const navigate = useNavigate();
const from = useFromLocation();

const onRequestAccess = (projectCode: string) => {
navigate({
pathname: ROUTES.REQUEST_ACCESS,
search: `?project=${projectCode}`,
});
};

const onSuccess = () => {
navigate(from || ROUTES.HOME, {
state: null,
Expand All @@ -30,7 +41,14 @@ export const ProjectSelectPage = () => {

return (
<Container>
<ProjectSelectForm onClose={onSuccess} />
<ProjectSelectForm
onRequestAccess={onRequestAccess}
onClose={onSuccess}
projects={projects}
isLoading={isLoading}
onConfirm={onConfirm}
isConfirming={isConfirming}
/>
</Container>
);
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/

import React from 'react';
import { List as MuiList } from '@material-ui/core';
import { ListItem as Item, ListItemType } from './ListItem';
import { ListItem as Item } from './ListItem';
import { ListItemType } from './types';

interface SelectListProps {
items?: ListItemType[];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/

import React, { ReactElement, ReactNode, useState } from 'react';
Expand All @@ -11,9 +11,10 @@ import {
ListItemProps as MuiListItemProps,
} from '@material-ui/core';
import { Check, KeyboardArrowRight } from '@material-ui/icons';
import { Tooltip } from '@tupaia/ui-components';
import { Tooltip } from '../Tooltip';
import { ListItemType } from './types';

// explicity set the types so that the overrides are applied, for the `button` prop
// explicitly set the types so that the overrides are applied, for the `button` prop
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops 😆

export const BaseListItem = styled(MuiListItem)<MuiListItemProps>`
display: flex;
align-items: center;
Expand All @@ -30,8 +31,8 @@ export const BaseListItem = styled(MuiListItem)<MuiListItemProps>`
&.MuiButtonBase-root {
&:hover,
&.Mui-selected:hover,
&:focus-visible,
&.Mui-selected:focus-visible {
&:focus,
&.Mui-selected:focus {
background-color: ${({ theme }) => theme.palette.primary.main}33;
}
}
Expand Down Expand Up @@ -74,17 +75,6 @@ const IconWrapper = styled.div`
}
`;

export type ListItemType = Record<string, unknown> & {
children?: ListItemType[];
content: string | ReactNode;
value: string;
selected?: boolean;
icon?: ReactNode;
tooltip?: string;
button?: boolean;
disabled?: boolean;
};

interface ListItemProps {
item: ListItemType;
children?: ReactNode;
Expand Down Expand Up @@ -126,6 +116,7 @@ export const ListItem = ({ item, children, onSelect }: ListItemProps) => {

return (
<li>
{/*@ts-ignore*/}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this?

<BaseListItem
button={button}
onClick={button ? onClick : null}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/

import React from 'react';
import styled, { css } from 'styled-components';
import { FormLabel, FormLabelProps, Typography } from '@material-ui/core';
import { ListItemType } from './ListItem';
import { FormLabel, Typography, FormLabelProps } from '@material-ui/core';
import { ListItemType } from './types';
import { List } from './List';

const Wrapper = styled.div`
Expand Down Expand Up @@ -51,9 +51,10 @@ const Label = styled(FormLabel)<{
}>`
margin-bottom: 1rem;
font-size: 0.875rem;
font-weight: 400;
color: ${({ theme, color }) => theme.palette.text[color!]};
font-weight: 400;
`;

interface SelectListProps {
items?: ListItemType[];
onSelect: (item: ListItemType) => void;
Expand Down
8 changes: 8 additions & 0 deletions packages/ui-components/src/components/SelectList/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Tupaia
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/

export { List } from './List';
export { ListItem } from './ListItem';
export { SelectList } from './SelectList';
18 changes: 18 additions & 0 deletions packages/ui-components/src/components/SelectList/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Tupaia
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd
*/

import React, { ReactNode } from 'react';
import { FormLabelProps } from '@material-ui/core';

export type ListItemType = Record<string, unknown> & {
children?: ListItemType[];
content: string | ReactNode;
value: string;
selected?: boolean;
icon?: ReactNode;
tooltip?: string;
button?: boolean;
disabled?: boolean;
};
1 change: 1 addition & 0 deletions packages/ui-components/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ export * from './Tooltip';
export * from './UserMessage';
export * from './TooltipIconButton';
export * from './Pagination';
export * from './SelectList';
Loading