Skip to content

Commit

Permalink
improve adminjs to import qfround matching and better filters
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Sep 6, 2024
1 parent 23bdade commit 4498aa8
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/server/adminJs/tabs/components/CustomIdFilterComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { FormGroup, Label, Input } from '@adminjs/design-system';

const CustomIdFilterComponent = (props) => {
const { onChange, property, filter } = props;
const handleChange = (event) => {
onChange(property.path, event.target.value);
};

return (
<FormGroup>
<Label>{property.label}</Label>
<Input
type="text"
onChange={handleChange}
value={filter[property.path] || ''}
placeholder={`Enter ${property.label} ID`}
style={{
color: 'white',
backgroundColor: 'rgba(255, 255, 255, 0.1)', // Semi-transparent white background
borderColor: 'rgba(255, 255, 255, 0.3)', // Lighter border for contrast
}}
/>
</FormGroup>
);
};

export default CustomIdFilterComponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// components/CustomProjectReferenceListComponent.jsx
import React from 'react';
import { Link } from '@adminjs/design-system';

const CustomProjectReferenceListComponent = (props) => {
const { record } = props;
const projectId = record.params.project?.id || record.params.projectId || 'N/A';
const href = `/admin/resources/Project/records/${projectId}/show`;

return <Link href={href}>Project {projectId}</Link>;
};

export default CustomProjectReferenceListComponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// components/CustomProjectReferenceShowComponent.jsx
import React from 'react';
import { Link, ValueGroup } from '@adminjs/design-system';

const CustomProjectReferenceShowComponent = (props) => {
const { record } = props;
const projectId = record.params.project?.id || record.params.projectId || 'N/A';
const href = `/admin/resources/Project/records/${projectId}/show`;

return (
<ValueGroup label="Project">
<Link href={href} style={{ color: 'inherit', textDecoration: 'none' }}>
{projectId}
</Link>
</ValueGroup>
);
};

export default CustomProjectReferenceShowComponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// customQfRoundMultiUpdateComponent.js
import React, { useState } from 'react';
import { Box, Button, Text, DatePicker, Select } from '@adminjs/design-system';
import { FormGroup, Label, Input } from '@adminjs/design-system';
import { ApiClient } from 'adminjs';

const RecordInput = ({ index, record, updateRecord, removeRecord }) => (
<Box mb="lg" variant="white">
<FormGroup>
<Label>Project ID</Label>
<Input
value={record.projectId}
onChange={(e) => updateRecord(index, 'projectId', e.target.value)}
required
/>
</FormGroup>
<FormGroup>
<Label>QF Round ID</Label>
<Input
value={record.qfroundId}
onChange={(e) => updateRecord(index, 'qfroundId', e.target.value)}
required
/>
</FormGroup>
<FormGroup>
<Label>Matching Fund Amount</Label>
<Input
value={record.matchingFundAmount}
onChange={(e) => updateRecord(index, 'matchingFundAmount', e.target.value)}
required
/>
</FormGroup>
<FormGroup>
<Label>Matching Fund Price USD</Label>
<Input
value={record.matchingFundPriceUsd}
onChange={(e) => updateRecord(index, 'matchingFundPriceUsd', e.target.value)}
required
/>
</FormGroup>
<FormGroup>
<Label>Matching Fund Currency</Label>
<Select
options={[
{ value: 'ETH', label: 'ETH' },
{ value: 'DAI', label: 'DAI' },
{ value: 'USDC', label: 'USDC' },
]}
value={record.matchingFundCurrency}
onChange={(selected) => updateRecord(index, 'matchingFundCurrency', selected.value)}
required
/>
</FormGroup>
<FormGroup>
<Label>Distributed Fund Tx Hash</Label>
<Input
value={record.distributedFundTxHash}
onChange={(e) => updateRecord(index, 'distributedFundTxHash', e.target.value)}
/>
</FormGroup>
<FormGroup>
<Label>Distributed Fund Network</Label>
<Input
value={record.distributedFundNetwork}
onChange={(e) => updateRecord(index, 'distributedFundNetwork', e.target.value)}
/>
</FormGroup>
<FormGroup>
<Label>Distributed Fund Tx Date</Label>
<DatePicker
value={record.distributedFundTxDate}
onChange={(date) => updateRecord(index, 'distributedFundTxDate', date)}
/>
</FormGroup>
<Button onClick={() => removeRecord(index)} mt="default">Remove</Button>
</Box>
);

const CustomQfRoundMultiUpdateComponent = (props) => {
const [records, setRecords] = useState([{
projectId: '',
qfroundId: '',
matchingFundAmount: '',
matchingFundPriceUsd: '',
matchingFundCurrency: '',
distributedFundTxHash: '',
distributedFundNetwork: '',
distributedFundTxDate: null
}]);
const [message, setMessage] = useState('');

const api = new ApiClient();

const addRecord = () => {
setRecords([...records, {
projectId: '',
qfroundId: '',
matchingFundAmount: '',
matchingFundPriceUsd: '',
matchingFundCurrency: '',
distributedFundTxHash: '',
distributedFundNetwork: '',
distributedFundTxDate: null
}]);
};

const updateRecord = (index, field, value) => {
const updatedRecords = [...records];
updatedRecords[index][field] = value;
setRecords(updatedRecords);
};

const removeRecord = (index) => {
const updatedRecords = records.filter((_, i) => i !== index);
setRecords(updatedRecords);
};

const handleSubmit = async (event) => {
event.preventDefault();
setMessage('');

try {
const response = await api.resourceAction({
resourceId: 'QfRoundHistory',
actionName: 'bulkUpdate',
data: { records },
});

if (response.data.notice) {
if (typeof response.data.notice === 'string') {
setMessage(response.data.notice);
} else if (typeof response.data.notice.message === 'string') {
setMessage(response.data.notice.message);
} else {
setMessage('Update successful');
}
} else {
setMessage('Update successful');
}
} catch (error) {
setMessage(`Error: ${error.message}`);
}
};

return (
<Box as="form" onSubmit={handleSubmit}>
<Text variant="lg" fontWeight="bold">Update Multiple QfRoundHistory Records</Text>
{records.map((record, index) => (
<RecordInput
key={index}
index={index}
record={record}
updateRecord={updateRecord}
removeRecord={removeRecord}
/>
))}
<Button onClick={addRecord} mt="default">Add Another Record</Button>
<Button type="submit" mt="xl">Update All</Button>
{message && <Text mt="default">{message}</Text>}
</Box>
);
};

export default CustomQfRoundMultiUpdateComponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// components/CustomQfRoundReferenceListComponent.jsx
import React from 'react';
import { Link } from '@adminjs/design-system';

const CustomQfRoundReferenceListComponent = (props) => {
const { record } = props;
const qfRoundId = record.params.qfRound?.id || record.params.qfRoundId || 'N/A';
const href = `/admin/resources/QfRound/records/${qfRoundId}/show`;

return <Link href={href}>QF Round {qfRoundId}</Link>;
};

export default CustomQfRoundReferenceListComponent;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// components/CustomQfRoundReferenceShowComponent.jsx
import React from 'react';
import { Link, ValueGroup } from '@adminjs/design-system';

const CustomQfRoundReferenceShowComponent = (props) => {
const { record } = props;
const qfRoundId = record.params.qfRound?.id || record.params.qfRoundId || 'N/A';
const href = `/admin/resources/QfRound/records/${qfRoundId}/show`;

return (
<ValueGroup label="QF Round">
<Link href={href} style={{ color: 'inherit', textDecoration: 'none' }}>
{qfRoundId}
</Link>
</ValueGroup>
);
};

export default CustomQfRoundReferenceShowComponent;
95 changes: 90 additions & 5 deletions src/server/adminJs/tabs/qfRoundHistoryTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
canAccessQfRoundHistoryAction,
ResourceActions,
} from '../adminJsPermissions';
import adminJs from 'adminjs';

Check failure on line 5 in src/server/adminJs/tabs/qfRoundHistoryTab.ts

View workflow job for this annotation

GitHub Actions / test

`adminjs` import should occur before import of `../adminJsPermissions`

import { QfRoundHistory } from '../../../entities/qfRoundHistory';
import {
Expand Down Expand Up @@ -48,21 +49,53 @@ export const qfRoundHistoryTab = {
resource: QfRoundHistory,
options: {
properties: {
project: {
projectId: {
isVisible: {
list: false,
list: true,
edit: false,
filter: true,
show: true,
},
},
qfRound: {
reference: 'Project',
position: 100,

Check failure on line 60 in src/server/adminJs/tabs/qfRoundHistoryTab.ts

View workflow job for this annotation

GitHub Actions / test

Delete `····`
type: 'reference',

Check failure on line 61 in src/server/adminJs/tabs/qfRoundHistoryTab.ts

View workflow job for this annotation

GitHub Actions / test

Delete `····`
custom: {

Check failure on line 62 in src/server/adminJs/tabs/qfRoundHistoryTab.ts

View workflow job for this annotation

GitHub Actions / test

Replace `············` with `········`
getValue: (record) => {

Check failure on line 63 in src/server/adminJs/tabs/qfRoundHistoryTab.ts

View workflow job for this annotation

GitHub Actions / test

Replace `··············getValue:·(record)` with `··········getValue:·record`
return record.params.project?.id || record.params.projectId;

Check failure on line 64 in src/server/adminJs/tabs/qfRoundHistoryTab.ts

View workflow job for this annotation

GitHub Actions / test

Replace `················` with `············`
},

Check failure on line 65 in src/server/adminJs/tabs/qfRoundHistoryTab.ts

View workflow job for this annotation

GitHub Actions / test

Delete `····`
renderValue: (value, record) => {

Check failure on line 66 in src/server/adminJs/tabs/qfRoundHistoryTab.ts

View workflow job for this annotation

GitHub Actions / test

Replace `··············` with `··········`

Check failure on line 66 in src/server/adminJs/tabs/qfRoundHistoryTab.ts

View workflow job for this annotation

GitHub Actions / test

'record' is defined but never used. Allowed unused args must match /^_/u
return value ? `Project ${value}` : 'N/A';

Check failure on line 67 in src/server/adminJs/tabs/qfRoundHistoryTab.ts

View workflow job for this annotation

GitHub Actions / test

Delete `····`
},
},
components: {
list: adminJs.bundle('./components/CustomProjectReferenceComponent'),
show: adminJs.bundle('./components/CustomProjectReferenceShowComponent'),
filter: adminJs.bundle('./components/CustomIdFilterComponent'),
},
},
qfRoundId: {
isVisible: {
list: false,
list: true,
edit: false,
filter: true,
show: true,
},
reference: 'QfRound',
position: 101,
type: 'reference',
custom: {
getValue: (record) => {
return record.params.qfRound?.id || record.params.qfRoundId;
},
renderValue: (value, record) => {
return value ? `QF Round ${value}` : 'N/A';
},
},
components: {
list: adminJs.bundle('./components/CustomQfRoundReferenceComponent'),
show: adminJs.bundle('./components/CustomQfRoundReferenceShowComponent'),
filter: adminJs.bundle('./components/CustomIdFilterComponent'),
},
},
uniqueDonors: {
isVisible: true,
Expand Down Expand Up @@ -135,6 +168,58 @@ export const qfRoundHistoryTab = {
isAccessible: ({ currentAdmin }) =>
canAccessQfRoundHistoryAction({ currentAdmin }, ResourceActions.SHOW),
},
bulkUpdateQfRound: {
component: adminJs.bundle('./components/CustomQfRoundMultiUpdateComponent'),
handler: async (request, response, context) => {
const { records } = request.payload;
const results: string[] = [];

for (const record of records) {
const {
projectId,
qfRoundId,
matchingFundAmount,
matchingFundPriceUsd,
matchingFundCurrency,
distributedFundTxHash,
distributedFundNetwork,
distributedFundTxDate
} = record;

let existingRecord = await QfRoundHistory.findOne({
where: { projectId, qfRoundId }
});

const matchingFund = Number(matchingFundAmount);

if (existingRecord) {
await QfRoundHistory.createQueryBuilder()
.update(QfRoundHistory)
.set({
matchingFund,
matchingFundAmount,
matchingFundPriceUsd,
matchingFundCurrency,
distributedFundTxHash,
distributedFundNetwork,
distributedFundTxDate: new Date(distributedFundTxDate)
})
.where("id = :id", { id: existingRecord.id })
.execute();
results.push(`Updated: Project ${projectId}, Round ${qfRoundId}, Matching Fund: ${matchingFund}`);
} else {
results.push(`Project QfRoundHistory Not found for Project ${projectId}, Round ${qfRoundId}.`);
}
}

return {
notice: {
message: `Operations completed:\n${results.join('\n')}`,
type: 'success',
},
};
},
},
updateQfRoundHistories: {
actionType: 'resource',
isVisible: true,
Expand Down

0 comments on commit 4498aa8

Please sign in to comment.