page.jsx 7.12 KB
Newer Older
1 2 3 4
'use client'

import { useQuery } from "@tanstack/react-query";
import { fetchApi } from "@/utils/fetchApi";
5 6
import { useSearchParams } from 'next/navigation'
import { useState } from "react";
7
import env from "dotenv"
8 9
import React from "react";
import ReactPlayer from "react-player/youtube";
10
import Button from "@/components/button";
11
import Image from "next/image";
12
import ErrorModal from "@/components/ErrorModal";
13 14 15

env.config();

16 17 18 19
function baixarArquivo(url) {
  window.open(`/api/arquivo?path=${encodeURIComponent(url)}`, '_blank');
};

20
export default function Aula() {
21
  const [modal, setModal] = useState(true)
22
  const [index, setIndex] = useState(0)
23

24 25
  const searchParams = useSearchParams()
  const modulo_id = searchParams.get('modulo_id')
26

27 28 29
  const { isLoading, error, data } = useQuery({
    queryKey: ['repoData'],
    queryFn: async () => {
30
      const response = await fetchApi(process.env.NEXT_PUBLIC_API_URL + 'aula?perPage=1000&modulo_id=' + modulo_id)
31

32 33 34 35 36 37 38
      if (response.isError) {
        throw response;
      } else {
        return response;
      }
    },
  })
39

40 41 42 43 44 45 46 47 48
  if (data && data.data.error) {
    return (
      <ErrorModal
        errorCode={data.data.code}
        errorMessage={data.data.errors[0].message}
      />
    )
  }

49 50 51
  const { isLoading: isLoadingArquivos, error: errorArquivos, data: dataArquivos } = useQuery({
    queryKey: ['arquivosData', index],
    queryFn: async () => {
52
      const response = await fetchApi(process.env.NEXT_PUBLIC_API_URL + 'arquivos?aula_id=' + data.data.data[index].id);
53

54 55 56 57 58 59
      if (response.isError) {
        throw response;
      } else {
        return response;
      }
    },
60
    enabled: !isLoading,
61 62
  });

63 64 65 66 67 68 69 70
  if (dataArquivos && dataArquivos.data.error) {
    return (
      <ErrorModal
        errorCode={dataArquivos.data.code}
        errorMessage={dataArquivos.data.errors[0].message}
      />
    )
  }
71

72 73 74 75 76 77 78 79 80
  if (dataArquivos && dataArquivos.isError || data && data.isError) {
    return (
      <ErrorModal
        errorCode="500"
        errorMessage="Ocorreu um erro interno."
      />
    )
  }

81
  if (isLoading || isLoadingArquivos) {
82 83 84 85 86 87 88 89 90 91
    return (
      <main>
        <div className={modal ? "w-3/4 overflow-hidden" : "w-full"}>
          <div className="flex items-center justify-center h-screen">
            <div className="border-t-4 border-verde border-solid w-16 h-16 rounded-full animate-spin"></div>
          </div>
        </div>
        <div className={`fixed top-0 right-0 h-screen w-1/4 bg-white border-l-2 border-gray-200 shadow-lg z-50 ${modal ? "block" : "hidden"}`}>
          <div className="flex items-center justify-between p-9 border-cinza_c2 border-b-2">
          </div>
92 93 94
          <div className="flex items-center justify-center h-screen">
            <div className="border-t-4 border-verde border-solid w-16 h-16 rounded-full animate-spin"></div>
          </div>
95 96 97 98 99
        </div>
      </main>
    )
  }

100 101
  const nextAula = () => {
    if (data && index < data.data.data.length - 1) {
102
      setIndex(index + 1);
103 104 105 106
    }
  };

  return (
107 108 109 110
    <main className="flex w-full">
      <div className={modal ? "w-3/4 overflow-hidden" : "w-full"}>
        <div className="flex flex-col gap-4">
          <ReactPlayer
111
            url={(data.data.data[index].video).split('&list=')[0]}
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
            controls
            width="100%"
            height="700px"
          />
          <button
            type="button"
            className={modal ? "hidden" : "block"}
            onClick={() => { setModal(true) }}
          >
            <Image
              className="absolute top-20 -right-24 z-50 opacity-50 hover:opacity-90 hover:right-0 duration-500"
              src="/conteudo.png"
              alt="botão de abrir o modal."
              width={130}
              height={40}
            />
          </button>
129
        </div>
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        <div className={modal ? "" : "w-full flex items-center justify-center"}>
          <div className={modal ? "" : "w-9/12"}>
            <div className="flex items-center justify-between p-3">
              <h1 className="w-full text-center text-2xl">{data && data.data.data[index].titulo}</h1>
              <Button className="w-1/12" texto="Finalizar" largura="10rem" onClick={nextAula}></Button>
            </div>

            <p className="text-justify p-3">{data && data.data.data[index].descricao}</p>

            <div>
              <h2 className="pl-3 text-xl">Conteúdo em arquivo</h2>
              {dataArquivos && dataArquivos.data.data?.map((arquivo) => {
                if (arquivo.tipo == "conteúdo") {
                  return (
                    <div key={arquivo.url} className="flex items-center p-3">
                      <Image
                        src="/pdf-image.png"
                        alt="imagem se um pdf."
                        width={24}
                        height={24}
                      />
                      <button type="button" onClick={() => baixarArquivo(arquivo.url)} className="pl-2 text-sm">
                        {arquivo.url.split('/').pop()}
                      </button>
                    </div>
                  );
                }
                return null
              })}
              <h3 className="pt-3 pl-3">Gabarito</h3>
              {dataArquivos && dataArquivos.data.data?.map((arquivo) => {
                if (arquivo.tipo == "gabarito") {
                  return (
                    <div key={arquivo.url} className="flex items-center p-3">
                      <Image
                        src="/pdf-image.png"
                        alt="imagem se um pdf."
                        width={24}
                        height={24}
                      />
                      <button type="button" onClick={() => baixarArquivo(arquivo.url)} className="pl-2 text-sm">
                        {arquivo.url.split('/').pop()}
                      </button>
                    </div>
                  );
                }
                return null
              })}
            </div>
          </div>
180 181
        </div>
      </div>
182 183 184 185 186 187 188 189 190 191 192 193 194

      {/* Modal */}
      <div className={`fixed top-0 right-0 h-screen w-1/4 bg-white border-l-2 border-gray-200 shadow-lg z-50 ${modal ? "block" : "hidden"}`}>
        <div className="flex items-center justify-between p-5 border-cinza_c2 border-b-2">
          <p className="text-xl">Conteúdo do Modulo</p>
          <button type="button" onClick={() => { setModal(false) }}>
            <Image
              className="mr-5"
              src="/x.png"
              alt="Fechar Modal"
              width={18}
              height={18}
            />
195
          </button>
196
        </div>
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        {/* Adicionando rolagem no conteúdo do modal */}
        <div className="overflow-y-auto h-full">
          {data && data.data.data?.map((aula, index) => (
            <div className="flex items-center justify-start p-3" key={index}>
              <Image
                src="/check.png"
                alt="imagem se um pdf."
                width={18}
                height={18}
              />
              <button className="ml-3 text-left" onClick={() => { setIndex(index) }}>
                {aula.titulo}
              </button>
            </div>
          ))}
        </div>
      </div>
    </main>
215
  )
216
}