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

Show related projects of qfRound in adminJs #1046

Merged
Merged
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
6 changes: 3 additions & 3 deletions src/entities/qfRound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ export class QfRound extends BaseEntity {
@Column('text', { nullable: true })
name: string;

@ManyToMany(type => Project, project => project.qfRounds)
projects: Project[];

@Field({ nullable: true })
@Column({ nullable: true })
isActive: boolean;
Expand All @@ -52,4 +49,7 @@ export class QfRound extends BaseEntity {

@CreateDateColumn()
createdAt: Date;

@ManyToMany(type => Project, project => project.qfRounds)
projects: Project[];
}
13 changes: 13 additions & 0 deletions src/repositories/qfRoundRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,16 @@ export const deactivateExpiredQfRounds = async (): Promise<void> => {
[now],
);
};

export const getRelatedProjectsOfQfRound = async (
qfRoundId: number,
): Promise<{ slug: string; name: string }[]> => {
const query = `
SELECT "p"."slug", "p"."title" , p.id
FROM "project" "p"
INNER JOIN "project_qf_rounds_qf_round" "qp" ON "qp"."projectId" = "p"."id"
WHERE "qp"."qfRoundId" = ${qfRoundId}
`;

return QfRound.query(query);
};
35 changes: 35 additions & 0 deletions src/server/adminJs/tabs/components/ProjectsInQfRound.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { useEffect, useState } from 'react';
import { withTheme } from 'styled-components';
import { Section, Label, Link } from '@adminjs/design-system';

const ProjectsInQfRound = props => {
const projects = props?.record?.params?.projects;

return (
<div>
<Label>Related Projects</Label>
<Section>
{projects.map(project => {
const { id, title, slug } = project;
const projectLink = `/admin/resources/Project/records/${id}/show`;
return (
<div key={id}>
<br />
<Section>
<h1>{title}</h1>
<h2>{slug}</h2>
<br />
<Link href={projectLink || ''} target="_blank">
{projectLink}
</Link>
</Section>
</div>
);
})}
</Section>
<br />
</div>
);
};

export default withTheme(ProjectsInQfRound);
41 changes: 37 additions & 4 deletions src/server/adminJs/tabs/qfRoundTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import {
AdminJsContextInterface,
AdminJsRequestInterface,
} from '../adminJs-types';
import { ValidationError } from 'adminjs';
import adminJs, { ValidationError } from 'adminjs';
import { isQfRoundHasEnded } from '../../../services/qfRoundService';
import { findQfRoundById } from '../../../repositories/qfRoundRepository';
import {
findQfRoundById,
getRelatedProjectsOfQfRound,
} from '../../../repositories/qfRoundRepository';
import { RecordJSON } from 'adminjs/src/frontend/interfaces/record-json.interface';

export const refreshMaterializedViews = async (
response,
Expand All @@ -24,6 +28,26 @@ export const refreshMaterializedViews = async (
return response;
};

export const fillProjects: After<ActionResponse> = async (
response,
_request,
_context,
) => {
const record: RecordJSON = response.record || {};
const qfRoundId = record.params.qfRoundId || record.params.id;
const projects = await getRelatedProjectsOfQfRound(qfRoundId);

const adminJsBaseUrl = process.env.SERVER_URL;
response.record = {
...record,
params: {
...record.params,
projects,
},
};
return response;
};

export const qfRoundTab = {
resource: QfRound,
options: {
Expand All @@ -47,11 +71,15 @@ export const qfRoundTab = {
isVisible: true,
},
projects: {
type: 'mixed',
isVisible: {
list: true,
edit: false,
list: false,
filter: false,
show: true,
edit: false,
},
components: {
show: adminJs.bundle('./components/ProjectsInQfRound'),
},
},
createdAt: {
Expand Down Expand Up @@ -88,6 +116,11 @@ export const qfRoundTab = {
canAccessQfRoundAction({ currentAdmin }, ResourceActions.NEW),
after: refreshMaterializedViews,
},
show: {
isAccessible: ({ currentAdmin }) =>
canAccessQfRoundAction({ currentAdmin }, ResourceActions.SHOW),
after: fillProjects,
},
edit: {
isAccessible: ({ currentAdmin }) =>
canAccessQfRoundAction({ currentAdmin }, ResourceActions.EDIT),
Expand Down