Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. import React, { FunctionComponent } from "react"
  2.  
  3. import {
  4. CommandBar,
  5. IContextualMenuItem,
  6. ContextualMenuItemType,
  7. SearchBox,
  8. Separator,
  9. Button,
  10. CommandBarButton,
  11. IIconProps,
  12. IconButton
  13. } from "office-ui-fabric-react"
  14. import {
  15. getCompanyDisplayName,
  16. IUserSetting
  17. } from "../../../../../datastore/entities/storage/static/userSettings"
  18. import { Connection } from "../../../../../api/securityApi"
  19. import ActionBar from "../../actionBar/ActionBar"
  20. import { useAppContext } from "../../../context"
  21. import { useDropzone } from "react-dropzone"
  22. import { exceptionHandler } from "../../../../../nCommon/telemetry"
  23. import { PhotonApi, CreateFile } from "../../../../../api/photonApi"
  24. import { PhotonApiClient } from "../../../../../api/photonApiClient"
  25. import { uploadBlobFile } from "../../../../../api/fetch"
  26. import { checkFileTypeBanned } from "../FileManager"
  27.  
  28. interface IProps {
  29. onRefresh: () => void
  30. onAddFile: () => void
  31. onCompanySwitch: (company: string) => void
  32. targetCompany: string
  33. onSearch: (input: string) => void
  34. onRejectFileType: () => void
  35. }
  36.  
  37. const onRenderVerticalDivider = (item: any): React.ReactNode => {
  38. return (
  39. <Separator
  40. vertical
  41. styles={{
  42. root: { marginTop: 6, marginBottom: 6 }
  43. }}
  44. />
  45. )
  46. }
  47.  
  48. const onUpload = async (file: CreateFile, binaryData: any) => {
  49. const api = new PhotonApiClient()
  50. const textResponse = await api.createFile(file)
  51. console.info(textResponse)
  52. const blob = new Blob([binaryData], {
  53. type: file.fileType
  54. })
  55. const response = await uploadBlobFile(
  56. textResponse.text,
  57. file.fileType,
  58. blob
  59. )
  60. console.info(response)
  61. }
  62.  
  63. const generateCommandBarOptions = (
  64. isTachyon: boolean,
  65. onRefresh: () => void,
  66. onAddFile: () => void,
  67. targetCompany: string,
  68. generateCompanyOptions: () => IContextualMenuItem[],
  69. onRenderSearchBar: (item: any) => React.ReactNode,
  70. onRenderUploadButton: (item: any) => React.ReactNode
  71. ): IContextualMenuItem[] => {
  72. const options: IContextualMenuItem[] = []
  73. options.push({
  74. key: "filter",
  75. name: "Filter",
  76. title: "Filter",
  77. onRender: onRenderSearchBar
  78. })
  79. options.push({
  80. key: "divider1",
  81. name: "",
  82. title: "",
  83. onRender: onRenderVerticalDivider
  84. })
  85. if (isTachyon) {
  86. options.push({
  87. key: "company",
  88. name: getCompanyDisplayName(targetCompany),
  89. title: "Switch Company",
  90. iconOnly: false,
  91. iconProps: {
  92. iconName: "Org"
  93. },
  94. split: false,
  95. subMenuProps: {
  96. items: generateCompanyOptions()
  97. }
  98. })
  99. }
  100. options.push(
  101. {
  102. key: "refresh",
  103. name: "Refresh",
  104. title: "Refresh",
  105. iconOnly: isTachyon,
  106. iconProps: {
  107. iconName: "Sync"
  108. },
  109. onClick: onRefresh
  110. },
  111. {
  112. key: "divider2",
  113. name: "",
  114. title: "",
  115. onRender: onRenderVerticalDivider
  116. },
  117. {
  118. key: "upload",
  119. name: "Upload",
  120. title: "upload",
  121. onRender: onRenderUploadButton
  122. }
  123. )
  124. return options
  125. }
  126.  
  127. const CmdBar: FunctionComponent<IProps> = ({
  128. onRefresh,
  129. onAddFile,
  130. onCompanySwitch,
  131. targetCompany,
  132. onSearch,
  133. onRejectFileType
  134. }) => {
  135. const ctx = useAppContext()
  136. const settings = ctx.settings
  137. const isTachyon = settings
  138. ? settings.baseCompany === Connection.Tachyus_com
  139. : false
  140. const switchCompany = (
  141. ev?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>,
  142. item?: IContextualMenuItem
  143. ) => {
  144. if (item) {
  145. console.info({ item })
  146. onCompanySwitch(item.key)
  147. }
  148. }
  149.  
  150. const onDrop = (acceptedFiles: any) => {
  151. if (checkFileTypeBanned(acceptedFiles)) {
  152. onRejectFileType()
  153. return
  154. } else {
  155. const reader = new FileReader()
  156. reader.onabort = () => console.info("file reading was aborted")
  157. reader.onerror = () => console.info("file reading has failed")
  158. reader.onload = () => {
  159. const binaryStr = reader.result
  160. if (binaryStr) {
  161. const file = new CreateFile({
  162. name: acceptedFiles[0].name,
  163. fileType: acceptedFiles[0].type
  164. })
  165. try {
  166. console.info(reader.result)
  167. console.info({ binaryStr })
  168. onUpload(file, binaryStr)
  169. // setState({...state,
  170. // showPanel: true
  171. // })
  172. } catch (e) {
  173. exceptionHandler(e)
  174. }
  175. }
  176. }
  177. console.info({ acceptedFiles })
  178. acceptedFiles.forEach((file: any) =>
  179. reader.readAsBinaryString(file)
  180. )
  181. }
  182. }
  183. const { getRootProps, getInputProps } = useDropzone({
  184. onDrop
  185. })
  186.  
  187. const onRenderSearchBar = (item: any): React.ReactNode => {
  188. return (
  189. <SearchBox
  190. styles={{ root: { width: 200, marginTop: 5, marginRight: 5 } }}
  191. placeholder="Search"
  192. onEscape={ev => onSearch("")}
  193. onClear={ev => onSearch("")}
  194. onSearch={newValue => onSearch(newValue)}
  195. />
  196. )
  197. }
  198.  
  199. const uploadIcon: IIconProps = { iconName: "PageAdd" }
  200. const onRenderUploadButton = (item: any): React.ReactNode => {
  201. return (
  202. <div
  203. {...getRootProps({
  204. // onClick: event => console.log(event)
  205. })}
  206. >
  207. <input
  208. {...getInputProps({
  209. onClick: event => console.log(event)
  210. // problem is onClick in getInputProps only fires the first time
  211. })}
  212. />
  213. <CommandBarButton
  214. styles={{
  215. root: { marginTop: 15, marginRight: 5 }
  216. }}
  217. iconProps={uploadIcon}
  218. text="Upload"
  219. />
  220. </div>
  221. )
  222. }
  223.  
  224. const renderCompanyOption = (company: string): IContextualMenuItem => {
  225. return {
  226. key: company,
  227. text: getCompanyDisplayName(company),
  228. checked: company === targetCompany,
  229. onClick: switchCompany
  230. }
  231. }
  232.  
  233. const generateCompanyOptions = (): IContextualMenuItem[] => {
  234. return [
  235. renderCompanyOption(Connection.Tachyus_com),
  236. {
  237. key: "divider1",
  238. text: "",
  239. itemType: ContextualMenuItemType.Divider
  240. },
  241. renderCompanyOption(Connection.Seneca),
  242. renderCompanyOption(Connection.Vaquero),
  243. {
  244. key: "divider2",
  245. text: "",
  246. itemType: ContextualMenuItemType.Divider
  247. },
  248. renderCompanyOption(Connection.Pae),
  249. renderCompanyOption(Connection.Vista),
  250. renderCompanyOption(Connection.Ypf),
  251. {
  252. key: "divider3",
  253. text: "",
  254. itemType: ContextualMenuItemType.Divider
  255. },
  256. renderCompanyOption(Connection.Pttep),
  257. renderCompanyOption(Connection.Equinor)
  258. ]
  259. }
  260.  
  261. return (
  262. <div>
  263. <ActionBar>
  264. <CommandBar
  265. items={generateCommandBarOptions(
  266. isTachyon,
  267. onRefresh,
  268. onAddFile,
  269. targetCompany,
  270. generateCompanyOptions,
  271. onRenderSearchBar,
  272. onRenderUploadButton
  273. )}
  274. />
  275. </ActionBar>
  276. </div>
  277. )
  278. }
  279.  
  280. export default CmdBar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement