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

Refactor - CreatePost component #167

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions src/common/graphql/fragments/postInFeed.graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { gql } from "@apollo/client";

export const FRAGMENT_POST = gql`
fragment PostInFeed on Post {
id
amount
message
images {
imageUrl
imageThumbnailUrl
}
createdAt
receivers {
id
avatar
name
}
sender {
id
name
avatar
}
votes {
voter {
id
name
}
}
}
`;
27 changes: 27 additions & 0 deletions src/common/graphql/mutations/createPost.graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { gql } from "@apollo/client";

const CREATE_POST = gql`
mutation CreatePost(
$message: String
$kudos: Int
$receivers: [ID!]
$virtual_receivers: [String!]
$team_id: ID
$images: [UploadedFile!]!
) {
createPost(
message: $message
amount: $kudos
receiverIds: $receivers
nullReceivers: $virtual_receivers
teamId: $team_id
images: $images
) {
post {
id
amount
}
}
}
`;
export default CREATE_POST;
18 changes: 18 additions & 0 deletions src/common/graphql/queries/getGoalPercentage.graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { gql } from "@apollo/client";

export const GET_GOAL_PERCENTAGE = gql`
query getGoalPercentage($team_id: ID!) {
teamById(id: $team_id) {
id
activeKudosMeter {
amount
}
activeGoals {
id
amount
name
achievedOn
}
}
}
`;
13 changes: 13 additions & 0 deletions src/common/graphql/queries/getGuidelines.graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { gql } from "@apollo/client";

export const GET_GUIDELINES = gql`
query Guidelines($team_id: ID!) {
teamById(id: $team_id) {
guidelines {
id
kudos
name
}
}
}
`;
23 changes: 23 additions & 0 deletions src/common/graphql/queries/getPosts.graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { gql } from "@apollo/client";
import { FRAGMENT_POST } from "../fragments/postInFeed.graphql";

export const GET_POSTS = gql`
query postsConnection($team_id: ID!, $end: String) {
teamById(id: $team_id) {
id
posts(first: 10, after: $end, orderBy: "created_at desc") {
edges {
cursor
node {
...PostInFeed
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
${FRAGMENT_POST}
`;
14 changes: 14 additions & 0 deletions src/common/graphql/queries/getUsers.graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { gql } from "@apollo/client";

export const GET_USERS = gql`
query getUsers($team_id: ID!) {
teamById(id: $team_id) {
id
users {
id
name
virtualUser
}
}
}
`;
31 changes: 31 additions & 0 deletions src/common/hooks/useFormValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useEffect, useState } from "react";

export type ErrorState = {
errors: string[];
};

type ValidationFunction<T> = (state: T) => ErrorState;

const useFormValidation = <T extends Record<string, any>>(
initialState: T,
validate: ValidationFunction<T>,
) => {
const [state, setState] = useState<T>(initialState);
const [errors, setErrors] = useState<ErrorState>({ errors: [] });

useEffect(() => {
const errorState = validate(state);
setErrors(errorState);
}, [])

const updateState = (state: T) => {
setState((prevState) => ({ ...prevState, ...state }));

const errorState = validate(state);
setErrors(errorState);
};

return { state, errors, updateState };
};

export default useFormValidation;
2 changes: 1 addition & 1 deletion src/components/organisms/RepoList/RepoList.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { mount, ReactWrapper } from "enzyme";
import { act } from "react-dom/test-utils";
import { GET_POSTS } from "../../../modules/feed/queries";
import {
findByTestId,
mockLocalstorage,
wait,
withMockedProviders,
} from "../../../spec_helper";
import { RepoList } from "./RepoList";
import { GET_POSTS } from "../../../common/graphql/queries/getPosts.graphql";

const mocks = (hasNextPage: boolean) => [
{
Expand Down
3 changes: 2 additions & 1 deletion src/components/organisms/RepoList/RepoList.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Query } from "@apollo/client/react/components";
import { Icon, TileList } from "@kabisa/ui-components";

import { GET_POSTS, GetPostsResult } from "../../../modules/feed/queries";
import { GetPostsResult } from "../../../modules/feed/queries";
import settings from "../../../config/settings";
import { Transaction } from "../../../modules/feed/components/Transaction";
import { Storage } from "../../../support/storage";

import s from "./RepoList.module.scss";
import { GET_POSTS } from "../../../common/graphql/queries/getPosts.graphql";

export function RepoList() {
return (
Expand Down
6 changes: 6 additions & 0 deletions src/graphql.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module '*.graphql' {
import { DocumentNode } from 'graphql'
const Schema: DocumentNode

export = Schema
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
resize: vertical;
border: 0.1rem solid var(--subtle-color);
border-radius: var(--radii-level-1);
width: 100%;
}

.button {
Expand Down
6 changes: 4 additions & 2 deletions src/modules/feed/components/CreatePost/CreatePost.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
wait,
withMockedProviders,
} from "../../../../spec_helper";
import { CREATE_POST, CreatePost } from "./CreatePost";
import { GET_GOAL_PERCENTAGE, GET_POSTS } from "../../queries";
import { GET_GOAL_PERCENTAGE } from "../../../../common/graphql/queries/getGoalPercentage.graphql";
import { GET_POSTS } from "../../../../common/graphql/queries/getPosts.graphql";
import CreatePost from "./CreatePost";
import CREATE_POST from "../../../../common/graphql/mutations/createPost.graphql";

let mutationCalled = false;
let goalPercentageQueryCalled = false;
Expand Down
Loading
Loading