forked from ThePalaceProject/circulation-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - bookEditorSlice working / tests passing / some old tests commen…
…ted out.
- Loading branch information
Showing
11 changed files
with
416 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
RequestError, | ||
RequestRejector, | ||
} from "@thepalaceproject/web-opds-client/lib/DataFetcher"; | ||
|
||
export const submitForm = ( | ||
url: string, | ||
{ | ||
data = null, | ||
csrfToken = undefined, | ||
returnType = undefined, | ||
method = "POST", | ||
defaultErrorMessage = "Failed to save changes", | ||
} = {} | ||
) => { | ||
let err: RequestError; | ||
return new Promise((resolve, reject: RequestRejector) => { | ||
const headers = new Headers(); | ||
if (csrfToken) { | ||
headers.append("X-CSRF-Token", csrfToken); | ||
} | ||
fetch(url, { | ||
method: method, | ||
headers: headers, | ||
body: data, | ||
credentials: "same-origin", | ||
}) | ||
.then((response) => { | ||
if (response.status === 200 || response.status === 201) { | ||
if (response.json && returnType === "JSON") { | ||
response.json().then((data) => { | ||
resolve(response); | ||
}); | ||
} else if (response.text) { | ||
response.text().then((text) => { | ||
resolve(response); | ||
}); | ||
} else { | ||
resolve(response); | ||
} | ||
} else { | ||
response | ||
.json() | ||
.then((data) => { | ||
err = { | ||
status: response.status, | ||
response: data.detail, | ||
url: url, | ||
}; | ||
reject(err); | ||
}) | ||
.catch((parseError) => { | ||
err = { | ||
status: response.status, | ||
response: defaultErrorMessage, | ||
url: url, | ||
}; | ||
reject(err); | ||
}); | ||
} | ||
}) | ||
.catch((err) => { | ||
err = { | ||
status: null, | ||
response: err.message, | ||
url: url, | ||
}; | ||
reject(err); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.