From 2f119e43bda13ea5229fb1631321447b1abebef8 Mon Sep 17 00:00:00 2001 From: Riccardo Date: Mon, 16 Aug 2021 11:06:02 +0200 Subject: [PATCH] Changes --- client/src/components/App.js | 2 + client/src/components/Search.js | 64 ++++++------------- .../src/components/appointment/Appointment.js | 8 +-- .../appointment/UpdateAppointment.js | 4 +- client/src/components/layout/Header.js | 5 +- server/src/index.js | 4 +- server/src/models/appointment.js | 5 ++ server/src/resolvers.js | 2 +- server/src/resolvers/Mutation.js | 12 ++-- server/src/resolvers/Query.js | 2 +- server/src/resolvers/Subscription.js | 24 +++---- server/src/schema.graphql | 37 ++++------- 12 files changed, 68 insertions(+), 101 deletions(-) diff --git a/client/src/components/App.js b/client/src/components/App.js index a7a83f5..312498d 100644 --- a/client/src/components/App.js +++ b/client/src/components/App.js @@ -5,6 +5,7 @@ import AppointmentList from './appointment/AppointmentList'; import CreateAppointment from './appointment/CreateAppointment'; import UpdateAppointemnt from './appointment/UpdateAppointment'; import Calendar from './Calendar'; +import Search from './Search'; import { Switch, Route } from 'react-router-dom'; const App = () => { @@ -18,6 +19,7 @@ const App = () => { + diff --git a/client/src/components/Search.js b/client/src/components/Search.js index de7269c..a2848c5 100644 --- a/client/src/components/Search.js +++ b/client/src/components/Search.js @@ -1,63 +1,39 @@ import React, { useState } from 'react'; -import { useMutation } from '@apollo/client'; import { useLazyQuery } from '@apollo/client'; import gql from 'graphql-tag'; -import Appointment from './Appointment'; -import { Link } from 'react-router-dom'; +import Appointment from './appointment/Appointment'; const FEED_SEARCH_QUERY = gql` query FeedSearchQuery($filter: String!) { feed(filter: $filter) { id - links { - id + appointments { + _id title description - type - createdBy { - id - username - } - # follows { - # id - # user { - # id - # } - # } } } } `; const Search = () => { - const [searchFilter, setSearchFilter] = useState(''); - const [executeSearch, { data }] = useLazyQuery( - FEED_SEARCH_QUERY - ); - return ( - <> -
- Search - setSearchFilter(e.target.value)} - /> - -
- {/* {data && - data.feed.appointments.map((appointment, index) => ( - - ))} */} - - ); + const [searchFilter, setSearchFilter] = useState(''); + const [executeSearch, { data }] = useLazyQuery( + FEED_SEARCH_QUERY + ); + return ( + <> +
+ Search + setSearchFilter(e.target.value)}/> + +
+ {data && + data.feed.appointments.map((appointment, index) => ( + + ))} + + ); }; export default Search; \ No newline at end of file diff --git a/client/src/components/appointment/Appointment.js b/client/src/components/appointment/Appointment.js index 5efd5c4..fd5fbc6 100644 --- a/client/src/components/appointment/Appointment.js +++ b/client/src/components/appointment/Appointment.js @@ -21,10 +21,10 @@ const Appointment = (props) => { onCompleted: () => history.push('/') }) - // const updateAppointment = () => { - // let path = `/update/${appointment._id}`; - // history.push(path); - // } + const updateAppointment = () => { + let path = `/update/${appointment._id}`; + history.push(path); + } return (
diff --git a/client/src/components/appointment/UpdateAppointment.js b/client/src/components/appointment/UpdateAppointment.js index 32b1e10..50ed89c 100644 --- a/client/src/components/appointment/UpdateAppointment.js +++ b/client/src/components/appointment/UpdateAppointment.js @@ -1,8 +1,8 @@ import React, { useState } from 'react'; import { useHistory } from 'react-router'; import { useMutation, gql, useQuery } from '@apollo/client'; -import { APPOINTMENTS_PER_PAGE } from '../../constants'; -import { APPOINTMENTS_QUERY } from './AppointmentList'; +// import { APPOINTMENTS_PER_PAGE } from '../../constants'; +// import { APPOINTMENTS_QUERY } from './AppointmentList'; import Datetime from 'react-datetime'; import "react-datetime/css/react-datetime.css"; diff --git a/client/src/components/layout/Header.js b/client/src/components/layout/Header.js index af7b150..33e38aa 100644 --- a/client/src/components/layout/Header.js +++ b/client/src/components/layout/Header.js @@ -1,6 +1,6 @@ import React from 'react'; // import { useHistory } from 'react-router'; -import { Link, withRouter } from 'react-router-dom'; +import { Link } from 'react-router-dom'; // import { AUTH_TOKEN } from '../../constants'; const Header = () => { @@ -17,6 +17,9 @@ const Header = () => {
New
+ {/*
+ Search +
*/} {/*
{authToken ? (
{ diff --git a/server/src/models/appointment.js b/server/src/models/appointment.js index ed792e2..2adde1e 100644 --- a/server/src/models/appointment.js +++ b/server/src/models/appointment.js @@ -24,6 +24,11 @@ const AppointmentSchema = new Schema({ deleted: { type: Boolean, required: false + }, + user: { + type: mongoose.Schema.Types.ObjectId, + ref: "user", + required: false } }); export default mongoose.model('appointment', AppointmentSchema); \ No newline at end of file diff --git a/server/src/resolvers.js b/server/src/resolvers.js index 5d99811..9691a39 100644 --- a/server/src/resolvers.js +++ b/server/src/resolvers.js @@ -1,5 +1,5 @@ import Appointment from './models/appointment.js'; -import User from './models/user.js' +import User from './models/user.js'; import jwt from 'jsonwebtoken'; export const resolvers = { diff --git a/server/src/resolvers/Mutation.js b/server/src/resolvers/Mutation.js index c98e157..0fc9393 100644 --- a/server/src/resolvers/Mutation.js +++ b/server/src/resolvers/Mutation.js @@ -2,7 +2,7 @@ const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); const { APP_SECRET } = require('../utils'); -function createAppointment(parent, args, context, info) { +function createAppointment(parent, args, context) { const { userId } = context; const newAppointment = context.mongo.appointment.create({ @@ -16,7 +16,7 @@ function createAppointment(parent, args, context, info) { return newAppointment; } -async function signup(parent, args, context, info) { +async function signup(parent, args, context) { const password = await bcrypt.hash(args.password, 10); const user = await context.mongo.user.create({ data: { ...args, password } @@ -30,7 +30,7 @@ async function signup(parent, args, context, info) { }; } -async function login(parent, args, context, info) { +async function login(parent, args, context) { const user = await context.mongo.user.findUnique({ where: { email: args.email } }); @@ -54,18 +54,18 @@ async function login(parent, args, context, info) { }; } -async function follow(parent, args, context, info) { +async function follow(parent, args, context) { const { userId } = context; const follow = await context.mongo.follow.findUnique({ where: { linkId_userId: { linkId: Number(args.linkId), - userId: userId + userId } } }); - if (Boolean(follow)) { + if (follow) { throw new Error( `Already followed the appointment: ${args.linkId}` ); diff --git a/server/src/resolvers/Query.js b/server/src/resolvers/Query.js index b68f879..c2fbc82 100644 --- a/server/src/resolvers/Query.js +++ b/server/src/resolvers/Query.js @@ -1,4 +1,4 @@ -async function feed(parent, args, context, info) { +async function feed(parent, args, context) { const where = args.filter ? { diff --git a/server/src/resolvers/Subscription.js b/server/src/resolvers/Subscription.js index bb4d0b5..54cbf82 100644 --- a/server/src/resolvers/Subscription.js +++ b/server/src/resolvers/Subscription.js @@ -1,26 +1,22 @@ -function newLinkSubscribe(parent, args, context, info) { - return context.pubsub.asyncIterator("NEW_LINK") +function newAppointmentSubscribe(parent, args, context) { + return context.pubsub.asyncIterator("NEW_APPOINTMENT"); } const newAppointment = { - subscribe: newLinkSubscribe, - resolve: payload => { - return payload - }, -} + subscribe: newAppointmentSubscribe, + resolve: payload => payload, +}; -function newFollowSubscribe(parent, args, context, info) { - return context.pubsub.asyncIterator("NEW_FOLLOW") +function newFollowSubscribe(parent, args, context) { + return context.pubsub.asyncIterator("NEW_FOLLOW"); } const newFollow = { subscribe: newFollowSubscribe, - resolve: payload => { - return payload - }, -} + resolve: payload => payload, +}; module.exports = { newAppointment, newFollow -} \ No newline at end of file +}; \ No newline at end of file diff --git a/server/src/schema.graphql b/server/src/schema.graphql index f061385..50939c6 100644 --- a/server/src/schema.graphql +++ b/server/src/schema.graphql @@ -7,9 +7,7 @@ type Query { orderBy: AppointmentOrderByInput ): Feed! allAppointments: [Appointment] - oneAppointment( - _id: ID! - ): Appointment + oneAppointment(_id: ID!): Appointment allUsers: [User] users: [User!]! } @@ -30,7 +28,7 @@ type Mutation { deleted: Boolean ): Appointment! updateAppointment( - _id: ID!, + _id: ID! title: String! description: String type: String! @@ -38,21 +36,10 @@ type Mutation { end: DateTime! deleted: Boolean ): Appointment - deleteAppointment( - _id: ID! - ) : Appointment - signup( - email: String! - password: String! - username: String! - ): AuthPayload - login( - email: String!, - password: String! - ): AuthPayload - follow( - appointmentId: ID! - ): Follow + deleteAppointment(_id: ID!): Appointment + signup(email: String!, password: String!, username: String!): AuthPayload + login(email: String!, password: String!): AuthPayload + follow(appointmentId: ID!): Follow } type Subscription { @@ -66,13 +53,13 @@ type User { username: String! email: String! password: String! - # appointments: [Appointment!]! + appointments: [Appointment!]! } -input UserInput{ +input UserInput { username: String! email: String! password: String! - # appointments: [Appointment!]! + appointments: [Appointment!]! } type AuthPayload { token: String @@ -88,9 +75,8 @@ type Appointment { start: DateTime! end: DateTime! deleted: Boolean - createdBy: User + user: User # follows: [Follow!]! - # createdAt: DateTime! } input AppointmentInput { title: String! @@ -99,11 +85,10 @@ input AppointmentInput { start: DateTime! end: DateTime! deleted: Boolean - } +} input AppointmentOrderByInput { title: Sort desc: Sort - # createdAt: Sort } # Follow schemas