storj/web/satellite/src/api/projects.ts

137 lines
4.0 KiB
TypeScript
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
import apollo from '@/utils/apolloManager';
import gql from 'graphql-tag';
import { RequestResponse } from '@/types/response';
import { CreateProjectModel, Project } from '@/types/projects';
// Performs graqhQL request for project creation.
2019-07-09 14:51:33 +01:00
export async function createProjectRequest(createProjectModel: CreateProjectModel): Promise<RequestResponse<Project>> {
let result: RequestResponse<Project> = new RequestResponse<Project>();
let response: any = await apollo.mutate(
{
mutation: gql(`
2019-08-05 16:43:52 +01:00
mutation($name: String!, $description: String!) {
createProject(
input: {
name: $name,
description: $description,
}
) {id}
}`
),
2019-08-05 16:43:52 +01:00
variables: {
name: createProjectModel.name,
description: createProjectModel.description
},
fetchPolicy: 'no-cache',
errorPolicy: 'all',
}
);
if (response.errors) {
result.errorMessage = response.errors[0].message;
} else {
result.isSuccess = true;
result.data.id = response.data.createProject.id;
result.data.description = createProjectModel.description;
result.data.name = createProjectModel.name;
}
return result;
}
// Performs graqhQL request for fetching all projects of current user.
export async function fetchProjectsRequest(): Promise<RequestResponse<Project[]>> {
let result: RequestResponse<Project[]> = new RequestResponse<Project[]>();
let response: any = await apollo.query(
{
query: gql(`
2019-08-05 16:43:52 +01:00
query {
myProjects{
name
id
description
createdAt
}
}`
),
fetchPolicy: 'no-cache',
errorPolicy: 'all',
}
);
if (response.errors) {
result.errorMessage = response.errors[0].message;
} else {
result.isSuccess = true;
result.data = response.data.myProjects;
}
return result;
}
// Performs graqhQL request for updating selected project description
2019-08-05 16:43:52 +01:00
export async function updateProjectRequest(projectId: string, description: string): Promise<RequestResponse<null>> {
let result: RequestResponse<null> = new RequestResponse<null>();
let response: any = await apollo.mutate(
{
mutation: gql(`
2019-08-05 16:43:52 +01:00
mutation($projectId: String!, $description: String!) {
updateProjectDescription(
id: $projectId,
description: $description
) {name}
}`
),
2019-08-05 16:43:52 +01:00
variables: {
projectId: projectId,
description: description
},
fetchPolicy: 'no-cache',
errorPolicy: 'all',
}
);
if (response.errors) {
result.errorMessage = response.errors[0].message;
} else {
result.isSuccess = true;
}
return result;
}
// Performs graqhQL request for deleting selected project
2019-08-05 16:43:52 +01:00
export async function deleteProjectRequest(projectId: string): Promise<RequestResponse<null>> {
let result: RequestResponse<null> = new RequestResponse<null>();
let response = await apollo.mutate(
{
mutation: gql(`
2019-08-05 16:43:52 +01:00
mutation($projectId: String!) {
deleteProject(
id: $projectId
) {name}
}`
),
2019-08-05 16:43:52 +01:00
variables: {
projectId: projectId
},
fetchPolicy: 'no-cache',
errorPolicy: 'all',
}
);
if (response.errors) {
result.errorMessage = response.errors[0].message;
} else {
result.isSuccess = true;
}
return result;
}