Skip to content

Commit

Permalink
Rework config
Browse files Browse the repository at this point in the history
  • Loading branch information
AshDyson committed Mar 27, 2021
1 parent f4d1261 commit 85982d5
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 53 deletions.
30 changes: 28 additions & 2 deletions admin/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,36 @@ class App extends React.Component {
});
}

checkConfig() {
async checkConfig(setup = false) {
this.setState({
configChecked: true,
});
try {
let res = await Api.checkConfig();
if (res.error) {
this.props.msg({
type: "error",
message: res.error,
});
throw "Config error";
}
if (setup && !res.ready) {
setTimeout(() => {
this.checkConfig(true);
}, 10000);
return;
}
this.setState({
config: res.config,
loading: false,
});
} catch {
this.setState({
error: true,
loading: false,
});
}

Api.checkConfig()
.then((res) => {
console.log(res);
Expand Down Expand Up @@ -177,7 +203,7 @@ class App extends React.Component {
})}
</div>
<div className="page-wrap">
<Setup checkConfig={this.checkConfig} />
<Setup msg={this.msg} checkConfig={this.checkConfig} />
</div>
</div>
);
Expand Down
137 changes: 88 additions & 49 deletions admin/src/page/Setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,60 +195,93 @@ class Setup extends React.Component {
db: this.state.mongoType + this.state.db,
};
this.waitText();
Api.saveConfig(config)
.then(() => {
setTimeout(() => {
this.props.checkConfig();
}, 80000);
})
.catch(() => {
alert(
"Setup failed, check API is running and no errors, if this persists please contact the dev team."
);
window.location.reload(false);
try {
await Api.saveConfig(config);
this.props.checkConfig(true);
} catch {
this.props.msg({
type: "error",
message:
"Setup failed, check API is running and no errors, if this persists please contact the dev team.",
});
this.setTimeout(() => {
location.reload();
}, 3000);
}
}

async waitText() {
await this.timeout(10000);
this.setState({
finalText: "Creating configs...",
});
await this.timeout(10000);
this.setState({
finalText: "Connecting to the database...",
});
await this.timeout(10000);
this.setState({
finalText: "Loading your libraries...",
});
await this.timeout(10000);
this.setState({
finalText: "Finding your embarassing movies...",
});
await this.timeout(10000);
this.setState({
finalText: "Finding your embarassing movies... oh wow...",
});
await this.timeout(10000);
this.setState({
finalText: "Matching content against online sources...",
});
await this.timeout(10000);
this.setState({
finalText: "Getting your friends...",
});
await this.timeout(10000);
this.setState({
finalText: "Finishing up...",
});
await this.timeout(50000);
async waitText(it = 1) {
if (this.state.exiting) return;
let text,
to = 0;
switch (it) {
case 1:
text = "Starting things up...";
to = 1000;
break;
case 2:
text = "Creating configs...";
to = 1000;
break;
case 3:
text = "Connecting to the database...";
to = 1000;
break;
case 4:
text = "Getting your friends...";
to = 1000;
break;
case 5:
text = "Loading your libraries...";
to = 3000;
break;
case 6:
text = "Finding your embarassing movies...";
to = 3000;
break;
case 7:
text = "Finding your embarassing movies... oh wow...";
to = 1000;
break;
case 8:
text = "Building your Petio library...";
to = 3000;
break;
case 9:
text =
"Building your Petio library... Grab a cup of tea this could take a few minutes...";
to = 1000;
break;
case 10:
text =
"If you're library is extremely big or your server connection is remote this might take a little while...";
to = 120000;
break;
case 11:
text =
"We're not done, but you can start using Petio now while the rest of your library scans. Just a second we'll let you in....";
to = 120000;
break;
case 12:
text =
"We're not done, but you can start using Petio now while the rest of your library scans. Just a second we'll let you in....";
to = 3000;
break;
case 13:
location.reload();
break;
}
it++;
await this.timeout(to);
this.setState({
finalText: "This is taking a while...",
finalText: text,
});
await this.timeout(100000);
this.waitText(it);
}

componentWillUnmount() {
this.setState({
finalText: "Should really be done by now...",
exiting: true,
});
}

Expand Down Expand Up @@ -373,7 +406,12 @@ class Setup extends React.Component {
value={this.state.password}
onChange={this.inputChange}
/>
<button className="btn btn__square" onClick={this.saveUser}>
<button
className={`btn btn__square ${
this.state.password ? "" : "disabled"
}`}
onClick={this.saveUser}
>
Next
</button>
</div>
Expand Down Expand Up @@ -607,6 +645,7 @@ function SetupContainer(props) {
api={props.api}
user={props.user}
checkConfig={props.checkConfig}
msg={props.msg}
/>
);
}
Expand Down
24 changes: 23 additions & 1 deletion api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const bcrypt = require("bcryptjs");

// Config
const getConfig = require("./util/config");
const setupReady = require("./util/setupReady");
const Worker = require("./worker");

// Plex
Expand Down Expand Up @@ -80,13 +81,34 @@ class Main {
logger.log("info", "API: Setting up routes");
this.e.get("/config", async (req, res) => {
let config = getConfig();
let ready = false;
if (config) {
try {
let setupCheck = await setupReady();
if (setupCheck.ready) {
ready = true;
}
if (setupCheck.error) {
res.status(500).json({
error: "An error has occured",
});
return;
}
} catch {
res.status(500).json({
error: "An error has occured",
});
return;
}
}
res.json(
config
? {
config: true,
login_type: config.login_type ? config.login_type : 1,
ready: ready,
}
: { config: false, login_type: 1 }
: { config: false, login_type: 1, ready: ready }
);
});
this.setup();
Expand Down
2 changes: 1 addition & 1 deletion api/discovery/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async function create(id) {
}
} catch (err) {
//
console.log(err);
// console.log(err);
}
return;
}
Expand Down
33 changes: 33 additions & 0 deletions api/util/setupReady.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const Movie = require("../models/movie");
const Show = require("../models/show");
const User = require("../models/user");
const logger = require("./logger");

async function setupReady() {
try {
let [movie, show, user] = await Promise.all([
Movie.findOne(),
Show.findOne(),
User.findOne(),
]);
if (movie && show && user) {
return {
ready: true,
error: false,
};
} else {
return {
ready: false,
error: false,
};
}
} catch {
logger.error("CHK: Fatal Error unable to write Db to file!");
return {
ready: false,
error: "Database write error",
};
}
}

module.exports = setupReady;
1 change: 1 addition & 0 deletions api/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Worker {
}

async startCrons() {
// return; // for debug local
try {
await this.connnectDb();
const libUpdate = new LibraryUpdate();
Expand Down

0 comments on commit 85982d5

Please sign in to comment.