|
1 | 1 | import { CircleBackslashIcon, Cross1Icon, PlusIcon } from "@radix-ui/react-icons";
|
2 |
| -import { Box, Button, Callout, Dialog, Flex, Grid, IconButton, Text, TextField } from "@radix-ui/themes"; |
| 2 | +import { Box, Button, Callout, Dialog, Flex, Grid, IconButton, Spinner, Text, TextField } from "@radix-ui/themes"; |
3 | 3 | import React, { useMemo } from "react";
|
4 | 4 | import { useCreateOfferDialog } from "./CreateOfferDialog.logic";
|
5 | 5 | import { IOfferType } from "@/types/IOfferType/IOfferType";
|
6 | 6 | import { Controller } from "react-hook-form";
|
7 | 7 | import { IUnit } from "@/types/IUnit/IUnit";
|
| 8 | +import { formatTIG } from "../../utils/formatTIG"; |
8 | 9 |
|
9 | 10 | const CreateOfferDialog = () => {
|
10 | 11 | const logic = useCreateOfferDialog();
|
@@ -33,110 +34,122 @@ const CreateOfferDialog = () => {
|
33 | 34 | <Dialog.Description size="2" color="gray" mb="5">
|
34 | 35 | {UI.description}
|
35 | 36 | </Dialog.Description>
|
36 |
| - <Flex> |
37 |
| - <form action="" style={{ width: "100%" }}> |
38 |
| - <Flex mt="4" style={{ flexFlow: "column" }}> |
39 |
| - <Flex width="100%" style={{ flexFlow: "column" }}> |
40 |
| - <Flex align="center" justify="between" mb="1"> |
41 |
| - <Text as="label" size="2" weight="regular" color="gray"> |
42 |
| - Amount of tig (required) |
43 |
| - </Text> |
44 |
| - <Text as="span" size="2" weight="regular" color="gray"> |
45 |
| - Balance: {logic.balance} |
46 |
| - <span style={{ fontSize: ".6rem" }}>{IUnit.TIG}</span> |
47 |
| - </Text> |
48 |
| - </Flex> |
49 |
| - <Box mb="4"> |
50 |
| - <Controller |
51 |
| - name="quantity" |
52 |
| - rules={{ required: true, validate: (field) => logic.hasEnoughtTIG(field) }} |
53 |
| - control={logic.control} |
54 |
| - render={({ field, fieldState }) => ( |
55 |
| - <> |
56 |
| - <TextField.Root |
57 |
| - size="3" |
58 |
| - type="number" |
59 |
| - style={{ paddingLeft: "0" }} |
60 |
| - onChange={(e) => logic.fieldUpdate("quantity", e, field.onChange)} |
61 |
| - value={field.value} |
62 |
| - > |
63 |
| - <TextField.Slot style={{ paddingLeft: "0" }}></TextField.Slot> |
64 |
| - <TextField.Slot> |
65 |
| - <Text as="label" size="2" weight="regular" color="gray"> |
66 |
| - {IUnit.TIG} |
67 |
| - </Text> |
68 |
| - </TextField.Slot> |
69 |
| - </TextField.Root> |
70 |
| - {fieldState.error && ( |
71 |
| - <Callout.Root mt="4" color="red"> |
72 |
| - <Callout.Icon> |
73 |
| - <CircleBackslashIcon /> |
74 |
| - </Callout.Icon> |
75 |
| - <Callout.Text>{fieldState.error.message}</Callout.Text> |
76 |
| - </Callout.Root> |
77 |
| - )} |
78 |
| - </> |
79 |
| - )} |
80 |
| - ></Controller> |
81 |
| - </Box> |
82 |
| - <Grid columns="2" gap="6"> |
83 |
| - <Flex width="100%" style={{ flexFlow: "column" }} mb="4"> |
84 |
| - <Text as="label" size="2" weight="regular" mb="1" color="gray"> |
85 |
| - Price per TIG (required) |
| 37 | + {logic.balance !== undefined ? ( |
| 38 | + <Flex> |
| 39 | + <form action="" style={{ width: "100%" }}> |
| 40 | + <Flex mt="4" style={{ flexFlow: "column" }}> |
| 41 | + <Flex width="100%" style={{ flexFlow: "column" }}> |
| 42 | + <Flex align="center" justify="between" mb="1"> |
| 43 | + <Text as="label" size="2" weight="regular" color="gray"> |
| 44 | + Amount of tig (required) |
| 45 | + </Text> |
| 46 | + <Text as="span" size="2" weight="regular" color="gray"> |
| 47 | + Balance: {formatTIG(logic.balance)} |
| 48 | + <span style={{ fontSize: ".6rem" }}>{IUnit.TIG}</span> |
86 | 49 | </Text>
|
| 50 | + </Flex> |
| 51 | + <Box mb="4"> |
87 | 52 | <Controller
|
88 |
| - name="price" |
| 53 | + name="quantity" |
| 54 | + rules={{ required: true, validate: (field) => logic.hasEnoughtTIG(field) }} |
89 | 55 | control={logic.control}
|
90 |
| - render={({ field }) => ( |
91 |
| - <TextField.Root size="3" type="number" onChange={field.onChange} value={field.value}> |
92 |
| - <TextField.Slot style={{ paddingLeft: "0" }}></TextField.Slot> |
93 |
| - <TextField.Slot> |
94 |
| - <Text as="label" size="2" weight="regular" color="gray"> |
95 |
| - {IUnit.USDC} |
96 |
| - </Text> |
97 |
| - </TextField.Slot> |
98 |
| - </TextField.Root> |
| 56 | + render={({ field, fieldState }) => ( |
| 57 | + <> |
| 58 | + <TextField.Root |
| 59 | + size="3" |
| 60 | + type="number" |
| 61 | + style={{ paddingLeft: "0" }} |
| 62 | + onChange={(e) => logic.fieldUpdate("quantity", e, field.onChange)} |
| 63 | + min={1} |
| 64 | + value={field.value} |
| 65 | + > |
| 66 | + <TextField.Slot style={{ paddingLeft: "0" }}></TextField.Slot> |
| 67 | + <TextField.Slot> |
| 68 | + <Text as="label" size="2" weight="regular" color="gray"> |
| 69 | + {IUnit.TIG} |
| 70 | + </Text> |
| 71 | + </TextField.Slot> |
| 72 | + </TextField.Root> |
| 73 | + {fieldState.error && ( |
| 74 | + <Callout.Root mt="4" color="red"> |
| 75 | + <Callout.Icon> |
| 76 | + <CircleBackslashIcon /> |
| 77 | + </Callout.Icon> |
| 78 | + <Callout.Text>{fieldState.error.message}</Callout.Text> |
| 79 | + </Callout.Root> |
| 80 | + )} |
| 81 | + </> |
99 | 82 | )}
|
100 | 83 | ></Controller>
|
101 |
| - </Flex> |
102 |
| - <Flex width="100%" style={{ flexFlow: "column" }}> |
103 |
| - <Text as="label" size="2" weight="regular" mb="1" color="gray"> |
104 |
| - Total USDC (required) |
105 |
| - </Text> |
106 |
| - <Box> |
| 84 | + </Box> |
| 85 | + <Grid columns="2" gap="6"> |
| 86 | + <Flex width="100%" style={{ flexFlow: "column" }} mb="4"> |
| 87 | + <Text as="label" size="2" weight="regular" mb="1" color="gray"> |
| 88 | + Price per TIG (required) |
| 89 | + </Text> |
107 | 90 | <Controller
|
108 |
| - name="total" |
| 91 | + name="price" |
109 | 92 | control={logic.control}
|
110 | 93 | render={({ field }) => (
|
111 | 94 | <TextField.Root
|
112 | 95 | size="3"
|
113 | 96 | type="number"
|
114 |
| - onChange={(e) => logic.fieldUpdate("total", e, field.onChange)} |
| 97 | + onChange={(e) => logic.fieldUpdate("price", e, field.onChange)} |
115 | 98 | value={field.value}
|
116 | 99 | >
|
117 | 100 | <TextField.Slot style={{ paddingLeft: "0" }}></TextField.Slot>
|
118 | 101 | <TextField.Slot>
|
119 | 102 | <Text as="label" size="2" weight="regular" color="gray">
|
120 |
| - {IUnit.DOLLARD} |
| 103 | + {IUnit.USDC} |
121 | 104 | </Text>
|
122 | 105 | </TextField.Slot>
|
123 | 106 | </TextField.Root>
|
124 | 107 | )}
|
125 | 108 | ></Controller>
|
| 109 | + </Flex> |
| 110 | + <Flex width="100%" style={{ flexFlow: "column" }}> |
| 111 | + <Text as="label" size="2" weight="regular" mb="1" color="gray"> |
| 112 | + Total USDC (required) |
| 113 | + </Text> |
| 114 | + <Box> |
| 115 | + <Controller |
| 116 | + name="total" |
| 117 | + control={logic.control} |
| 118 | + render={({ field }) => ( |
| 119 | + <TextField.Root |
| 120 | + size="3" |
| 121 | + type="number" |
| 122 | + onChange={(e) => logic.fieldUpdate("total", e, field.onChange)} |
| 123 | + value={field.value} |
| 124 | + > |
| 125 | + <TextField.Slot style={{ paddingLeft: "0" }}></TextField.Slot> |
| 126 | + <TextField.Slot> |
| 127 | + <Text as="label" size="2" weight="regular" color="gray"> |
| 128 | + {IUnit.DOLLARD} |
| 129 | + </Text> |
| 130 | + </TextField.Slot> |
| 131 | + </TextField.Root> |
| 132 | + )} |
| 133 | + ></Controller> |
| 134 | + </Box> |
| 135 | + </Flex> |
| 136 | + </Grid> |
| 137 | + <Flex justify={"end"}> |
| 138 | + <Box> |
| 139 | + <Button onClick={logic.handleSubmit(logic.onSubmit)} disabled={!logic.isDirty || !logic.isValid}> |
| 140 | + <PlusIcon /> Create Offer |
| 141 | + </Button> |
126 | 142 | </Box>
|
127 | 143 | </Flex>
|
128 |
| - </Grid> |
129 |
| - <Flex justify={"end"}> |
130 |
| - <Box> |
131 |
| - <Button onClick={logic.handleSubmit(logic.onSubmit)} disabled={!logic.isDirty || !logic.isValid}> |
132 |
| - <PlusIcon /> Create Offer |
133 |
| - </Button> |
134 |
| - </Box> |
135 | 144 | </Flex>
|
136 | 145 | </Flex>
|
137 |
| - </Flex> |
138 |
| - </form> |
139 |
| - </Flex> |
| 146 | + </form> |
| 147 | + </Flex> |
| 148 | + ) : ( |
| 149 | + <Flex justify="center" align="center" style={{ minHeight: "200px" }}> |
| 150 | + <Spinner /> |
| 151 | + </Flex> |
| 152 | + )} |
140 | 153 | </Dialog.Content>
|
141 | 154 | </Dialog.Root>
|
142 | 155 | );
|
|
0 commit comments