WiFi only working while charging the phone
WiFi consumes battery. With this in mind, the operating system is designed to restrict or lower the strength of using the WiFi, with the basic goal of saving battery juice, to extend the battery life a bit.

In your case, the most likely culprit is your battery. Replace it, and all should be well.