Skip to content

Documenting useEdit hook #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/components/file/useEdit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { useState } from 'react';
import { updateContent } from '../..';

/**
* Custom hook for editing content of translation helps resources
* @param {string} sha
* @param {string} repo
* @param {string} owner
* @param {string} token
* @param {object} config - config settings for fetches (timeout, cache, etc.)
* @param {string} branch - branch name.
* @param {string} author - author of the edit.
* @param {string} content - Edited/updated content.
* @param {string} message - Optional commit message.
* @param {string} filePath - file path, file path for the file being edited.
* @return {{error: object, isError: boolean, isEditing: boolean, onSaveEdit: (function(_branch: string)), editResponse: object}}
*/
export default function useEdit({
sha,
repo,
Expand All @@ -13,18 +27,21 @@ export default function useEdit({
message,
filepath,
}) {
const [{ isEditing, isError, error, editResponse }, setState] = useState({
const initialState = {
editResponse: null,
isEditing: false,
isError: false,
error: null,
})
}
const [{ isEditing, isError, error, editResponse }, setState] = useState(initialState)
const { name: tokenid } = token || {}
const _message = message || `Edit '${filepath}' using '${tokenid}'`;

async function onSaveEdit(_branch) {
try {
// content is the updated string or dirty content.
if (content) {
// clear state to remove left over state from a previous edit.
setState((prevState) => ({
...prevState,
editResponse: null,
Expand Down