Skip to content

Commit

Permalink
[optimize] simplify Scroll List components with Query Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Jul 11, 2023
1 parent 704cad7 commit 8d9d85a
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 156 deletions.
51 changes: 25 additions & 26 deletions components/Activity/ActivityList.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { observer } from 'mobx-react';
import { ScrollList } from 'mobx-restful-table';
import { FC, PureComponent } from 'react';
import { Col, Row } from 'react-bootstrap';

import {
Activity,
ActivityFilter,
ActivityListType,
ActivityModel,
} from '../../models/Activity';
import platformAdmin from '../../models/PlatformAdmin';
import sessionStore from '../../models/Session';
import { i18n } from '../../models/Translation';
import { XScrollList, XScrollListProps } from '../layout/ScrollList';
import { XScrollListProps } from '../layout/ScrollList';
import { ActivityCard, ActivityCardProps } from './ActivityCard';

const { t } = i18n;

export interface ActivityListProps
export interface ActivityListLayoutProps
extends XScrollListProps<Activity>,
Pick<ActivityCardProps, 'onPublish' | 'onDelete'> {
type?: ActivityListType;
size?: 'sm' | 'lg';
userId?: string;
}

export const ActivityListLayout = ({
export const ActivityListLayout: FC<ActivityListLayoutProps> = ({
size,
type = 'online',
defaultData = [],
userId,
...props
}: ActivityListProps) => (
}) => (
<Row
className="g-4"
xs={1}
Expand All @@ -53,21 +53,13 @@ export const ActivityListLayout = ({
</Row>
);

@observer
export default class ActivityList extends XScrollList<ActivityListProps> {
store = new ActivityModel();

filter: ActivityFilter = {
userId: this.props.userId,
listType: this.props.type,
};
export type ActivityListProps = ActivityListLayoutProps;

constructor(props: ActivityListProps) {
super(props);

this.boot();
export default class ActivityList extends PureComponent<ActivityListProps> {
store = new ActivityModel();

if (props.type === 'admin' && !platformAdmin.isPlatformAdmin)
componentDidMount() {
if (this.props.type === 'admin' && !platformAdmin.isPlatformAdmin)
platformAdmin.checkAuthorization();
}

Expand All @@ -86,15 +78,22 @@ export default class ActivityList extends XScrollList<ActivityListProps> {
this.props.onDelete?.(name);
};

renderList() {
const { allItems } = this.store;
render() {
const { userId, type } = this.props;

return (
<ActivityListLayout
{...this.props}
defaultData={allItems}
onPublish={this.onPublish}
onDelete={this.onDelete}
<ScrollList
translator={i18n}
store={this.store}
filter={{ userId, listType: type }}
renderList={allItems => (
<ActivityListLayout
{...this.props}
defaultData={allItems}
onPublish={this.onPublish}
onDelete={this.onDelete}
/>
)}
/>
);
}
Expand Down
7 changes: 4 additions & 3 deletions components/Activity/AwardList.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { faTrash } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Button, Image, Table } from 'react-bootstrap';

import { ScrollList, ScrollListProps } from 'mobx-restful-table';
import { FC, PureComponent } from 'react';
import { Button, Image, Table } from 'react-bootstrap';

import { Award } from '../../models/Award';
import { i18n } from '../../models/Translation';
import styles from '../../styles/Table.module.less';
Expand Down Expand Up @@ -80,7 +80,8 @@ export const AwardListLayout: FC<AwardListLayoutProps> = ({
</Table>
);

export type AwardListProps = ScrollListProps<Award> & AwardListLayoutProps;
export type AwardListProps = Pick<ScrollListProps<Award>, 'store'> &
AwardListLayoutProps;

export class AwardList extends PureComponent<AwardListProps> {
onEdit = (id: string) => {
Expand Down
4 changes: 1 addition & 3 deletions components/Activity/EnrollmentList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ export const EnrollmentListLayout: FC<EnrollmentListLayoutProps> = ({
</Table>
);

export interface EnrollmentListProps
extends ScrollListProps<Enrollment>,
EnrollmentListLayoutProps {
export interface EnrollmentListProps extends EnrollmentListLayoutProps {
activity: string;
}

Expand Down
2 changes: 1 addition & 1 deletion components/Message/MessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const MessageListLayout: FC<MessageListLayoutProps> = ({
</Table>
);

export type MessageListProps = ScrollListProps<Message> &
export type MessageListProps = Pick<ScrollListProps<Message>, 'store'> &
MessageListLayoutProps;

@observer
Expand Down
61 changes: 12 additions & 49 deletions components/Team/TeamAdministratorTable.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import { observer } from 'mobx-react';
import { FC } from 'react';
import { Form, Table } from 'react-bootstrap';

import sessionStore from '../../models/Session';
import {
MembershipStatus,
TeamMember,
TeamMemberFilter,
TeamMemberModel,
} from '../../models/Team';
import { TeamMember } from '../../models/Team';
import { i18n } from '../../models/Translation';
import styles from '../../styles/Table.module.less';
import { convertDatetime } from '../../utils/time';
import { XScrollList, XScrollListProps } from '../layout/ScrollList';
import { XScrollListProps } from '../layout/ScrollList';

const { t } = i18n;

export interface TeamAdministratorTableProps
export interface TeamAdministratorTableLayoutProps
extends XScrollListProps<TeamMember> {
store: TeamMemberModel;
onUpdateRole?: (userId: string, role: 'admin' | 'member') => any;
onPopUpUpdateRoleModal?: (userId: string) => any;
}

const TableHeads = [
const TableHeads = () => [
'#',
t('nick_name'),
t('mail'),
Expand All @@ -33,23 +28,20 @@ const TableHeads = [
t('role_type'),
];

const RoleName = {
const RoleName = () => ({
member: t('member'),
admin: t('admin'),
};
});

export const TeamAdministratorTableLayout = observer(
({
defaultData = [],
onUpdateRole,
}: Omit<TeamAdministratorTableProps, 'store'>) => {
export const TeamAdministratorTableLayout: FC<TeamAdministratorTableLayoutProps> =
observer(({ defaultData = [], onUpdateRole }) => {
const { id: currentUserId } = sessionStore?.user || {};

return (
<Table hover responsive="lg" className={styles.table}>
<thead>
<tr>
{TableHeads.map((data, idx) => (
{TableHeads().map((data, idx) => (
<th key={idx + data}>{data}</th>
))}
</tr>
Expand Down Expand Up @@ -87,7 +79,7 @@ export const TeamAdministratorTableLayout = observer(
}
defaultValue={role}
>
{Object.entries(RoleName).map(([key, value]) => (
{Object.entries(RoleName()).map(([key, value]) => (
<option key={key} value={key}>
{value}
</option>
Expand All @@ -106,33 +98,4 @@ export const TeamAdministratorTableLayout = observer(
</tbody>
</Table>
);
},
);

@observer
export class TeamAdministratorTable extends XScrollList<TeamAdministratorTableProps> {
store = this.props.store;

filter: TeamMemberFilter = {
status: MembershipStatus.APPROVED,
};

constructor(props: TeamAdministratorTableProps) {
super(props);

this.boot();
}

onUpdateRole: TeamAdministratorTableProps['onUpdateRole'] = (userId, role) =>
this.store.updateRole(userId, role);

renderList() {
return (
<TeamAdministratorTableLayout
{...this.props}
defaultData={this.store.allItems}
onUpdateRole={this.onUpdateRole}
/>
);
}
}
});
2 changes: 1 addition & 1 deletion components/Team/TeamAwardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const TeamAwardListLayout: FC<TeamAwardListLayoutProps> = ({
</Row>
);

export type TeamAwardListProps = ScrollListProps<Team> &
export type TeamAwardListProps = Pick<ScrollListProps<Team>, 'store'> &
TeamAwardListLayoutProps;

export class TeamAwardList extends PureComponent<TeamAwardListProps> {
Expand Down
35 changes: 2 additions & 33 deletions components/Team/TeamParticipantTable.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { ScrollList, ScrollListProps } from 'mobx-restful-table';
import { FC, PureComponent } from 'react';
import { FC } from 'react';
import { Form, Table } from 'react-bootstrap';

import {
MembershipStatus,
TeamMember,
TeamMemberModel,
} from '../../models/Team';
import { MembershipStatus, TeamMember } from '../../models/Team';
import { i18n } from '../../models/Translation';
import styles from '../../styles/Table.module.less';
import { convertDatetime } from '../../utils/time';
Expand Down Expand Up @@ -102,29 +97,3 @@ export const TeamParticipantTableLayout: FC<
</tbody>
</Table>
);

export interface TeamParticipantTableProps
extends ScrollListProps<TeamMember>,
TeamParticipantTableLayoutProps {
store: TeamMemberModel;
}

export class TeamParticipantTable extends PureComponent<TeamParticipantTableProps> {
onApprove: TeamParticipantTableProps['onApprove'] = (userId, status) =>
this.props.store.approveOne(userId, status);

render() {
return (
<ScrollList
translator={i18n}
store={this.props.store}
renderList={allItems => (
<TeamParticipantTableLayout
defaultData={allItems}
onApprove={this.onApprove}
/>
)}
/>
);
}
}
7 changes: 2 additions & 5 deletions components/Team/TeamWorkList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { faCalendarDay } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { ScrollList, ScrollListProps } from 'mobx-restful-table';
import { ScrollList } from 'mobx-restful-table';
import { FC, PureComponent } from 'react';
import {
Button,
Expand Down Expand Up @@ -116,10 +116,7 @@ export const TeamWorkListLayout: FC<TeamWorkListLayoutProps> = ({
</Container>
);

export type TeamWorkListProps = ScrollListProps<TeamWork> &
TeamWorkListLayoutProps;

export class TeamWorkList extends PureComponent<TeamWorkListProps> {
export class TeamWorkList extends PureComponent<TeamWorkListLayoutProps> {
store = activityStore.teamOf(this.props.activity).workOf(this.props.team);

onDelete = (id?: string) =>
Expand Down
2 changes: 1 addition & 1 deletion components/User/UserList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const UserListLayout: FC<UserListLayoutProps> = ({
</>
);

export type UserListProps = ScrollListProps<User, UserFilter> &
export type UserListProps = Pick<ScrollListProps<User, UserFilter>, 'store'> &
UserListLayoutProps;

export class UserList extends PureComponent<UserListProps> {
Expand Down
30 changes: 1 addition & 29 deletions components/layout/ScrollList.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
import { Loading } from 'idea-react';
import { observable } from 'mobx';
import { DataObject } from 'mobx-restful';
import { ScrollList, ScrollListProps } from 'mobx-restful-table';

import { i18n } from '../../models/Translation';
import { ScrollListProps } from 'mobx-restful-table';

export interface XScrollListProps<T extends DataObject = DataObject>
extends Pick<ScrollListProps<T>, 'defaultData'> {
selectedIds?: string[];
onSelect?: (selectedIds: string[]) => any;
}

export abstract class XScrollList<
P extends XScrollListProps,
> extends ScrollList<P> {
translator = i18n;

@observable
selectedIds: string[] = [];

onSelect = (list: string[]) =>
(this.selectedIds = list) && this.props.onSelect?.(list);

render() {
const { downloading, uploading } = this.store;

return (
<>
{(downloading > 0 || uploading > 0) && <Loading />}

{super.render()}
</>
);
}
}
Loading

1 comment on commit 8d9d85a

@github-actions
Copy link

Choose a reason for hiding this comment

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

Deploy preview for open-hackathon ready!

✅ Preview
https://open-hackathon-45r7ntul7-techquery.vercel.app

Built with commit 8d9d85a.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.