File.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import styled from 'styled-components'
  2. import { endpoint } from '../config'
  3. const FileStyle = styled.div`
  4. display: grid;
  5. grid-template-columns: 250px 100px 1fr;
  6. grid-auto-flow: dense;
  7. align-items: start;
  8. justify-items: start;
  9. align-content: start;
  10. justify-content: start;
  11. a,
  12. img {
  13. grid-row: span 6;
  14. max-width: 230px;
  15. max-height: 140px;
  16. justify-self: center;
  17. }
  18. h2 {
  19. grid-column: span 2;
  20. margin: 0 0 0.5em 0;
  21. line-height: 1;
  22. }
  23. .description {
  24. grid-column: span 2;
  25. }
  26. p {
  27. margin: 0;
  28. line-height: 1.5;
  29. }
  30. `
  31. const FileFields = {
  32. id: null,
  33. name: '',
  34. description: '',
  35. file: null
  36. }
  37. const FileFormFields = props => {
  38. const { state, onChange } = props
  39. const toState = event => {
  40. if (event.target.type === 'file') {
  41. onChange({ ...state, [event.target.name]: event.target.files[0] })
  42. } else {
  43. onChange({ ...state, [event.target.name]: event.target.value })
  44. }
  45. }
  46. return (
  47. <fieldset>
  48. <label htmlFor='name'>Name</label>
  49. <input
  50. type='text'
  51. name='name'
  52. id='name'
  53. placeholder='Name'
  54. value={state.name}
  55. onChange={toState}
  56. />
  57. <label htmlFor='description'>Description</label>
  58. <textarea
  59. name='description'
  60. id='description'
  61. placeholder='Description'
  62. value={state.description}
  63. onChange={toState}
  64. />
  65. <input type='file' required name='file' onChange={toState} />
  66. </fieldset>
  67. )
  68. }
  69. const File = props => {
  70. const { path, name, description, filename, mimetype, size } = props.data
  71. const downloadPath = `${endpoint}/${path}`
  72. return (
  73. <FileStyle>
  74. {mimetype.startsWith('image/') ? (
  75. <img src={downloadPath} alt={description} />
  76. ) : (
  77. <a href={downloadPath}>
  78. <img
  79. src={`${endpoint}/static/document-download-solid.png`}
  80. alt={description}
  81. width={75}
  82. />
  83. </a>
  84. )}
  85. <h2>{name}</h2>
  86. <p className='description'>{description}</p>
  87. <p>Path</p>
  88. <p>
  89. <a href={downloadPath}>{downloadPath}</a>
  90. </p>
  91. <p>Filename</p>
  92. <p>{filename}</p>
  93. <p>MIME Type</p>
  94. <p>{mimetype}</p>
  95. <p>Size</p>
  96. <p>{size}</p>
  97. </FileStyle>
  98. )
  99. }
  100. export default File
  101. export { FileFormFields, FileFields }