• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cartesi / rollups-explorer / 10174191028

31 Jul 2024 04:40AM UTC coverage: 93.726% (+0.4%) from 93.346%
10174191028

Pull #213

github

brunomenezes
refactor: Exposing onSuccess for better reusability. Removed routing on success only notifications are used.
Pull Request #213: Feature/165 add decode specification v1

1141 of 1361 branches covered (83.84%)

Branch coverage included in aggregate %.

3317 of 3433 new or added lines in 36 files covered. (96.62%)

1 existing line in 1 file now uncovered.

12348 of 13031 relevant lines covered (94.76%)

46.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.63
/apps/web/src/components/specification/SpecificationFormView.tsx
1
"use client";
1✔
2
import {
1✔
3
    Center,
1✔
4
    Flex,
1✔
5
    Grid,
1✔
6
    GridCol,
1✔
7
    SegmentedControl,
1✔
8
    Stack,
1✔
9
} from "@mantine/core";
1✔
10
import { notifications } from "@mantine/notifications";
1✔
11
import { propOr } from "ramda";
1✔
12
import { isFunction, isNilOrEmpty, isNotNilOrEmpty } from "ramda-adjunct";
1✔
13
import { FC, useCallback, useState } from "react";
1✔
14
import { TbLayoutColumns, TbLayoutList } from "react-icons/tb";
1✔
15
import { DecodingPreview } from "./components/DecodingPreview";
1✔
16
import { SpecificationForm } from "./form/SpecificationForm";
1✔
17
import {
1✔
18
    FormMode,
1✔
19
    SpecFormProvider,
1✔
20
    SpecFormValues,
1✔
21
    useSpecForm,
1✔
22
} from "./form/context";
1✔
23
import {
1✔
24
    specABIValidation,
1✔
25
    specAbiParamValidation,
1✔
26
    specConditionalsValidation,
1✔
27
    specEncodedDataValidation,
1✔
28
    specModeValidation,
1✔
29
    specNameValidation,
1✔
30
    specSliceInstructionsValidation,
1✔
31
} from "./form/validations";
1✔
32
import { JSON_ABI, Specification } from "./types";
1✔
33

1✔
34
type Layout = "split_screen" | "stack_screen";
1✔
35
const getInitialValues = (spec?: Specification): SpecFormValues => {
1✔
36
    const values: SpecFormValues = {
108✔
37
        formMode: isNilOrEmpty(spec) ? "CREATION" : "EDITION",
108✔
38
        name: propOr("", "name", spec),
108✔
39
        mode: propOr(JSON_ABI, "mode", spec),
108✔
40
        conditionals: propOr([], "conditionals", spec),
108✔
41
        conditionalsOn: isNotNilOrEmpty(propOr([], "conditionals", spec)),
108✔
42
        abi: propOr(undefined, "abi", spec),
108✔
43
        abiParams: propOr([], "abiParams", spec),
108✔
44
        sliceInstructions: propOr([], "sliceInstructions", spec),
108✔
45
        sliceInstructionsOn: isNotNilOrEmpty(
108✔
46
            propOr([], "sliceInstructions", spec),
108✔
47
        ),
108✔
48
        sliceTarget: propOr(undefined, "sliceTarget", spec),
108✔
49
        editingData: isNotNilOrEmpty(spec)
108✔
50
            ? { originalSpec: spec! }
11✔
51
            : undefined,
97✔
52
    };
108✔
53
    return values;
108✔
54
};
108✔
55

1✔
56
type SuccessData = { spec: Specification; formMode: FormMode };
1✔
57
export type SpecificationFormViewOnSuccess = (data: SuccessData) => void;
1✔
58
interface ViewProps {
1✔
59
    specification?: Specification;
1✔
60
    onSuccess?: SpecificationFormViewOnSuccess;
1✔
61
}
1✔
62

1✔
63
export const SpecificationFormView: FC<ViewProps> = ({
1✔
64
    specification,
108✔
65
    onSuccess,
108✔
66
}) => {
108✔
67
    const [layout, setLayout] = useState<Layout>("split_screen");
108✔
68
    const colSpan = layout === "split_screen" ? 6 : 12;
108!
69
    const form = useSpecForm({
108✔
70
        initialValues: getInitialValues(specification),
108✔
71
        validate: {
108✔
72
            name: specNameValidation,
108✔
73
            mode: specModeValidation,
108✔
74
            abi: specABIValidation,
108✔
75
            abiParams: specAbiParamValidation,
108✔
76
            sliceInstructions: specSliceInstructionsValidation,
108✔
77
            conditionals: specConditionalsValidation,
108✔
78
            encodedData: specEncodedDataValidation,
108✔
79
        },
108✔
80
    });
108✔
81
    const { formMode } = form.getTransformedValues();
108✔
82

108✔
83
    const onFinished = useCallback(
108✔
84
        (spec: Specification) => {
108✔
85
            const isEditionMode = formMode === "EDITION";
3✔
86
            const action = isEditionMode ? "Updated!" : "Saved!";
3✔
87
            const message = `Specification ${spec.name} ${action}`;
3✔
88
            notifications.show({
3✔
89
                color: "green",
3✔
90
                title: "Success!",
3✔
91
                withBorder: true,
3✔
92
                withCloseButton: true,
3✔
93
                message,
3✔
94
            });
3✔
95

3✔
96
            isFunction(onSuccess) && onSuccess({ formMode, spec });
3✔
97
        },
3✔
98
        [formMode, onSuccess],
108✔
99
    );
108✔
100

108✔
101
    const onFailure = useCallback((reason: Error) => {
108✔
102
        notifications.show({
2✔
103
            color: "red",
2✔
104
            title: "Oops!",
2✔
105
            message: reason.message ?? "Something went wrong!",
2!
106
            withBorder: true,
2✔
107
            withCloseButton: true,
2✔
108
        });
2✔
109
    }, []);
108✔
110

108✔
111
    return (
108✔
112
        <Stack>
108✔
113
            <Flex justify="flex-start">
108✔
114
                <SegmentedControl
108✔
115
                    data-testid="specification-creation-view-switch"
108✔
116
                    value={layout}
108✔
117
                    onChange={(value) => {
108✔
NEW
118
                        setLayout(value as Layout);
×
NEW
119
                    }}
×
120
                    data={[
108✔
121
                        {
108✔
122
                            value: "split_screen",
108✔
123
                            label: (
108✔
124
                                <Center style={{ gap: 10 }}>
108✔
125
                                    <TbLayoutColumns size={18} />
108✔
126
                                    <span>Split View</span>
108✔
127
                                </Center>
108✔
128
                            ),
108✔
129
                        },
108✔
130
                        {
108✔
131
                            value: "stack_screen",
108✔
132
                            label: (
108✔
133
                                <Center style={{ gap: 10 }}>
108✔
134
                                    <TbLayoutList size={18} />
108✔
135
                                    <span>List View</span>
108✔
136
                                </Center>
108✔
137
                            ),
108✔
138
                        },
108✔
139
                    ]}
108✔
140
                />
108✔
141
            </Flex>
108✔
142
            <SpecFormProvider form={form}>
108✔
143
                <Grid>
108✔
144
                    <GridCol span={colSpan}>
108✔
145
                        <SpecificationForm
108✔
146
                            onSuccess={onFinished}
108✔
147
                            onFailure={onFailure}
108✔
148
                        />
108✔
149
                    </GridCol>
108✔
150
                    <GridCol span={colSpan}>
108✔
151
                        <DecodingPreview />
108✔
152
                    </GridCol>
108✔
153
                </Grid>
108✔
154
            </SpecFormProvider>
108✔
155
        </Stack>
108✔
156
    );
108✔
157
};
108✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc