This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const mongoose = require("mongoose"); | |
| const Joi = require("joi"); | |
| const Schema = mongoose.Schema; | |
| const PostSchema = new Schema({ | |
| title: { | |
| type: String, | |
| required: true, | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const mongoose = require("mongoose"); | |
| const Joi = require("joi"); | |
| const Schema = mongoose.Schema; | |
| const UserSchema = new Schema({ | |
| name: { | |
| type: String, | |
| required: true, | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| mongoose | |
| .connect( | |
| "<database connection string>", | |
| { useNewUrlParser: true, useUnifiedTopology: true } | |
| ) | |
| .then((result) => { | |
| server.listen(port); | |
| server.on("listening", onListening); | |
| }) | |
| .catch((err) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useState, useEffect } from "react"; | |
| import useInfiniteScroll from "./useInfiniteScroll"; | |
| import axios from "axios"; | |
| const App = () => { | |
| //we change here | |
| const [Items, setItems] = useState([]); | |
| const [isFetching, setIsFetching] = useState(false); | |
| //setting tha initial page | |
| const [page, setPage] = useState(0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const router = express.Router(); | |
| const usersController = require('../controllers/usersController'); | |
| router.get('/users', usersController.getAllUsers); | |
| module.exports = router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Import the User model from the Sequelize models | |
| const { User } = require('../models'); | |
| //setting up number of items to be fetched per page | |
| const getPagination = (_page, _limit) => { | |
| const limit = _limit ? +_limit : 20; | |
| const offset = _page ? _page * limit : 0; | |
| return { limit, offset }; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const faker = require('faker'); | |
| const users = [...Array(200)].map((user) => ( | |
| { | |
| firstName: faker.name.firstName(), | |
| lastName: faker.name.lastName(), | |
| email: faker.internet.email(), | |
| gender: faker.name.gender(), | |
| password: faker.internet.password(8), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useRef, useCallback } from "react"; | |
| const useInfiniteScroll = (callback, isFetching) => { | |
| //here we use useRef to store a DOM node and the returned object will persist regardless of re-renders | |
| const observer = useRef(); | |
| //useCallback takes a callback argument and an array dependency list and returns a memoized callback | |
| //which is guaranteed to have the same reference | |
| const lastElementRef = useCallback( | |
| (node) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //App.js | |
| import { useState, useEffect } from "react"; | |
| import useInfiniteScroll from "./useInfiniteScroll"; | |
| import axios from "axios"; | |
| const App = () => { | |
| //we change here | |
| const [Items, setItems] = useState([]); | |
| const [isFetching, setIsFetching] = useState(false); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //useInfiniteScroll.js | |
| import { useRef, useCallback } from "react"; | |
| const useInfiniteScroll = (callback, isFetching) => { | |
| //here we use useRef to store a DOM node and the returned object will persist regardless of re-renders | |
| const observer = useRef(); | |
| //useCallback takes a callback argument and an array dependency list and returns a memoized callback | |
| //which is guaranteed to have the same reference |
NewerOlder