|
| 1 | +# Customizing snap-in configuration pages |
| 2 | + |
| 3 | +The DevRev snap-ins platform allows developers to define custom configuration pages for their snap-ins. |
| 4 | + |
| 5 | +While the default configuration page automatically renders input fields for keyrings and inputs, there may be cases where a custom configuration page is more suitable: |
| 6 | + |
| 7 | +- Improved user experience: Developers can design the configuration page to provide a more intuitive and streamlined experience for users setting up the snap-in. |
| 8 | +- Advanced input handling: Custom configuration pages enable developers to handle complex input scenarios, such as fetching data from external systems to populate dropdown options or validating user input. |
| 9 | +- Branding and styling: Developers can align the configuration page with their snap-in's branding and style guidelines, ensuring a consistent look and feel. |
| 10 | + |
| 11 | +## Defining custom configuration pages |
| 12 | + |
| 13 | +To create a custom configuration page for a snap-in, developers need to define the following in the snap-in manifest: |
| 14 | + |
| 15 | +```yaml |
| 16 | +snap_kit_actions: |
| 17 | + - name: org_snap_kit_action |
| 18 | + function: snap_in_configuration_handler |
| 19 | + - name: user_snap_kit_action |
| 20 | + function: snap_in_configuration_handler |
| 21 | + |
| 22 | +functions: |
| 23 | + - name: snap_in_configuration_handler |
| 24 | + description: Handler for processing organization and user configuration options. |
| 25 | + - name: config_initializer |
| 26 | + description: Generates the initial configuration options for both organization and user. |
| 27 | + |
| 28 | +configuration_handler: |
| 29 | + organization: |
| 30 | + initializer: config_initializer |
| 31 | + snap_kit_action_name: org_snap_kit_action |
| 32 | + user: |
| 33 | + initializer: config_initializer |
| 34 | + snap_kit_action_name: user_snap_kit_action |
| 35 | +``` |
| 36 | +
|
| 37 | +The `configuration_handler` section in the manifest connects the functions responsible for generating and processing the custom configuration page. |
| 38 | + |
| 39 | +- `config_initializer`: Generates the initial configuration options for both organization and user. It is called when the configuration page is first loaded. |
| 40 | +- `snap_in_configuration_handler`: This function processes the user and organization configuration options. It is triggered when actions are performed on the organization or user configuration snap-kit. |
| 41 | + |
| 42 | +## Configuration functions |
| 43 | + |
| 44 | +The configuration functions should return a valid snap-kit JSON that defines the layout and elements of the custom configuration page. Here's an example of a snap-kit JSON: |
| 45 | + |
| 46 | +```json |
| 47 | +{ |
| 48 | + "snap_kit_body": { |
| 49 | + "body": { |
| 50 | + "snaps": [ |
| 51 | + { |
| 52 | + "elements": [ |
| 53 | + { |
| 54 | + "action_id": "user_snap_kit_action", |
| 55 | + "action_type": "remote", |
| 56 | + "elements": [ |
| 57 | + { |
| 58 | + "element": { |
| 59 | + "action_id": "select", |
| 60 | + "action_type": "client", |
| 61 | + "initial_selected_option": { |
| 62 | + "text": { |
| 63 | + "text": "Ticket", |
| 64 | + "type": "plain_text" |
| 65 | + }, |
| 66 | + "value": "ticket" |
| 67 | + }, |
| 68 | + "options": [ |
| 69 | + { |
| 70 | + "text": { |
| 71 | + "text": "Ticket", |
| 72 | + "type": "plain_text" |
| 73 | + }, |
| 74 | + "value": "ticket" |
| 75 | + }, |
| 76 | + { |
| 77 | + "text": { |
| 78 | + "text": "Conversation", |
| 79 | + "type": "plain_text" |
| 80 | + }, |
| 81 | + "value": "conversation" |
| 82 | + } |
| 83 | + ], |
| 84 | + "type": "static_select" |
| 85 | + }, |
| 86 | + "type": "input_layout" |
| 87 | + } |
| 88 | + ], |
| 89 | + "submit_action": { |
| 90 | + "action_id": "next", |
| 91 | + "style": "primary", |
| 92 | + "text": { |
| 93 | + "text": "Next", |
| 94 | + "type": "plain_text" |
| 95 | + }, |
| 96 | + "type": "button", |
| 97 | + "value": "next" |
| 98 | + }, |
| 99 | + "type": "form" |
| 100 | + } |
| 101 | + ], |
| 102 | + "type": "card" |
| 103 | + } |
| 104 | + ] |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +In this example, the snap-kit renders a dropdown select for choosing between `Ticket` and `Conversation`, along with a `Next` button. When the **Next** button is clicked, the `user_snap_in_configuration_handler` function is invoked to process the user's selection. |
| 111 | +In the snap-kit action handler, the `event.payload.action.id` can be used to determine the form being submitted and call the `snap-ins.update` API to update the configuration. |
| 112 | + |
| 113 | +## Update snap-in inputs (beta) |
| 114 | + |
| 115 | +Updates the inputs of a snap-in based on the inputs defined in the snap-in configuration. |
| 116 | + |
| 117 | +**Note: This endpoint is currently in beta and may be subject to change in the future. Reach out to us via PLuG to subscribe to changes to beta endpoints.** |
| 118 | + |
| 119 | +### Request payload |
| 120 | + |
| 121 | +The request payload should be a JSON object with the following properties: |
| 122 | + |
| 123 | +- `id` (string, required): The ID of the snap-in to update. |
| 124 | +- `inputs_values` (object, required): An object containing the input values to update. The properties of this object should match the input names defined in the snap-in configuration. |
| 125 | + |
| 126 | +Example payload: |
| 127 | +```json |
| 128 | +{ |
| 129 | + "id": "snap_in_id", |
| 130 | + "inputs_values": { |
| 131 | + "part_picker": "don:core:dvrv-us-1:devo/XXXX/product:XXXX", |
| 132 | + "enum_list_picker": ["value-1", "value-2"] |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +In the example above, the `part_picker` and `enum_list_picker` are the input names defined in the snap-in configuration, and their corresponding values are provided in the `inputs_values` object. |
| 138 | + |
| 139 | +### Response |
| 140 | + |
| 141 | +#### Success response |
| 142 | + |
| 143 | +- Status Code: 200 OK |
| 144 | +- Content-Type: application/json |
| 145 | + |
| 146 | +Response body: |
| 147 | +```json |
| 148 | +{ |
| 149 | + "data": {}, |
| 150 | + "message": "Snap-in updated successfully", |
| 151 | + "success": true |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +#### Error response |
| 156 | + |
| 157 | +If an error occurs while updating the snap-in, the response has the following format: |
| 158 | + |
| 159 | +- Status Code: 4xx or 5xx |
| 160 | +- Content-Type: application/json |
| 161 | + |
| 162 | +Response body: |
| 163 | +```json |
| 164 | +{ |
| 165 | + "data": {}, |
| 166 | + "success": false |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +## Example usage |
| 171 | + |
| 172 | +Here's an example of how to update the input values: |
| 173 | + |
| 174 | +```typescript |
| 175 | +async updateSnapInInputs(snapInId: string, inputsValues: Record<string, any>): Promise<HTTPResponse> { |
| 176 | + const payload: SnapInsUpdateRequest = { |
| 177 | + id: snapInId, |
| 178 | + inputsValues, |
| 179 | + }; |
| 180 | + return this.updateSnapInInputs(payload); |
| 181 | +} |
| 182 | +
|
| 183 | +async updateSnapInInputs(payload: SnapInsUpdateRequest): Promise<HTTPResponse> { |
| 184 | + try { |
| 185 | + |
| 186 | + await devrevSDK.snapInsUpdate(payload); |
| 187 | + return { data: {}, message: 'Snap-in updated successfully', success: true }; |
| 188 | + } catch (error: any) { |
| 189 | + if (error.response) { |
| 190 | + const err = `Failed to update the snap-in. Err: ${JSON.stringify(error.response.data)}, Status: ${error.response.status}`; |
| 191 | + return { ...defaultResponse, message: err }; |
| 192 | + } else { |
| 193 | + return { ...defaultResponse, message: error.message }; |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +In this example, the `updateSnapInInputs` function takes the `snapInId` and `inputsValues` as parameters. The `inputsValues` object should contain the input names and their corresponding values as defined in the snap-in configuration. |
| 200 | + |
| 201 | +The function constructs the payload object with the `snapInId` and `inputsValues`, and then calls the `updateSnapInInputs` function to make the POST request to the `snap-ins.update` endpoint. |
| 202 | + |
| 203 | +If the request is successful, it returns a success response with a status code of 200 and a message indicating that the snap-in was updated successfully. |
| 204 | + |
| 205 | +If an error occurs, it catches the error and returns an error response with an appropriate status code and an error message containing the error details. |
| 206 | + |
| 207 | +<Note> |
| 208 | +Note: This endpoint is currently in beta, and its functionality or parameters may change in future updates. |
| 209 | +</Note> |
| 210 | + |
| 211 | +For more details on the snap-kit JSON format and available elements, refer to the [DevRev Snap-kit documentation](/snapin-development/references/snapkit). |
0 commit comments