Commit c1299091859b926d3a91d1cbdce2b25a4092b5e3

Authored by voaserre@up.edu.ph
1 parent 524281ec

OJT21-61: Add log component and logs mock request

  1 +export default function Log ({ log }) {
  2 + return (
  3 + <div>
  4 + <h1>{log.ticket}</h1>
  5 + <p>{log.hours}</p>
  6 + <p>{log.details}</p>
  7 + </div>
  8 + )
  9 +}
@@ -2,12 +2,28 @@ import { rest } from 'msw' @@ -2,12 +2,28 @@ import { rest } from 'msw'
2 2
3 export const handlers = [ 3 export const handlers = [
4 // sample 4 // sample
5 - rest.get('*/', (req, res, ctx) => { 5 + rest.get('/', (req, res, ctx) => {
6 ctx.status = 200 6 ctx.status = 200
7 return res( 7 return res(
8 ctx.json({ 8 ctx.json({
9 sample: 'this is a sample' 9 sample: 'this is a sample'
10 }) 10 })
11 ) 11 )
  12 + }),
  13 + rest.get('https://api/logs', (req, res, ctx) => {
  14 + console.log('yooooooo')
  15 + ctx.status = 200
  16 + return res(
  17 + ctx.json({
  18 + logs: [
  19 + {
  20 + ticket: 'SAM-PLE',
  21 + details:
  22 + 'Today ...................................................................................',
  23 + hours: '5'
  24 + }
  25 + ]
  26 + })
  27 + )
12 }) 28 })
13 ] 29 ]
  1 +import { setupServer } from 'msw/node'
  2 +import { handlers } from './handlers'
  3 +
  4 +export const server = setupServer(...handlers)
1 import 'tailwindcss/tailwind.css' 1 import 'tailwindcss/tailwind.css'
2 2
  3 +if (process.env.NODE_ENV === 'development') {
  4 + if (typeof window === 'undefined') {
  5 + const { server } = require('../mocks/server')
  6 + server.listen()
  7 + console.log('SERVER STARTED')
  8 + } else {
  9 + const { worker } = require('../mocks/browser')
  10 + worker.start()
  11 + console.log('WORKER STARTED')
  12 + }
  13 +}
  14 +
3 function MyApp ({ Component, pageProps }) { 15 function MyApp ({ Component, pageProps }) {
4 return <Component {...pageProps} /> 16 return <Component {...pageProps} />
5 } 17 }
  1 +import Log from '../components/log.js'
  2 +const axios = require('axios')
  3 +
  4 +function Logs ({ logs }) {
  5 + return (
  6 + <div>
  7 + {logs.logs.map((log) => (
  8 + <Log log={log} />
  9 + ))}
  10 + </div>
  11 + )
  12 +}
  13 +
  14 +export async function getStaticProps () {
  15 + let logs = {}
  16 + try {
  17 + const response = await axios.get('https://api/logs')
  18 + console.log(response.data)
  19 + logs = response.data
  20 + } catch (error) {
  21 + console.error(error)
  22 + }
  23 + return {
  24 + props: {
  25 + logs
  26 + }
  27 + }
  28 +}
  29 +
  30 +export default Logs