-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from syke9p3/feat/saving
date generation
- Loading branch information
Showing
7 changed files
with
219 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { classify } from "./classifier.js"; | ||
import { setGlobalInput, setGlobalWordCount, setGlobalOutput, globalState } from './state.js'; | ||
import { updateWordCountDisplay, updateOutputDisplay, setInputFieldValue, getWordCountFromInputField } from './ui.js'; | ||
import { isValidWordCount, countWords } from './utils.js'; | ||
|
||
const inputField = document.getElementById("input-text"); | ||
const submitButton = document.getElementById("analyze-btn"); | ||
const clearButton = document.getElementById("clear-btn"); | ||
const exampleSelector = document.getElementById("sample-hate-speech"); | ||
|
||
const initializeGlobalState = () => { | ||
setGlobalInput(inputField.value); | ||
setGlobalWordCount(getWordCountFromInputField()); | ||
}; | ||
|
||
const updateWordCount = () => { | ||
const wordCount = getWordCountFromInputField(); | ||
setGlobalWordCount(wordCount); | ||
updateWordCountDisplay(wordCount); | ||
}; | ||
|
||
const updateInput = () => { | ||
setGlobalInput(inputField.value); | ||
}; | ||
|
||
document.addEventListener("DOMContentLoaded", function () { | ||
initializeGlobalState(); | ||
debug('globalState.input:', globalState.input); | ||
debug('globalState.wordCount:', globalState.wordCount); | ||
updateWordCountDisplay(globalState.wordCount); | ||
|
||
inputField.oninput = () => { | ||
updateInput(); | ||
updateWordCount(); | ||
debugGlobalState(); | ||
}; | ||
|
||
clearButton.onclick = () => { | ||
inputField.value = ""; | ||
updateInput(); | ||
exampleSelector.selectedIndex = 0; | ||
updateWordCount(); | ||
debugGlobalState(); | ||
}; | ||
|
||
submitButton.onclick = async () => { | ||
const wordCount = globalState.wordCount; | ||
|
||
if (!isValidWordCount(wordCount)) { | ||
debug('Invalid word count, not submitting input.'); | ||
return; | ||
} | ||
|
||
debug('Submitting input...'); | ||
globalState.isLoading = true; | ||
|
||
try { | ||
const output = await classify(globalState.input); | ||
setGlobalOutput(output); | ||
updateOutputDisplay(globalState.output); | ||
} finally { | ||
globalState.isLoading = false; | ||
} | ||
|
||
debugGlobalState(); | ||
}; | ||
|
||
exampleSelector.oninput = () => { | ||
setInputFieldValue(exampleSelector.value); | ||
updateWordCount(); | ||
updateInput(); | ||
debugGlobalState(); | ||
}; | ||
}); |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class GlobalState { | ||
constructor() { | ||
this._input = ''; | ||
this._output = []; | ||
this._isLoading = false; | ||
this._wordCount = 0; | ||
} | ||
|
||
get input() { | ||
return this._input; | ||
} | ||
|
||
set input(value) { | ||
this._input = value; | ||
} | ||
|
||
get output() { | ||
return this._output; | ||
} | ||
|
||
set output(value) { | ||
this._output = value; | ||
} | ||
|
||
get isLoading() { | ||
return this._isLoading; | ||
} | ||
|
||
set isLoading(value) { | ||
this._isLoading = value; | ||
} | ||
|
||
get wordCount() { | ||
return this._wordCount; | ||
} | ||
|
||
set wordCount(value) { | ||
this._wordCount = value; | ||
} | ||
|
||
debugGlobalState() { | ||
console.group('Global State Debug:'); | ||
console.log(`Global Input: ${this._input}`); | ||
console.log(`Global Word Count: ${this._wordCount}`); | ||
console.log('Global Output:', this._output); | ||
console.groupEnd(); | ||
} | ||
} | ||
|
||
export const globalState = new GlobalState(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters