Skip to content

Commit

Permalink
Merge pull request #824 from filecoin-project/@martinalong/links
Browse files Browse the repository at this point in the history
@martinalong/links
  • Loading branch information
martinalong committed Jul 7, 2021
2 parents 18f283b + 7843ca6 commit b10accf
Show file tree
Hide file tree
Showing 157 changed files with 3,294 additions and 2,407 deletions.
63 changes: 39 additions & 24 deletions common/actions.js
@@ -1,34 +1,50 @@
import "isomorphic-fetch";
import microlink from "@microlink/mql";

import * as Events from "~/common/custom-events";
import * as Websockets from "~/common/browser-websockets";
import * as Strings from "~/common/strings";
import * as Credentials from "~/common/credentials";

//NOTE(martina): call Websockets.checkWebsocket() before any api call that uses websockets to return updates
// to make sure that websockets are properly connected (and to reconnect them if they are not)
// otherwise updates may not occur properly

//NOTE(martina): if the server is the slate backend, you should set credentials: "include". If it is cross origin (aka a call to shovel or lens), you will not be able to use credentials
// Instead, if a cross origin server requires authorization, pass an API key with Authorization: getCookie(Credentials.session.key)
const getCookie = (name) => {
var match = document.cookie.match(new RegExp("(^| )" + name + "=([^;]+)"));
if (match) return match[2];
};

const REQUEST_HEADERS = {
Accept: "application/json",
"Content-Type": "application/json",
};

//NOTE(martina): used for calls to the server
const DEFAULT_OPTIONS = {
method: "POST",
headers: REQUEST_HEADERS,
credentials: "include",
};

//NOTE(martina): used for calls to other servers (where sending credentials isn't allowed b/c it's cross origin) which also don't require API keys
const CORS_OPTIONS = {
method: "POST",
headers: REQUEST_HEADERS,
credentials: "omit",
};

const returnJSON = async (route, options) => {
const response = await fetch(route, options);
const json = await response.json();
try {
const response = await fetch(route, options);
const json = await response.json();

return json;
return json;
} catch (e) {
console.log(e);
}
};

export const createZipToken = async ({ files, resourceURI }) => {
Expand Down Expand Up @@ -68,6 +84,18 @@ export const sendFilecoin = async (data) => {
});
};

// export const mql = async (url) => {
// try {
// const res = await microlink(url, { screenshot: true });
// return res;
// } catch (e) {
// console.log(e);
// if (e.description) {
// Events.dispatchMessage({ message: e.description });
// }
// }
// };

export const checkUsername = async (data) => {
return await returnJSON(`/api/users/check-username`, {
...DEFAULT_OPTIONS,
Expand Down Expand Up @@ -197,6 +225,14 @@ export const createFile = async (data) => {
});
};

export const createLink = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/data/create-link`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};

export const addFileToSlate = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/slates/add-file`, {
Expand Down Expand Up @@ -232,20 +268,6 @@ export const createUserViaTwitterWithVerification = async (data) => {
});
};

export const linkTwitterAccount = async (data) => {
return await returnJSON(`/api/twitter/link`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};

export const linkTwitterAccountWithVerification = async (data) => {
return await returnJSON(`/api/twitter/link-with-verification`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};

export const updateViewer = async (data) => {
await Websockets.checkWebsocket();
return await returnJSON(`/api/users/update`, {
Expand Down Expand Up @@ -466,13 +488,6 @@ export const createPasswordResetVerification = async (data) => {
});
};

export const resendPasswordResetVerification = async (data) => {
return await returnJSON(`/api/verifications/password-reset/resend`, {
...DEFAULT_OPTIONS,
body: JSON.stringify({ data }),
});
};

export const verifyPasswordResetEmail = async (data) => {
return await returnJSON(`/api/verifications/password-reset/verify`, {
...DEFAULT_OPTIONS,
Expand Down
14 changes: 14 additions & 0 deletions common/arrays.js
@@ -1,3 +1,5 @@
import * as Validations from "~/common/validations";

export const filterPublic = (arr = []) => {
return arr.filter((item) => item.isPublic);
};
Expand All @@ -10,6 +12,10 @@ export const mapToIds = (arr = []) => {
return arr.map((item) => item.id);
};

export const mapToCids = (arr = []) => {
return arr.map((item) => item.cid);
};

export const countPublic = (arr = []) => {
const reducer = (count, item) => {
if (item.isPublic) return count + 1;
Expand All @@ -25,3 +31,11 @@ export const countPrivate = (arr = []) => {
};
return arr.reduce(reducer, 0);
};

export const filterLinks = (arr = []) => {
return arr.filter((item) => item.isLink);
};

export const filterFiles = (arr = []) => {
return arr.filter((item) => !item.isLink);
};
4 changes: 2 additions & 2 deletions common/constants.js
Expand Up @@ -7,7 +7,7 @@ export const sizes = {
mobile: 768,
navigation: 288,
sidebar: 416,
header: 72,
header: 56,
tablet: 960,
desktop: 1024,
topOffset: 0, //NOTE(martina): Pushes UI down. 16 when there is a persistent announcement banner, 0 otherwise
Expand Down Expand Up @@ -96,7 +96,7 @@ export const semantic = {
bgBlurWhite: "rgba(system.white, 0.7)",
bgBlurWhiteOP: "rgba(system.white, 0.85)",
bgBlurWhiteTRN: "rgba(system.white, 0.3)",
bgBlurlight6: "rgba(system.grayLight6, 0.7)",
bgBlurLight6: "rgba(system.grayLight6, 0.7)",
bgBlurLight6OP: "rgba(system.grayLight6, 0.85)",
bgBlurLight6TRN: "rgba(system.grayLight6, 0.3)",

Expand Down

0 comments on commit b10accf

Please sign in to comment.