ApplicationDetails.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, { useState } from 'react';
  2. import { ParsedApplication } from '../../../types/workingGroups';
  3. import { ProfilePreviewFromStruct as MemberPreview } from '../../../MemberProfilePreview';
  4. import { useTransport, usePromise } from '../../hooks';
  5. import { Item, Label, Button } from 'semantic-ui-react';
  6. import { formatBalance } from '@polkadot/util';
  7. import { WorkingGroupKey } from '@joystream/types/common';
  8. import PromiseComponent from '../PromiseComponent';
  9. type ApplicationsDetailsProps = {
  10. applications: ParsedApplication[];
  11. acceptedIds?: number[];
  12. }
  13. export const ApplicationsDetails = ({ applications, acceptedIds }: ApplicationsDetailsProps) => {
  14. const rejectedApplications = acceptedIds !== undefined ? applications.filter(a => !acceptedIds.includes(a.wgApplicationId)) : [];
  15. const [showAll, setShowAll] = useState(!rejectedApplications.length);
  16. const shownApplications = applications.filter(a => showAll || acceptedIds?.includes(a.wgApplicationId));
  17. return (<>
  18. <Item.Group>
  19. {
  20. shownApplications.map(({ member, stakes, wgApplicationId, humanReadableText }) => {
  21. let HRT = humanReadableText.toString();
  22. const accepted = acceptedIds?.includes(wgApplicationId);
  23. try { HRT = JSON.stringify(JSON.parse(HRT), undefined, 4); } catch (e) { /* Do nothing */ }
  24. return (
  25. <Item key={wgApplicationId} style={{
  26. background: 'white',
  27. padding: '1em 1.5em',
  28. boxShadow: `0 0 0.5rem 1px ${accepted ? '#21ba45' : (acceptedIds !== undefined ? '#db282899' : '#00000050')}`
  29. }}>
  30. <Item.Content>
  31. <Item.Header><MemberPreview profile={member}></MemberPreview></Item.Header>
  32. <Item.Meta>
  33. <Label>Application id: {wgApplicationId}</Label>
  34. { stakes.application > 0 && <Label>Appl. stake: {formatBalance(stakes.application)}</Label> }
  35. { stakes.role > 0 && <Label>Role stake: {formatBalance(stakes.role)}</Label> }
  36. </Item.Meta>
  37. <Item.Description>
  38. <pre style={{
  39. whiteSpace: 'pre-wrap',
  40. fontWeight: 'normal'
  41. }}>
  42. {HRT}
  43. </pre>
  44. </Item.Description>
  45. </Item.Content>
  46. </Item>
  47. );
  48. })
  49. }
  50. </Item.Group>
  51. {rejectedApplications.length > 0 && (
  52. <Button fluid onClick={() => setShowAll(current => !current)}>
  53. { showAll ? 'Hide rejected applications' : 'Show rejected applications' }
  54. </Button>
  55. )}
  56. </>);
  57. };
  58. type ApplicationsDetailsByIdsProps = {
  59. group: WorkingGroupKey;
  60. ids: number[];
  61. acceptedIds?: number[];
  62. };
  63. export const ApplicationsDetailsByIds = ({ group, ids, acceptedIds }: ApplicationsDetailsByIdsProps) => {
  64. const transport = useTransport();
  65. const [applications, error, loading] = usePromise<ParsedApplication[]>(
  66. () => Promise.all(ids.map(id => transport.workingGroups.parsedApplicationById(group, id))),
  67. [],
  68. [ids]
  69. );
  70. return (
  71. <PromiseComponent {...{ error, loading }} message="Fetching application(s)...">
  72. <ApplicationsDetails applications={applications} acceptedIds={acceptedIds}/>
  73. </PromiseComponent>
  74. );
  75. };
  76. type ApplicationsDetailsByOpeningProps = {
  77. group: WorkingGroupKey;
  78. openingId: number;
  79. acceptedIds?: number[];
  80. };
  81. export const ApplicationsDetailsByOpening = ({ group, openingId, acceptedIds }: ApplicationsDetailsByOpeningProps) => {
  82. const transport = useTransport();
  83. const [applications, error, loading] = usePromise<ParsedApplication[]>(
  84. // Cannot filter by active, otherwise the details will be broken once opening is filled!
  85. () => transport.workingGroups.openingApplications(group, openingId),
  86. [],
  87. [openingId]
  88. );
  89. return (
  90. <PromiseComponent {...{ error, loading }} message="Fetching applications...">
  91. <ApplicationsDetails applications={applications} acceptedIds={acceptedIds}/>
  92. </PromiseComponent>
  93. );
  94. };