|
| 1 | +import styled from '@emotion/styled' |
| 2 | +import { Button } from 'components/common/Button/Button' |
| 3 | +import { Hug } from 'components/common/Hug/Hug' |
| 4 | +import { Tooltip } from 'components/common/Tooltip/Tooltip' |
| 5 | +import { useState } from 'react' |
| 6 | +import { template } from '../../../services/template' |
| 7 | + |
| 8 | +type TemplateDataPrefetcherProps = { |
| 9 | + templateRepository: string, |
| 10 | + templateVersion: string, |
| 11 | + children: React.ReactNode, |
| 12 | + onFetchSuccess?: (data: Record<string, string>) => void |
| 13 | +}; |
| 14 | + |
| 15 | +const PreFetchWrapper = styled.div(({ |
| 16 | + height: '100%', |
| 17 | + position: 'relative' |
| 18 | +})) |
| 19 | + |
| 20 | +const PreFetchBoxWrapper = styled.div(({ |
| 21 | + height: '100%', |
| 22 | + position: 'absolute', |
| 23 | + top: 0, |
| 24 | + left: 0, |
| 25 | + width: '100%', |
| 26 | + zIndex: 100, |
| 27 | + display: 'flex', |
| 28 | + justifyContent: 'center', |
| 29 | + alignItems: 'center', |
| 30 | + backgroundColor: 'rgba(165, 165, 165, 0.6)' |
| 31 | +})) |
| 32 | + |
| 33 | +const PreFetchBox = styled.div(({ |
| 34 | + backgroundColor: 'rgba(49, 49, 49, 0.6)', |
| 35 | + width: '80%', |
| 36 | + minHeight: '60%', |
| 37 | + maxHeight: '85%', |
| 38 | + padding: '1rem', |
| 39 | + borderRadius: '.8rem', |
| 40 | + display: 'flex', |
| 41 | + flexDirection: 'column', |
| 42 | + justifyContent: 'center', |
| 43 | + alignItems: 'center', |
| 44 | + color: 'white', |
| 45 | + boxShadow: '0 0 5px rgba(0, 0, 0, 0.2)' |
| 46 | +})) |
| 47 | + |
| 48 | +const TemplateDataPrefetcherTitle = styled.h1(({ |
| 49 | + textAlign: 'center' |
| 50 | +})) |
| 51 | + |
| 52 | +const TemplateDataPrefetcherContent = styled.div(({ |
| 53 | + padding: '.5rem', |
| 54 | + textAlign: 'center' })) |
| 55 | + |
| 56 | +const TemplateDataPrefetcherActions = styled.div(({ |
| 57 | + display: 'flex', |
| 58 | + justifyContent: 'center', |
| 59 | + padding: '.5rem' |
| 60 | +})) |
| 61 | + |
| 62 | +const MagicButton = styled.button(({ |
| 63 | + position: 'absolute', |
| 64 | + bottom: '.8rem', |
| 65 | + right: '.8rem', |
| 66 | + width: '2.5rem', |
| 67 | + height: '2.5rem', |
| 68 | + fontSize: '1.5rem', |
| 69 | + borderRadius: '50%', |
| 70 | + border: '1px solid rgb(137, 137, 137)', |
| 71 | + cursor: 'pointer', |
| 72 | + padding: 0, |
| 73 | + paddingBottom: '.2rem', |
| 74 | + paddingLeft: '.2rem', |
| 75 | + backgroundColor: 'rgba(255, 255, 255, 0.15)', |
| 76 | + |
| 77 | + '&:hover': { |
| 78 | + backgroundColor: 'rgba(255, 255, 255, 0.2)' |
| 79 | + } |
| 80 | + |
| 81 | +})) |
| 82 | + |
| 83 | +export const TemplateDataPrefetcher = ({ templateRepository, templateVersion, children, onFetchSuccess }: TemplateDataPrefetcherProps) => { |
| 84 | + const [isActivated, setIsActivated] = useState(false) |
| 85 | + const [isLoading, setIsLoading] = useState(false) |
| 86 | + |
| 87 | + const fetchData = async () => { |
| 88 | + if (onFetchSuccess) { |
| 89 | + setIsLoading(true) |
| 90 | + const data = await template.getDefaultVariables(templateRepository, templateVersion) |
| 91 | + setIsLoading(false) |
| 92 | + setIsActivated(false) |
| 93 | + onFetchSuccess(data) |
| 94 | + |
| 95 | + // TODO implement error handeling, if the fetch fails |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + const missingFields = [] |
| 100 | + if (!templateRepository) { |
| 101 | + missingFields.push('Template repository') |
| 102 | + } |
| 103 | + if (!templateVersion) { |
| 104 | + missingFields.push('Template version') |
| 105 | + } |
| 106 | + |
| 107 | + return isActivated ? ( |
| 108 | + <PreFetchWrapper> |
| 109 | + <PreFetchBoxWrapper> |
| 110 | + <PreFetchBox> |
| 111 | + <TemplateDataPrefetcherTitle>Automatically populate the template data</TemplateDataPrefetcherTitle> |
| 112 | + <TemplateDataPrefetcherContent> |
| 113 | + You can automatically prefetch the data from the repository, which you have provided. |
| 114 | + </TemplateDataPrefetcherContent> |
| 115 | + <TemplateDataPrefetcherContent> |
| 116 | + If you would wish to do so, click the button "Prefetch data" below. This will populate the template data with the data found in the repository. |
| 117 | + </TemplateDataPrefetcherContent> |
| 118 | + <TemplateDataPrefetcherContent> |
| 119 | + <strong>Note: This will overwrite the current template data. And is not reversible.</strong> |
| 120 | + </TemplateDataPrefetcherContent> |
| 121 | + <TemplateDataPrefetcherActions style={{ height: '2rem' }}> |
| 122 | + {missingFields.length > 0 && ( |
| 123 | + <span style={{ color: '#fc2121', margin: 0 }}> |
| 124 | + Please fill in the missing data: <strong>{missingFields.join(', ')}</strong> |
| 125 | + </span> |
| 126 | + )} |
| 127 | + </TemplateDataPrefetcherActions> |
| 128 | + |
| 129 | + <TemplateDataPrefetcherActions> |
| 130 | + <Hug m=".5rem"> |
| 131 | + <Tooltip title="Fetch data"> |
| 132 | + <Button loading={isLoading} minWidth="9.5rem" disabled={(!templateRepository || !templateVersion) || isLoading} type="button" onClick={fetchData}>Prefetch data</Button> |
| 133 | + </Tooltip> |
| 134 | + </Hug><Hug m=".5rem"> |
| 135 | + <Button variant="warning" minWidth="9.5rem" type="button" disabled={isLoading} onClick={() => setIsActivated(false)}>Continue Editing</Button> |
| 136 | + </Hug> |
| 137 | + </TemplateDataPrefetcherActions> |
| 138 | + </PreFetchBox> |
| 139 | + </PreFetchBoxWrapper> |
| 140 | + {children} |
| 141 | + </PreFetchWrapper>) : ( |
| 142 | + <PreFetchWrapper> |
| 143 | + {children} |
| 144 | + <MagicButton type="button" onClick={() => setIsActivated(true)}>🪄</MagicButton> |
| 145 | + </PreFetchWrapper> |
| 146 | + ) |
| 147 | +} |
0 commit comments