1
1
import { useState } from 'react' ;
2
2
import { updateContent } from '../..' ;
3
3
4
+ /**
5
+ * Custom hook for editing content of translation helps resources
6
+ * @param {string } sha
7
+ * @param {string } repo
8
+ * @param {string } owner
9
+ * @param {string } token
10
+ * @param {object } config - config settings for fetches (timeout, cache, etc.)
11
+ * @param {string } branch - branch name.
12
+ * @param {string } author - author of the edit.
13
+ * @param {string } content - Edited/updated content.
14
+ * @param {string } message - Optional commit message.
15
+ * @param {string } filePath - file path, file path for the file being edited.
16
+ * @return {{error: object, isError: boolean, isEditing: boolean, onSaveEdit: (function(_branch: string)), editResponse: object} }
17
+ */
4
18
export default function useEdit ( {
5
19
sha,
6
20
repo,
@@ -13,18 +27,21 @@ export default function useEdit({
13
27
message,
14
28
filepath,
15
29
} ) {
16
- const [ { isEditing , isError , error , editResponse } , setState ] = useState ( {
30
+ const initialState = {
17
31
editResponse : null ,
18
32
isEditing : false ,
19
33
isError : false ,
20
34
error : null ,
21
- } )
35
+ }
36
+ const [ { isEditing, isError, error, editResponse } , setState ] = useState ( initialState )
22
37
const { name : tokenid } = token || { }
23
38
const _message = message || `Edit '${ filepath } ' using '${ tokenid } '` ;
24
39
25
40
async function onSaveEdit ( _branch ) {
26
41
try {
42
+ // content is the updated string or dirty content.
27
43
if ( content ) {
44
+ // clear state to remove left over state from a previous edit.
28
45
setState ( ( prevState ) => ( {
29
46
...prevState ,
30
47
editResponse : null ,
0 commit comments