Skip to content
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

sveltekit projects writable #647

Merged
merged 20 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 0 additions & 1 deletion src/api/types/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ export interface Project {
updated_at: string;
edit_access: boolean;
add_remove_access: boolean;
// TODO: Add 'pinned' field on the server
pinned?: boolean;
}
4 changes: 2 additions & 2 deletions src/common/Dropdown2.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@
position: absolute;
}
.dropdown.top {
bottom: 100%;
bottom: calc(100% + var(--offset, 0px));
}
.dropdown.bottom {
top: 100%;
top: calc(100% + var(--offset, 0px));
}
eyeseast marked this conversation as resolved.
Show resolved Hide resolved
.dropdown.left {
left: 0;
Expand Down
1 change: 0 additions & 1 deletion src/common/icons/Pin.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@
svg {
display: block;
transform: rotate(-45deg);
fill: var(--fill, var(--orange, #ec7b6b));
}
</style>
58 changes: 55 additions & 3 deletions src/langs/json/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"new": "New",
"learnMore": "Learn more",
"or": "or",
"explore": "Explore"
"explore": "Explore",
"more": "More",
"add": "Add"
},
"homeTemplate": {
"signedIn": "Signed in as {name}",
Expand Down Expand Up @@ -682,15 +684,65 @@
"createProject": "Create your first project by clicking “New Project” above.",
"pinsEmpty": "Pinned projects will appear here for quick access",
"private": "Private Project",
"public": "Public Project",
"public": "Public Projects",
"loading": "Loading project documents…",
"empty": "This project is empty",
"error": "Error loading project documents",
"viewInSearch": "View in Document Search",
"yours": "Your Projects",
"shared": "Shared with you",
"none": "No projects found",
"create": "Create project"
"create": "Create project",
"edit": "Edit project metadata",
"share": "Share & embed this project",
"users": "Manage Collaborators",
"add": "Add {n, plural, one {this document} other {# documents}} to a project",
"delete": {
"action": "Delete project",
"confirm": "Confirm delete",
"really": "Are you sure you want to delete this project ({project})?"
},
"placeholder": {
"projects": "Search projects",
"documents": "Search documents"
},
"collaborators": {
"title": "Collaborators",
"manage": "Change access",
"empty": "No collaborators",
"add": "Invite users to this project"
},
"fields": {
"title": "Title",
"description": "Description",
"private": "Private Project",
"pinned": "Pin project"
}
},
"collaborators": {
"remove": {
"label": "Remove user",
"confirm": "Confirm remove user",
"message": "Proceeding will remove {name} from {title}. Do you wish to continue?"
},
"add": "Add",
"addCollaborators": "Add Collaborators",
"invite": "Put in the email of an existing DocumentCloud user below. If they don't have an account, have them register <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://accounts.muckrock.com/accounts/signup/?intent=documentcloud\">here for free</a>, and then ask them to log in to DocumentCloud at least once.",
"admin": "Admin",
"view": "View",
"edit": "Edit",
"help": {
"admin": "Collaborators can edit this project and its documents",
"edit": "Collaborators can edit documents in this project",
"view": "Collaborators can view documents in this project",
"remove": "Remove this user from this project"
},
"empty": "You have not yet added any collaborators to this project. Invite collaborators to grant other users access to the documents shared in this project. You can control whether collaborators have access to view/edit the project’s documents or be an admin with permissions to invite other users and edit the project itself.",
"manage": "Manage Collaborators",
"name": "Name",
"access": "Access",
"you": "(you)",
"change": "Change access"
},
"organizations": {
"sameOrgUsers": "Users in organization"
Expand Down
104 changes: 104 additions & 0 deletions src/lib/api/collaborators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// manage users in a project
import type { ProjectAccess, ProjectUser } from "./types";

import { APP_URL, BASE_API_URL, CSRF_HEADER_NAME } from "@/config/config.js";
import { getAll, isErrorCode } from "$lib/utils/api";

export async function list(project_id: number, fetch = globalThis.fetch) {
const endpoint = new URL(
`projects/${project_id}/users/?expand=user`,
BASE_API_URL,
);

return getAll<ProjectUser>(endpoint, undefined, fetch);
}

export async function add(
project_id: number,
user: { email: string; access: ProjectAccess },
csrf_token: string,
fetch = globalThis.fetch,
) {
const endpoint = new URL(`projects/${project_id}/users/`, BASE_API_URL);

const resp = await fetch(endpoint, {
body: JSON.stringify(user),
credentials: "include",
headers: {
"Content-type": "application/json",
[CSRF_HEADER_NAME]: csrf_token,
Referer: APP_URL,
},
method: "POST",
}).catch(console.error);

if (!resp) {
throw new Error("API unavailable");
}

if (isErrorCode(resp.status)) {
const data = await resp.json();
console.error(data);
throw new Error(resp.statusText);
}

return resp.json();
}

export async function update(
project_id: number,
user_id: number,
access: ProjectAccess,
csrf_token: string,
fetch = globalThis.fetch,
) {
const endpoint = new URL(
`projects/${project_id}/users/${user_id}/`,
BASE_API_URL,
);

const resp = await fetch(endpoint, {
body: JSON.stringify({ user: user_id, access }),
credentials: "include",
headers: {
"Content-type": "application/json",
[CSRF_HEADER_NAME]: csrf_token,
Referer: APP_URL,
},
method: "PATCH",
}).catch(console.error);

if (!resp) {
throw new Error("API unavailable");
}

if (isErrorCode(resp.status)) {
const data = await resp.json();
console.error(data);
throw new Error(resp.statusText);
}

return resp.json();
}

export async function remove(
project_id: number,
user_id: number,
csrf_token: string,
fetch = globalThis.fetch,
) {
const endpoint = new URL(
`projects/${project_id}/users/${user_id}/`,
BASE_API_URL,
);

return fetch(endpoint, {
credentials: "include",
headers: {
"Content-type": "application/json",
[CSRF_HEADER_NAME]: csrf_token,
Referer: APP_URL,
},
method: "DELETE",
});
}
Loading