As someone who has had dogs for 25 years I have not yet encountered this problem, but with my present dog may encounter, I see how difficult a situation this is, and one requiring mountains of patience and compassion.
One thing I've turned to when my dogs were terminally ill is to darken the house and play music (New Age, symphony, something like that) and follow their lead. If they wanted to sleep, I let them sleep. I tried to keep them comfortable. Sometimes I would introduce new toys or treats.
There are things to help dog owners, like pee pads, although I've had trouble teaching dogs at that stage of life to use them.
One miracle drug for dogs that aren't eating is called Entyce. You could ask your veterinarian about that. Or the other way of looking at a dog that is near the end of their life is to allow them not to eat. My sister and brother in law had to do that with one of their dogs who died from kidney failure.
This is a super important question you've raised, and I will be closely reading all the responses.
I'm so sorry. It can be incredibly stressful to have a dog that is in ill health, especially because they cannot communicate precisely what they are feeling, what they want, what they don't want. It's very hard. Sending you heartfelt positive feelings.