Skip to content

Commit

Permalink
Добавил скелетон для хедера
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikMix committed Mar 22, 2024
1 parent a2ec1f6 commit b884f4a
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 125 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@types/webpack-dev-server": "^4.7.2",
"@typescript-eslint/eslint-plugin": "^7.1.1",
"@typescript-eslint/parser": "^7.1.1",
"@veglem/screact": "^1.0.5",
"@veglem/screact": "^1.0.9",
"@webpack-cli/generators": "^3.0.7",
"babel-loader": "^9.1.3",
"css-loader": "^6.10.0",
Expand Down
2 changes: 1 addition & 1 deletion public/src/components/Button/Button.sass
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
color: $white
text-align: center
cursor: pointer
transition: 0.3s
transition: transform 0.3s

&:hover
transform: scale(1.05)
Expand Down
19 changes: 14 additions & 5 deletions public/src/components/Header/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,40 @@ import {AppRouter} from "../../modules/router";
import {Logo} from "../Logo/logo";
import {Profile} from "../Profile/Profile";
import {AuthPage} from "../../pages/Auth";
import {AppUserStore} from "../../modules/stores/UserStore";
import {AppUserStore, UserActions, UserStoreState} from "../../modules/stores/UserStore";
import {AppDispatcher} from "../../modules/dispatcher";

export class Header extends ScReact.Component<any, any>{
state = {
isAuth: false,
avatarUrl: ""
userChecked: false,
avatarUrl: false
}

componentDidMount() {
AppUserStore.SubscribeToStore(this.updateState)
AppDispatcher.dispatch(UserActions.CHECK_USER)
}

updateState = (store) => {
updateState = (store:UserStoreState) => {
this.setState(state => ({
...state,
isAuth: store.isAuth,
avatarUrl: store.avatarUrl
avatarUrl: store.avatarUrl,
userChecked: true
}))
}

render() {
return (
<header id="header">
<Logo />
{ this.state.isAuth ? <Profile avatarUrl={this.state.avatarUrl}/> : (this.props.currPage !== AuthPage ? <Button label="Вход" onClick={() => AppRouter.go("/login")} /> : "") }
{ this.state.userChecked ? (
this.state.isAuth ? <Profile avatarUrl={this.state.avatarUrl}/> : (
this.props.currPage !== AuthPage ? <Button label="Вход" onClick={() => AppRouter.go("/login")} /> : ""
)
) : ""
}
</header>
)
}
Expand Down
50 changes: 34 additions & 16 deletions public/src/components/NoteEditor/NoteEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ export class NoteEditor extends ScReact.Component<any, any> {
state = {
open: false,
selectedNote: undefined,
saving: undefined
saving: undefined,
content: undefined
}

componentDidMount() {
AppNotesStore.SubscribeToStore(this.updateState)
const saveNote = debounce(this.handleKeypress, 1000)
document.querySelector(".note-editor").addEventListener("input", saveNote)
document.querySelector(".note-editor").addEventListener("input", debounce(this.handleKeypress, 1000))
}

handleKeypress = () => {
if (!this.state.selectedNote) {
return
if (this.state.selectedNote) {
this.saveNote()
}

this.saveNote()
}

saveNote = () => {
Expand All @@ -37,22 +35,42 @@ export class NoteEditor extends ScReact.Component<any, any> {
content: contentElem.innerText
}

AppDispatcher.dispatch(NotesActions.SAVE_NOTE, data)
if (data.title !== this.state.selectedNote.data.title || data.content !== this.state.selectedNote.data.content) {
AppDispatcher.dispatch(NotesActions.SAVE_NOTE, data)
}

this.setState(state => ({
...state,
selectedNote: {
id: state.selectedNote.id,
data: {
title: data.title,
content: data.content
},
update_time: state.selectedNote.update_time
}
}))
}

closeEditor = () => {
this.saveNote()

AppDispatcher.dispatch(NotesActions.CLOSE_NOTE)
}

updateState = (store:NotesStoreState) => {
// if (this.state.open && store.)

this.setState(state => ({
...state,
selectedNote: store.selectedNote,
open: store.selectedNote !== undefined,
saving: store.saving
}))
console.log("updateState")
console.log("2342342342342asdf")
console.log(this.state.selectedNote)
this.setState(state => {
return {
...state,
selectedNote: store.selectedNote,
open: store.selectedNote !== undefined,
saving: store.saving
}
})
console.log(this.state.selectedNote)
}

deleteNote = () => {
Expand Down
2 changes: 1 addition & 1 deletion public/src/components/SearchBar/SearchBar.sass
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@import "/public/src/utils/variables.sass"

.search
width: calc(100% - 30px)
width: 100%
display: flex
align-items: center
padding: 12px 16px
Expand Down
22 changes: 20 additions & 2 deletions public/src/modules/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {decode} from "./utils";
import {createUUID, decode} from "./utils";
import {Note} from "./stores/NotesStore";

export const isDebug = process.env.NODE_ENV === "development";
Expand Down Expand Up @@ -280,7 +280,6 @@ class NoteRequests {
}

Update = async(note, jwt: string)=> {
console.log(note)
const response = await Ajax.Post(this.baseUrl + "/" + note.id + "/edit", {
headers: {
"Authorization": jwt
Expand All @@ -293,6 +292,25 @@ class NoteRequests {
response.body.data = decode(response.body.data);
return response.body
}

Add = async (jwt:string) => {
const response = await Ajax.Post(this.baseUrl + "/add", {
headers: {
"Authorization": jwt
},
body: {
data: {
content: createUUID(),
title: "Новая заметка"
}
}
});

console.log(response.status)
console.log(response.body)
response.body.data = decode(response.body.data);
return response.body
}
}

export const AppAuthRequests = new AuthRequests();
Expand Down
53 changes: 3 additions & 50 deletions public/src/modules/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import {Header} from "../components/Header/header";
import {Background} from "../components/Background/Background";
import {Toasts} from "./toasts";
import NotesPageSkeleton from "../pages/Notes/Skeleton";
import {AppUserStore, UserActions, UserStoreState} from "./stores/UserStore";
import {AppDispatcher} from "./dispatcher";
import {AppNotesStore} from "./stores/NotesStore";
import AuthPageSkeleton from "../pages/Auth/Skeleton";
import {AuthPageLoader} from "../pages/Auth/loader";
import {NotesLoader} from "../pages/Notes/loader";

type routerState = {
currPage: {new(): Component<any, any> }
Expand All @@ -25,53 +24,6 @@ type RouterMapValue = {
skeleton: {new(): Component<any, any> }
}

const AuthPageLoader = async () => {
const p = new Promise((resolve, reject) => {
let isAuth = undefined;
const callback = (state: UserStoreState) => {
isAuth = state.isAuth;

AppUserStore.UnSubscribeToStore(callback);

if (isAuth) {
AppRouter.go("/")
reject()
} else {
resolve(null)
}
}

AppUserStore.SubscribeToStore(callback);
AppDispatcher.dispatch(UserActions.CHECK_USER)
})

return await p;
}

const NotesLoader = async () => {
const p = new Promise((resolve, reject) => {
let isAuth = undefined;
const callback = (state: UserStoreState) => {
isAuth = state.isAuth;

AppUserStore.UnSubscribeToStore(callback);

if (isAuth) {
AppNotesStore.init().then((store) => {
resolve({notes: store.notes});
})
} else {
AppRouter.go("/")
reject()
}
}

AppUserStore.SubscribeToStore(callback);
AppDispatcher.dispatch(UserActions.CHECK_USER)
})

return await p;
}

export class Router extends ScReact.Component<any, routerState> {
private pages: Map<string, RouterMapValue>
Expand Down Expand Up @@ -146,6 +98,7 @@ export class Router extends ScReact.Component<any, routerState> {
// }
// }));
})

} else {
this.setState(s => ({
...s,
Expand Down
45 changes: 36 additions & 9 deletions public/src/modules/stores/NotesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {AppNoteRequests} from "../api";
import {AppUserStore} from "./UserStore";
import {AppDispatcher} from "../dispatcher";
import {AppToasts, Toasts} from "../toasts";
import {createUUID} from "../utils";

export type Note = {
id: number,
Expand Down Expand Up @@ -69,6 +70,9 @@ class NotesStore extends BaseStore<NotesStoreState> {
case NotesActions.SAVE_NOTE:
await this.saveNote(action.payload);
break;
case NotesActions.CREATE_EMPTY_NOTE:
await this.createEmptyNote();
break;
}
});
}
Expand Down Expand Up @@ -167,20 +171,20 @@ class NotesStore extends BaseStore<NotesStoreState> {
async saveNote(data) {
if (this.state.selectedNote.id == data.id) {

// this.SetState(state => ({
// ...state,
// saving: true
// }))
this.SetState(state => ({
...state,
saving: true
}))

const note = await AppNoteRequests.Update(data, AppUserStore.state.JWT)
console.log(note)

AppToasts.success("Заметка успешно сохранена")

// this.SetState(state => ({
// ...state,
// saving: false
// }))
this.SetState(state => ({
...state,
saving: false
}))


// TODO: Смена стейта вызывает анфокус заметки ;(
Expand All @@ -199,6 +203,28 @@ class NotesStore extends BaseStore<NotesStoreState> {
// }))
}
}

async createEmptyNote() {
// TODO

const note = await AppNoteRequests.Add(AppUserStore.state.JWT)

// const note = {
// data: {
// content: "",
// title: "Новая заметка"
// },
// update_time: new Date().toISOString()
// }
//

this.SetState(state => ({
...state,
notes: [note, ...state.notes]
}))

console.log(this.state.notes)
}
}

export const NotesActions = {
Expand All @@ -210,7 +236,8 @@ export const NotesActions = {
OPEN_DELETE_NOTE_DIALOG: "OPEN_DELETE_NOTE_DIALOG",
CLOSE_DELETE_NOTE_DIALOG: "CLOSE_DELETE_NOTE_DIALOG",
DELETE_NOTE: "DELETE_NOTE",
SAVE_NOTE: "SAVE_NOTE"
SAVE_NOTE: "SAVE_NOTE",
CREATE_EMPTY_NOTE: "CREATE_EMPTY_NOTE"
}

export const AppNotesStore = new NotesStore();
1 change: 0 additions & 1 deletion public/src/modules/stores/UserStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class UserStore extends BaseStore<UserStoreState>{
super();
this.state.JWT = window.localStorage.getItem('Authorization');
this.registerEvents();
this.checkUser()
}

private registerEvents(){
Expand Down
Loading

0 comments on commit b884f4a

Please sign in to comment.