Skip to content

Commit

Permalink
show thinking and error
Browse files Browse the repository at this point in the history
  • Loading branch information
nishio committed Feb 5, 2021
1 parent 01b5b29 commit 39818ac
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
4 changes: 0 additions & 4 deletions src/BOT_IS_SLEEPING.tsx

This file was deleted.

37 changes: 26 additions & 11 deletions src/NewTalk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import { ChatLine } from "./ChatContents";
import { PRESET_LOGS } from "./PRESET_LOGS";
import { USE_PRESET, INITIAL_LOGS, APIROOT } from "./App";
import { scrollToBottom } from "./scrollToBottom";
import { BOT_IS_SLEEPING } from "./BOT_IS_SLEEPING";
import {
BOT_IS_SLEEPING,
ERROR_ON_SERVER,
BOT_IS_THINKING,
} from "./PRESET_MESSAGES";

export let TalkID: string = "";
export const NewTalk = () => {
const [logs, setLogs] = useState(USE_PRESET ? PRESET_LOGS : INITIAL_LOGS);
const [lastKeywords, setLastKeywords] = useState([] as string[]);
const [otherKeywords, setOtherKeywords] = useState([] as string[]);

useEffect(getNewTalkID, []);
useEffect(() => {
getNewTalkID(setLogs);
}, []);

// when log changed, scroll to bottom after the component rendered
useEffect(scrollToBottom, [logs]);
Expand Down Expand Up @@ -116,22 +122,28 @@ function sendToServer(
) {
if (TalkID !== "") {
const data = { user: "nobody", talk: TalkID, text: text };
setLogs([...newLogs, BOT_IS_THINKING]);

fetch(APIROOT + "web/", {
mode: "cors",
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
},
}).then((response) => {
response.json().then((data) => {
if (data.text !== "") {
setLogs([...newLogs, { text: data.text, user: false }]);
setLastKeywords(data.last_kw);
setOtherKeywords(data.other_kw);
}
})
.then((response) => {
response.json().then((data) => {
if (data.text !== "") {
setLogs([...newLogs, { text: data.text, user: false }]);
setLastKeywords(data.last_kw);
setOtherKeywords(data.other_kw);
}
});
})
.catch(() => {
setLogs([...newLogs, ERROR_ON_SERVER]);
});
});
} else {
// bot is sleeping
setLogs([...newLogs, BOT_IS_SLEEPING]);
Expand All @@ -142,7 +154,7 @@ function sendToServer(
}
}

const getNewTalkID = () => {
const getNewTalkID = (setLogs: any) => {
fetch(APIROOT + "web/create", {
mode: "cors",
method: "GET",
Expand All @@ -152,5 +164,8 @@ const getNewTalkID = () => {
})
.then((text) => {
TalkID = text;
})
.catch(() => {
setLogs([ERROR_ON_SERVER]);
});
};
14 changes: 14 additions & 0 deletions src/PRESET_MESSAGES.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const BOT_IS_SLEEPING = {
text: "(Botを起こしています。数秒お待ちください...)",
user: false,
};

export const ERROR_ON_SERVER = {
text: "問題が発生したため会話を継続できません。",
user: false,
};

export const BOT_IS_THINKING = {
text: "(考え中...)",
user: false,
};

0 comments on commit 39818ac

Please sign in to comment.