Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-kd committed Oct 13, 2021
1 parent 6a16c2c commit 949d01f
Show file tree
Hide file tree
Showing 23 changed files with 273 additions and 179 deletions.
3 changes: 0 additions & 3 deletions components/Add.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as bp3 from '@blueprintjs/core'

class Add extends React.Component{
constructor(props){
console.log('ADD CONST')
super(props)
this.toggle.bind(this)
this.state = {isOpen:!this.props.openPortal}
Expand All @@ -13,11 +12,9 @@ class Add extends React.Component{

toggle(){
this.setState({isOpen:!this.state.isOpen})
console.log('DIV CLICKED',this.state.isOpen)
}

render(){
console.log('ADD RENCER',this.state.isOpen,this.props.openPortal)
if(this.props.openPortal && !this.state.isOpen ){
return (
<bp3.Callout className="container flex pad-tb add flex center" onClick={(e)=>{this.toggle()}}>
Expand Down
72 changes: 72 additions & 0 deletions components/AllLangs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from 'react'
import axios from 'axios'
import { authCtx } from '../context/authCtx'
import Lang from './Lang'
import * as bp3 from '@blueprintjs/core'
class AllLangs extends React.Component {
constructor(props) {
super(props)
this.state = {
}
this.state = {
count: 0,
data:{}
}
}
static contextType = authCtx




render() {
if (JSON.stringify(this.context.langs) == '[]'){
return <bp3.Button minimal loading></bp3.Button>
}
else{
let row1 = []
let row2 = []
let row3 = []
let row4 = []
let i = 1
this.context.langs.forEach(element => {
if(i%4 === 1){
console.log(element)
console.log(element.topics)
row1.push(<Lang key={element._id} id={element._id} name={element.name} topics={element.topics}/>)
}
if(i%4 === 2){
console.log(element)
row2.push(<Lang key={element._id} id={element._id} name={element.name} topics={element.topics}/>)
}
if(i%4 === 3){
console.log(element)
row3.push(<Lang key={element._id} id={element._id} name={element.name} topics={element.topics}/>)
}
if(i%4 === 0){
console.log(element)
row4.push(<Lang key={element._id} id={element._id} name={element.name} topics={element.topics}/>)
}
i++
})

return(
<div className="langs">
<div>
{row1}
</div>
<div>
{row2}
</div>
<div>
{row3}
</div>
<div>
{row4}
</div>
</div>
)
}
}
}

export default AllLangs
30 changes: 20 additions & 10 deletions components/Lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Image from 'next/image'
import * as bp3 from '@blueprintjs/core'
import styles from '../styles/components/lang.module.css'
import { Divider } from '@blueprintjs/core'
import Link from 'next/link'

class Lang extends React.Component{

Expand All @@ -11,6 +12,9 @@ class Lang extends React.Component{
this.topic.bind(this)
this.getAllTopic.bind(this)
}
static defaultProps = {
topics: []
}
getAllTopic(arr){
let elem = []
for(let i=0;i<arr.length;i++){
Expand All @@ -25,19 +29,25 @@ class Lang extends React.Component{
</bp3.Button>
}


render(){

return (
<bp3.Card interactive className={styles.lang} >
<div className="flex flex-row w-full">
<h3 className={'w-full '+styles.title}>{this.props.name}</h3>
<bp3.Button minimal icon="add" intent={bp3.Intent.PRIMARY}/>
</div>
<div className="mar-t"></div>
<bp3.ButtonGroup vertical className={styles.group}>
{this.getAllTopic(this.props.topics)}
</bp3.ButtonGroup>
</bp3.Card>
<>
<Link href={'/langs/'+ this.props.name +'?id='+this.props.id} passHref>
<bp3.Card interactive className={styles.lang} onClick={this.goto}>
<div className="flex flex-row w-full">
<h3 className={'w-full '+styles.title}>{this.props.name}</h3>
<Link href={'/langs/'+ this.props.name +'?id='+this.props.id} passHref><bp3.Button minimal icon="chevron-right" intent={bp3.Intent.PRIMARY} onClick={this.goto}/></Link>
</div>
<div className="mar-t"></div>
<bp3.ButtonGroup vertical className={styles.group}>
{this.getAllTopic(this.props.topics)}
</bp3.ButtonGroup>
</bp3.Card>
</Link>

</>
)
}
}
Expand Down
18 changes: 16 additions & 2 deletions components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@ class Layout extends react.Component {
}
static contextType = authCtx

componentDidMount() {
async componentDidMount() {
this.setState({
theme: localStorage.getItem('theme'),
})

if(localStorage.getItem('email')){
await axios.post('http://localhost:3030/user/getuserandlangs', {email: localStorage.getItem('email')})
.then( res =>{
this.context.setUser(res.data.user)
this.context.setLangs(res.data.langs)
})
.catch(
err => {
console.log(err)
}
)
}
}

changeTheme() {
Expand All @@ -31,8 +44,9 @@ class Layout extends react.Component {
localStorage.setItem('theme', 'dark')
}
}

render() {
console.log('WHOLE RENDER',this.context.user,this.context.isAuth)
console.log('render')
return (
<bp3.Callout className={'bp3-' + this.state.theme + ' h-full w-full pad-0'}>
<Nav
Expand Down
50 changes: 6 additions & 44 deletions components/Nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,16 @@ class Nav extends React.Component {
}
static contextType = authCtx

componentDidMount() {
const email = localStorage.getItem('email')

if (email) {
this.setState({isLoggedIn: true})
axios.post('http://localhost:3030/user/getuser',{email:email})
.then(
res => {
this.setState({
user: res.data,
})
this.context.setUser(res.data)
})
.catch(
err =>{
console.log(err)
}
)
}
else{
this.setState({isLoggedIn: false})
}

}

componentDidUpdate() {
this.setState()
console.log('COMPONEDIDUPDATE ',this.state.user)
}

handleName = (e) => {
console.log(e.target.value)
this.setState({ name: e.target.value })
}

handleEmail = (e) => {
console.log(e.target.value)
this.setState({ email: e.target.value })
}

Expand All @@ -80,7 +54,6 @@ class Nav extends React.Component {
password: this.state.password,
})
.then((res) => {
console.log(res.status)
if (res.status === 201) {
this.setState({ warn: res.data.message })
localStorage.removeItem('email')
Expand All @@ -94,7 +67,6 @@ class Nav extends React.Component {
}
})
.catch((err) => {
console.log(err)
})
}

Expand All @@ -106,7 +78,6 @@ class Nav extends React.Component {
password: this.state.password,
})
.then((res) => {
console.log(res)
if (res.status === 200) {
this.setState({ warn: '' })
localStorage.setItem('email', res.data.email)
Expand All @@ -116,14 +87,11 @@ class Nav extends React.Component {
this.context.setAuth(true)
}
if (res.status == 201) {
console.log('ERROE', res.data.error)
localStorage.removeItem('email')
this.setState({ warn: res.data.error })
}
})
.catch(function (error) {
console.log(error)
})
.catch()
}

handleLogout = () => {
Expand All @@ -144,7 +112,6 @@ class Nav extends React.Component {
password: this.state.password,
})
.then((res) => {
console.log(res.status)
if (res.status === 201) {
this.setState({ warn: res.data.message })
localStorage.removeItem('email')
Expand Down Expand Up @@ -173,7 +140,6 @@ class Nav extends React.Component {
password: this.state.password,
})
.then((res) => {
console.log(res)
if (res.status === 200) {
this.setState({ warn: '' })
localStorage.setItem('email', res.data.email)
Expand All @@ -183,14 +149,11 @@ class Nav extends React.Component {
this.context.setAuth(true)
}
if (res.status == 201) {
console.log('ERROE', res.data.error)
localStorage.removeItem('email')
this.setState({ warn: res.data.error })
}
})
.catch(function (error) {
console.log(error)
})
.catch()
}
}
Login() {
Expand Down Expand Up @@ -293,8 +256,7 @@ class Nav extends React.Component {
}
getElements() {
let userButton = <></>
console.log('USER', this.state.user)
if(!this.state.user){
if(JSON.stringify(this.context.user) === '{}'){
userButton = <bp3.Button
className={styles.button}
intent={bp3.Intent.PRIMARY}
Expand All @@ -312,7 +274,7 @@ class Nav extends React.Component {
intent={bp3.Intent.PRIMARY}
icon="user"
>
{this.state.user.name}
{this.context.user.name}
</bp3.Button>
}
>
Expand All @@ -321,7 +283,7 @@ class Nav extends React.Component {

}

if (this.state.isLoggedIn) {
if (JSON.stringify(this.context.user) !== '{}') {
return(
<bp3.Navbar.Group align={bp3.Alignment.RIGHT}>
<bp3.Button
Expand Down
29 changes: 27 additions & 2 deletions context/authCtx.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {createContext} from 'react'
import { useState } from 'react'
import axios from 'axios'


export const authCtx = createContext({
Expand All @@ -9,16 +10,40 @@ export const authCtx = createContext({

async function email(){
let email = await localStorage.getItem('email')
console.log('EMAIL FROM CTX',email)
return email
}

async function getUser(){
const e = await email()
let data = {}
if (e) {
await axios.post('http://localhost:3030/user/getuser',{email:e})
.then(res => {
data = res.data
})
}

return data
}

export const AuthProvider = ({children}) =>{
const [isAuth, setAuth] = useState(false)
const [user, setUser] = useState({})
const [langs, setLangs] = useState([])

// if (isAuth === false) {
// getUser().then(res => {
// setUser(res)
// setAuth(true)
// })
// }
// console.log('ctx')

// console.log(user)


return (
<authCtx.Provider value={{isAuth, user, setAuth, setUser}}>
<authCtx.Provider value={{isAuth, user, setAuth, setUser,langs,setLangs}}>
{children}
</authCtx.Provider>
)
Expand Down
Loading

0 comments on commit 949d01f

Please sign in to comment.