|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import Image from 'next/image'; |
| 5 | +import { graphql } from '@/gql'; |
| 6 | +import { ResourceChartImageInputPartial } from '@/gql/generated/graphql'; |
| 7 | +import { useMutation, useQuery } from '@tanstack/react-query'; |
| 8 | +import { Button, Text, toast } from 'opub-ui'; |
| 9 | + |
| 10 | +import { GraphQL } from '@/lib/api'; |
| 11 | +import { Loading } from '@/components/loading'; |
| 12 | +import TitleBar from '../../../components/title-bar'; |
| 13 | + |
| 14 | +const getResourceChartImageDetailsDoc: any = graphql(` |
| 15 | + query getResourceChartImageDetails($imageId: UUID!) { |
| 16 | + resourceChartImage(imageId: $imageId) { |
| 17 | + description |
| 18 | + dataset { |
| 19 | + id |
| 20 | + title |
| 21 | + slug |
| 22 | + } |
| 23 | + id |
| 24 | + name |
| 25 | + image { |
| 26 | + name |
| 27 | + path |
| 28 | + size |
| 29 | + url |
| 30 | + width |
| 31 | + height |
| 32 | + } |
| 33 | + status |
| 34 | + } |
| 35 | + } |
| 36 | +`); |
| 37 | + |
| 38 | +const updateResourceChartImageDoc: any = graphql(` |
| 39 | + mutation updateResourceCHartImage($input: ResourceChartImageInputPartial!) { |
| 40 | + updateResourceChartImage(input: $input) { |
| 41 | + __typename |
| 42 | + ... on TypeResourceChartImage { |
| 43 | + id |
| 44 | + name |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +`); |
| 49 | + |
| 50 | +const publishResourceChartImageDoc: any = graphql(` |
| 51 | + mutation publishResourceChartImage($resourceChartImageId: UUID!) { |
| 52 | + publishResourceChartImage(resourceChartImageId: $resourceChartImageId) |
| 53 | + } |
| 54 | +`); |
| 55 | + |
| 56 | +/** |
| 57 | + * Renders a page for chart image preview. |
| 58 | + * |
| 59 | + * @param {{ params: { entityType: string, entitySlug: string, chartID: string } }} props |
| 60 | + * @returns {JSX.Element} |
| 61 | + */ |
| 62 | +const ChartImagePreview = ({ params }: { params: any }) => { |
| 63 | + const getResourceChartDetailsRes: { |
| 64 | + data: any; |
| 65 | + isLoading: boolean; |
| 66 | + refetch: any; |
| 67 | + error: any; |
| 68 | + isError: boolean; |
| 69 | + } = useQuery([`getResourceChartImageDetails_${params.chartID}`], () => |
| 70 | + GraphQL( |
| 71 | + getResourceChartImageDetailsDoc, |
| 72 | + { |
| 73 | + [params.entityType]: params.entitySlug, |
| 74 | + }, |
| 75 | + { |
| 76 | + imageId: params.chartID, |
| 77 | + } |
| 78 | + ) |
| 79 | + ); |
| 80 | + |
| 81 | + const [chartTitle, setChartTitle] = useState(''); |
| 82 | + |
| 83 | + useEffect(() => { |
| 84 | + setChartTitle(getResourceChartDetailsRes?.data?.resourceChartImage?.name); |
| 85 | + }, [getResourceChartDetailsRes?.data]); |
| 86 | + |
| 87 | + const updateResourceChartImageMutation: { mutate: any; isLoading: any } = |
| 88 | + useMutation( |
| 89 | + (data: { input: ResourceChartImageInputPartial }) => |
| 90 | + GraphQL( |
| 91 | + updateResourceChartImageDoc, |
| 92 | + { |
| 93 | + [params.entityType]: params.entitySlug, |
| 94 | + }, |
| 95 | + data |
| 96 | + ), |
| 97 | + { |
| 98 | + onSuccess: () => { |
| 99 | + toast('ChartImage Updated Successfully'); |
| 100 | + getResourceChartDetailsRes.refetch(); |
| 101 | + }, |
| 102 | + onError: (err: any) => { |
| 103 | + toast(`Received ${err} while updating chart `); |
| 104 | + }, |
| 105 | + } |
| 106 | + ); |
| 107 | + |
| 108 | + const publishResourceChartImageMutation: { mutate: any; isLoading: any } = |
| 109 | + useMutation( |
| 110 | + (data: { resourceChartImageId: string }) => |
| 111 | + GraphQL( |
| 112 | + publishResourceChartImageDoc, |
| 113 | + { |
| 114 | + [params.entityType]: params.entitySlug, |
| 115 | + }, |
| 116 | + data |
| 117 | + ), |
| 118 | + { |
| 119 | + onSuccess: () => { |
| 120 | + toast('Chart Image Published Successfully'); |
| 121 | + getResourceChartDetailsRes.refetch(); |
| 122 | + }, |
| 123 | + onError: (err: any) => { |
| 124 | + toast(`Received ${err} while publishing chart `); |
| 125 | + }, |
| 126 | + } |
| 127 | + ); |
| 128 | + |
| 129 | + return ( |
| 130 | + <div> |
| 131 | + <TitleBar |
| 132 | + label={'CHART NAME'} |
| 133 | + title={chartTitle} |
| 134 | + goBackURL={`/dashboard/${params.entityType}/${params.entitySlug}/charts`} |
| 135 | + onSave={(val: any) => { |
| 136 | + console.log(val); |
| 137 | + updateResourceChartImageMutation.mutate({ |
| 138 | + input: { |
| 139 | + id: params.chartID, |
| 140 | + dataset: |
| 141 | + getResourceChartDetailsRes?.data?.resourceChartImage?.dataset |
| 142 | + ?.id, |
| 143 | + name: val, |
| 144 | + }, |
| 145 | + }); |
| 146 | + }} |
| 147 | + loading={getResourceChartDetailsRes.isLoading} |
| 148 | + status={ |
| 149 | + updateResourceChartImageMutation.isLoading ? 'loading' : 'success' |
| 150 | + } |
| 151 | + setStatus={() => {}} |
| 152 | + /> |
| 153 | + |
| 154 | + {getResourceChartDetailsRes?.isError ? ( |
| 155 | + <div> |
| 156 | + <Text variant="heading2xl">Something went wrong</Text> |
| 157 | + </div> |
| 158 | + ) : getResourceChartDetailsRes?.isLoading ? ( |
| 159 | + <Loading /> |
| 160 | + ) : ( |
| 161 | + <div className="border-t-2 border-solid border-greyExtralight pt-8"> |
| 162 | + <div className="flex flex-col items-center justify-center gap-4"> |
| 163 | + <div className="relative aspect-video w-full max-w-5xl"> |
| 164 | + <Image |
| 165 | + src={ |
| 166 | + process.env.NEXT_PUBLIC_BACKEND_URL + |
| 167 | + getResourceChartDetailsRes?.data?.resourceChartImage?.image |
| 168 | + ?.url |
| 169 | + } |
| 170 | + alt={getResourceChartDetailsRes?.data?.resourceChartImage?.name} |
| 171 | + // width={ |
| 172 | + // getResourceChartDetailsRes?.data?.resourceChartImage?.image |
| 173 | + // ?.width |
| 174 | + // } |
| 175 | + // height={ |
| 176 | + // getResourceChartDetailsRes?.data?.resourceChartImage?.image |
| 177 | + // ?.height |
| 178 | + // } |
| 179 | + fill |
| 180 | + className="rounded-4 border-1 border-solid border-greyExtralight object-contain" |
| 181 | + /> |
| 182 | + </div> |
| 183 | + |
| 184 | + {getResourceChartDetailsRes?.data?.resourceChartImage?.status === |
| 185 | + 'DRAFT' ? ( |
| 186 | + <Button |
| 187 | + kind="primary" |
| 188 | + onClick={() => { |
| 189 | + publishResourceChartImageMutation.mutate({ |
| 190 | + resourceChartImageId: params.chartID, |
| 191 | + }); |
| 192 | + }} |
| 193 | + > |
| 194 | + Publish Chart |
| 195 | + </Button> |
| 196 | + ) : getResourceChartDetailsRes?.data?.resourceChartImage?.status === |
| 197 | + 'PUBLISHED' ? ( |
| 198 | + <Button |
| 199 | + kind="primary" |
| 200 | + onClick={() => { |
| 201 | + console.log('unpublish'); |
| 202 | + }} |
| 203 | + > |
| 204 | + UnPublish Chart |
| 205 | + </Button> |
| 206 | + ) : ( |
| 207 | + <Button kind="primary" disabled> |
| 208 | + Publish Chart |
| 209 | + </Button> |
| 210 | + )} |
| 211 | + </div> |
| 212 | + </div> |
| 213 | + )} |
| 214 | + </div> |
| 215 | + ); |
| 216 | +}; |
| 217 | + |
| 218 | +export default ChartImagePreview; |
0 commit comments