Channel.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import * as Yup from 'yup';
  2. import { BlockNumber, AccountId } from '@polkadot/types/interfaces';
  3. import { ChannelContentTypeValue, PrincipalId, Channel, ChannelId, ChannelPublicationStatusValue, ChannelCurationStatusValue } from '@joystream/types/content-working-group';
  4. import { MemberId } from '@joystream/types/members';
  5. import { ChannelValidationConstraints } from '@polkadot/joy-media/transport';
  6. import { ValidationConstraint } from '@polkadot/joy-utils/ValidationConstraint';
  7. function textValidation (constraint?: ValidationConstraint) {
  8. if (!constraint) {
  9. return Yup.string();
  10. }
  11. const { min, max } = constraint;
  12. return Yup.string()
  13. .min(min, `Text is too short. Minimum length is ${min} chars.`)
  14. .max(max, `Text is too long. Maximum length is ${max} chars.`);
  15. }
  16. export const buildChannelValidationSchema = (constraints?: ChannelValidationConstraints) =>
  17. Yup.object().shape({
  18. handle: textValidation(constraints?.handle).required('This field is required'),
  19. title: textValidation(constraints?.title),
  20. description: textValidation(constraints?.description),
  21. avatar: textValidation(constraints?.avatar),
  22. banner: textValidation(constraints?.banner)
  23. });
  24. export type ChannelFormValues = {
  25. content: ChannelContentTypeValue;
  26. handle: string;
  27. title: string;
  28. description: string;
  29. avatar: string;
  30. banner: string;
  31. publicationStatus: ChannelPublicationStatusValue;
  32. };
  33. export type ChannelType = {
  34. id: number;
  35. verified: boolean;
  36. handle: string;
  37. title?: string;
  38. description?: string;
  39. avatar?: string;
  40. banner?: string;
  41. content: ChannelContentTypeValue;
  42. owner: MemberId;
  43. roleAccount: AccountId;
  44. publicationStatus: ChannelPublicationStatusValue;
  45. curationStatus: ChannelCurationStatusValue;
  46. created: BlockNumber;
  47. principalId: PrincipalId;
  48. };
  49. export class ChannelCodec {
  50. static fromSubstrate (id: ChannelId, sub: Channel): ChannelType {
  51. return {
  52. id: id.toNumber(),
  53. verified: sub.getBoolean('verified'),
  54. handle: sub.getString('handle'),
  55. title: sub.getOptionalString('title'),
  56. description: sub.getOptionalString('description'),
  57. avatar: sub.getOptionalString('avatar'),
  58. banner: sub.getOptionalString('banner'),
  59. content: sub.getEnumAsString<ChannelContentTypeValue>('content'),
  60. owner: sub.getField('owner'),
  61. roleAccount: sub.getField('role_account'),
  62. publicationStatus: sub.getEnumAsString<ChannelPublicationStatusValue>('publication_status'),
  63. curationStatus: sub.getEnumAsString<ChannelCurationStatusValue>('curation_status'),
  64. created: sub.getField('created'),
  65. principalId: sub.getField('principal_id')
  66. };
  67. }
  68. }
  69. export function ChannelToFormValues (entity?: ChannelType): ChannelFormValues {
  70. return {
  71. content: (entity && entity.content) || 'Video',
  72. handle: (entity && entity.handle) || '',
  73. title: (entity && entity.title) || '',
  74. description: (entity && entity.description) || '',
  75. avatar: (entity && entity.avatar) || '',
  76. banner: (entity && entity.banner) || '',
  77. publicationStatus: (entity && entity.publicationStatus) || 'Public'
  78. };
  79. }
  80. export type ChannelPropId =
  81. 'content' |
  82. 'handle' |
  83. 'title' |
  84. 'description' |
  85. 'avatar' |
  86. 'banner' |
  87. 'publicationStatus'
  88. ;
  89. export type ChannelGenericProp = {
  90. id: ChannelPropId;
  91. type: string;
  92. name: string;
  93. description?: string;
  94. required?: boolean;
  95. maxItems?: number;
  96. maxTextLength?: number;
  97. classId?: any;
  98. };
  99. type ChannelClassType = {
  100. [id in ChannelPropId]: ChannelGenericProp
  101. };
  102. export const ChannelClass: ChannelClassType = {
  103. content: {
  104. id: 'content',
  105. name: 'Content',
  106. description: 'The type of channel.',
  107. type: 'Text',
  108. required: true,
  109. maxTextLength: 100
  110. },
  111. handle: {
  112. id: 'handle',
  113. name: 'Handle',
  114. description: 'Unique URL handle of channel.',
  115. type: 'Text',
  116. required: true,
  117. maxTextLength: 40
  118. },
  119. title: {
  120. id: 'title',
  121. name: 'Title',
  122. description: 'Human readable title of channel.',
  123. type: 'Text',
  124. maxTextLength: 100
  125. },
  126. description: {
  127. id: 'description',
  128. name: 'Description',
  129. description: 'Human readable description of channel purpose and scope.',
  130. type: 'Text',
  131. maxTextLength: 4000
  132. },
  133. avatar: {
  134. id: 'avatar',
  135. name: 'Avatar',
  136. description: 'URL to avatar (logo) iamge: NOTE: Should be an https link to a square image.',
  137. type: 'Text',
  138. maxTextLength: 1000
  139. },
  140. banner: {
  141. id: 'banner',
  142. name: 'Banner',
  143. description: 'URL to banner image: NOTE: Should be an https link to a rectangular image, between 1400x1400 and 3000x3000 pixels, in JPEG or PNG format.',
  144. type: 'Text',
  145. maxTextLength: 1000
  146. },
  147. publicationStatus: {
  148. id: 'publicationStatus',
  149. name: 'Publication Status',
  150. description: 'The publication status of the channel.',
  151. required: true,
  152. type: 'Internal',
  153. classId: 'Publication Status'
  154. }
  155. };