Skip to content
Snippets Groups Projects
Commit ae7efd3e authored by Karel Hala's avatar Karel Hala
Browse files

Add JSdoc to custom functions

parent 81567979
No related branches found
No related tags found
No related merge requests found
...@@ -65,6 +65,13 @@ export const Auth0Provider = ({ children, onRedirectCallback = DEFAULT_REDIRECT_ ...@@ -65,6 +65,13 @@ export const Auth0Provider = ({ children, onRedirectCallback = DEFAULT_REDIRECT_
setUser(user); setUser(user);
}; };
/**
* Update user data in Auth0 application.
* @param {*} userData data to be updated.
* @example
* updateProfile({ user_metadata: { name: "something" } }) // updates user metadata
* @see https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id
*/
const updateProfile = async (userData) => { const updateProfile = async (userData) => {
if (isAuthenticated) { if (isAuthenticated) {
const [{ _id, ...body }] = await Api.get("/rest/auth"); const [{ _id, ...body }] = await Api.get("/rest/auth");
...@@ -75,15 +82,13 @@ export const Auth0Provider = ({ children, onRedirectCallback = DEFAULT_REDIRECT_ ...@@ -75,15 +82,13 @@ export const Auth0Provider = ({ children, onRedirectCallback = DEFAULT_REDIRECT_
body: JSON.stringify(body) body: JSON.stringify(body)
}) })
).json(); ).json();
const rawUserData = await ( return (
await fetch(`https://${domain}/api/v2/users/${user.sub}`, { await fetch(`https://${domain}/api/v2/users/${user.sub}`, {
method: "PATCH", method: "PATCH",
headers: { Authorization: `Bearer ${access_token}`, "Content-Type": "application/json" }, headers: { Authorization: `Bearer ${access_token}`, "Content-Type": "application/json" },
body: JSON.stringify(userData) body: JSON.stringify(userData)
}) })
).json(); ).json();
setUser(rawUserData);
return rawUserData;
} }
}; };
......
...@@ -4,6 +4,15 @@ import get from "lodash/get"; ...@@ -4,6 +4,15 @@ import get from "lodash/get";
import config from "../auth_config.json"; import config from "../auth_config.json";
import keys from "../api_keys.json"; import keys from "../api_keys.json";
/**
* Update or add data at given path in custom data.
* @param {string} path what should be updated, requires at least team.dataset.
* @param {object} payload data payload that'll be saved on path.
* @param {add|replace} method either add or replace.
* @example:
* API.updateData("professors.example", {some: "value"}) // merging with previous data
* API.updateData("professors.example", {different: "value"}, "replace") // rewriting data
*/
const updateData = (api) => (path, payload, method = "add") => { const updateData = (api) => (path, payload, method = "add") => {
if (!path) { if (!path) {
throw new Error("Error, path must be specified when calling updateData endpoint"); throw new Error("Error, path must be specified when calling updateData endpoint");
...@@ -30,8 +39,12 @@ const updateData = (api) => (path, payload, method = "add") => { ...@@ -30,8 +39,12 @@ const updateData = (api) => (path, payload, method = "add") => {
}); });
}; };
/**
* Retrieve custom data for specific team.
* @param {string} teamName team to read data from.
*/
const getCustomData = (api) => (teamName = "professors") => { const getCustomData = (api) => (teamName = "professors") => {
return api.get(`/rest/customdata?q={ "team": "${teamName}" }`); return api.get(`/rest/customdata?q={"team": "${teamName}"}`);
}; };
const instances = Object.entries(keys).reduce( const instances = Object.entries(keys).reduce(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment